Skip to main content

eachCell()

Description

Iterates cells in the spreadsheet

info

If the range of cells is not specified, the method iterates the selected cells.

Usage

eachCell(
cb: (cellName: string, cellValue: any) => any,
range?: string
): void;

Parameters

  • callback - (required) a callback function
  • range - (optional) a range of cells to iterate

Example

const spreadsheet = new dhx.Spreadsheet("spreadsheet", {
// config parameters
});

spreadsheet.menu.data.add({
id: "validate",
value: "Validate",
items: [
{
id: "isNumber",
value: "Is number"
},
{
id: "isEven",
value: "Is even number"
}
]
});

function checkValue(check) {
spreadsheet.eachCell(function (cell, value) {
if (!check(value)) {
spreadsheet.setStyle(cell, { background: "#e57373" });
} else {
spreadsheet.setStyle(cell, { background: "" });
}
}, spreadsheet.selection.getSelectedCell());
}

spreadsheet.menu.events.on("click", function (id) {
switch (id) {
case "isNumber":
checkValue(function (value) { return !isNaN(value) });
break;
case "isEven":
checkValue(function (value) { return value % 2 === 0 });
break;
}
});

Related articles: Customization

Related sample: Spreadsheet. Menu