cycyexcel
中文0.2.0

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.

ts
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 cell

Rule types

NameTypeDescription
wholeIntegerThe value must be an integer; text is not implicitly converted to a number. operator compares it with value1 / value2.
decimalDecimalAny finite number, compared in the same way.
textLengthText lengthCompare the length of the string representation. Length bounds must be nonnegative integers.
dateDateCompare date serials. value1 / value2 can use "2026-01-15" and are converted when the rule is created. String cell values are parsed as dates.
timeTimeCompare only the fractional part of the serial number, independent of the date.
listListThe value must belong to items. Supports dropdown options, display modes, and option colors.
customCustomCall 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.

NameTypeDescription
stop (default)RejectReject 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.
warningMark onlyAllow submission and show a red triangle in the cell's upper-right corner.
informationMark onlySame 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:

NameTypeDescription
arrow (default)Text and arrowPlain text with an arrow on the right. Text reserves arrow space and is clipped before it.
textText onlyShow text without reserving arrow space. Double-click the cell to edit its value.
customPill labelShow 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.
ts
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.

ts
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.

ts
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.

Waiting for an action...