EXAMPLES
Data validation
Configure validation rules by range, with invalid-input rejection, cell markers, and hover hints. Rules are stored as range data in snapshots.
Configure rules
setDataValidation(range, rule) sets a rule on one or more ranges and returns its ID. Each cell belongs to at most one rule. New coverage is removed from older rules, and rules with no remaining coverage are deleted. Changes are undoable. Coverage updates with row and column insertion, deletion, and movement. The example imports its initial rules through sheet.dataValidations, so those rules do not create undo entries.
const id = sheet.setDataValidation(
{ startRow: 0, startColumn: 1, endRow: 49, endColumn: 1 },
{
criteria: { type: "whole", operator: "between", value1: 1, value2: 100 },
errorStyle: "stop", // Default "stop"
errorMessage: "Quantity must be an integer from 1 to 100"
}
);
sheet.clearDataValidation(range); // Clear coverage and remove rules with no remaining ranges
sheet.getDataValidations(); // All current rules
sheet.getDataValidationAt(row, column); // Rule covering the specified cellRule types
| Name | Type | Description |
|---|---|---|
| whole | Integer | The value must be an integer; text is not implicitly converted to a number. operator compares it with value1 / value2. |
| decimal | Decimal | Any finite number, compared in the same way. |
| textLength | Text length | Compare the length of the string representation. Length bounds must be nonnegative integers. |
| date | Date | Compare date serials. value1 / value2 can use "2026-01-15" and are converted when the rule is created. String cell values are parsed as dates. |
| time | Time | Compare only the fractional part of the serial number, independent of the date. |
| list | List | The value must belong to items. Supports dropdown options, display modes, and option colors. |
| custom | Custom | Call a registered validator by name; see below. |
Error styles
errorStyle controls invalid editor submissions. stop rejects input; warning and information allow submission and show invalid markers. Rejection applies only to editor submissions. Paste, fill, setCell(), and replacement allow writes and mark invalid values.
| Name | Type | Description |
|---|---|---|
| stop (default) | Reject | Reject the submission and show a modal dialog. Confirming or pressing Escape discards the invalid edit, retains the original value, and exits editing. Repeated submissions are blocked while the dialog is open. |
| warning | Mark only | Allow submission and show a red triangle in the cell's upper-right corner. |
| information | Mark only | Same as warning. |
Dropdowns and display modes
List rules show a dropdown arrow on the right by default. Clicking it opens the options; selecting an option uses the normal cell-write path. Clicking outside, pressing Escape, or changing the active cell closes the list. renderMode controls its appearance:
| Name | Type | Description |
|---|---|---|
| arrow (default) | Text and arrow | Plain text with an arrow on the right. Text reserves arrow space and is clipped before it. |
| text | Text only | Show text without reserving arrow space. Double-click the cell to edit its value. |
| custom | Pill label | Show text and an arrow inside a colored pill. Clicking the pill opens the list. Falls back to arrow mode if the cell is too small. |
sheet.setDataValidation(range, {
criteria: {
type: "list",
items: ["Pending", "In progress", "Completed"],
// Option colors: pill backgrounds in custom mode; cell backgrounds in arrow / text mode.
// Accepts #rgb / #rrggbb / #rrggbbaa.
colors: { "Pending": "#e8eaed", "In progress": "#d2e3fc", "Completed": "#ceead6" }
},
renderMode: "custom",
showDropDown: true // false hides the dropdown arrow
});Custom rules
Custom rules store only a name and JSON-serializable params, supporting snapshots, undo, and row/column movement. Validator functions are not serialized; register them by name when creating an instance. They must be synchronous pure functions with low overhead, since visible cells can be validated during drawing. Cells referencing an unregistered validator are treated as valid.
sheet.registerDataValidator("even", (value, params, context) => {
// Return true for valid; false for invalid with the default message; a string for invalid with that message
// context.getCell(row, column) Read committed state only; supports rules depending on other cells
return typeof value === "number" && value % 2 === 0 ? true : "An even number is required";
});
sheet.setDataValidation(range, { criteria: { type: "custom", name: "even" } });Explicit validation
Setting a rule does not reject or alter existing data. findInvalidCells() returns invalid cell coordinates; validateCell() checks one cell. Use dataValidation: { showInvalidMarks: false } to hide invalid markers. Hover over an invalid cell for 400 ms to show its rule description. Moving away, scrolling, or editing closes it. Hint titles distinguish stop, warning, and information rules.
sheet.findInvalidCells(); // [{ row: 2, column: 1 }, ...]
sheet.validateCell(2, 1); // { valid: false, errorStyle: "stop", message: "…" }Validation example
Quantity rejects invalid input such as abc or 500. A delivery date before 2026-01-01 or notes over ten characters are accepted and marked invalid. Status and Priority support dropdown selection. The example includes invalid values; hover over them to view rule descriptions.