You can lock a cell or several cells to protect their content from editing. The locked cells will have a small yellow lock in the bottom right corner.
For this purpose, you need to use the lockCell method and pass three parameters to it:
// locks the cell at the intersection of the 3rd row and 2nd column
$$("ssheet").lockCell(3, 2, true, "Sheet1");
You can also lock/unlock several cells at a time with the lockCell method. Call it with different parameters:
// locks 7 cells in the 2nd row
$$("ssheet").lockCell({ row:2, column:1 }, { row:2, column:7 }, true);
// locks 7 cells in the 2nd column
$$("ssheet").lockCell({ row:1, column:2 }, { row:7, column:2 }, true);
// locks 10 cells in the 1st and 2nd rows
$$("ssheet").lockCell({ row:1, column:1 }, { row:2, column:5 }, true);
In case the cell/cells to lock aren't specified, the method will lock the selected cell.
You can modify the default styling of locked cells, by disabling yellow locks and applying some background color via CSS:
<style>
.webix_lock:after{
content:" ";
}
.webix_lock {
background-color:#99f29d;
}
</style>
Related sample: Styling locked cells
You can check, whether a cell is locked, with the help of the isCellLocked method.
It takes two parameters:
and returns true, if the cell is locked and false if it's unlocked.
var isLocked = $$("ssheet").isCellLocked(3, 2);
It's possible to add an editor into a cell of the sheet. It can include either some custom options or values of a cell range. You can also explicitly specify whether to add an empty option.
Use the setCellEditor to do it. The method expects three parameters:
$$("ss1").setCellEditor(8, 1, {
editor:"ss_richselect",
options:["One", "Two", "Three"]
}, "Sheet1");
// or
$$("ss1").setCellEditor(8, 2, {
editor:"ss_richselect",
options:"B3:B7",
empty:true
}, "Sheet1");
You can get the editor set in a cell with the help of the getCellEditor method.
The method takes the following parameters:
$$("ss1").getCellEditor(8, 1, "Sheet1");
It will return an object with two properties:
{ editor:"ss_richselect", options:["One","Two","Three"] }
// or
{ editor:"ss_richselect", options:"B3:B7" }
You can add checkboxes and radio buttons into cells, mark them and check their states with the help of the corresponding SpreadSheet API methods.
To add checkboxes in a cell, use the addCheckbox method. To add radio buttons in a cell, use the addRadio method. Both methods take as a parameter an object with the start and end cells of the range to add checkboxes or radio buttons into:
{row:id, column:id}
{row:id, column:id}
$$("ssheet").addRadio({
start:{row:1, column:1},
end:{row:3, column:1}
});
$$("ssheet").addCheckbox({
start:{row:1, column:3},
end:{row:3, column:3}
});
To mark a checkbox, apply the markCheckbox method. To mark a radio button, make use of the markRadio method. Both methods take two parameters:
$$("ssheet").markRadio(2,1);
$$("ssheet").markCheckbox(1,3);
If you need to get the state of a checkbox or a radio button, you can apply the getCellValue method to the necessary cell:
const isChecked = $$("ssheet").getCellValue(1, 1, false);
// -> 1 - checked, 0 - unchecked
Another way to check the state is to refer to the cell with the necessary checkbox or radio button via the setCellValue method in a different cell. You will get 1, if the checkbox/radio button is marked and 0, if it isn't marked. For example:
// getting the state of the radio button from the cell A1 in the cell B1
$$("ssheet").setCellValue(1,2,"=A1");
You can also specify a more complex condition, e.g. to use some text that will be rendered in the resulting cell, depending on the state of the checkbox or radio button in the referred cell:
// getting the state of the radio button from the cell A2 in the cell A5
$$("ssheet").setCellValue(5,1,"=IF(A2,\"A2 marked\", \"A2 is not marked\")");
You can set a filter inside of a cell. This is how you can add a filter from the UI:
The setCellFilter method will help you to set a filter via the API. You need to pass the following parameters to this method:
// an array of options
$$("ss1").setCellFilter(1, 2, {
options: ["", "Europe", "Asia", "America"],
mode: "text",
value: values,
lastRow: 3
}, "Sheet1");
// a range of cells references
$$("ss1").setCellFilter(2, 2, {
options: "B3:B7",
mode: "text",
value: values,
lastRow: 3
}, "Sheet1");
It is possible to specify a range of cells references the values of which will be filtered or an array of filter options as a third parameter instead of the filter object:
$$("ssheet").setCellFilter(2,1, ["", "Europe", "Asia", "America"] );
// or
$$("ssheet").setCellFilter(2,2, "B3:B7");
To get a filter set in a cell, make use of the getCellFilter method. It takes the following parameters:
and returns an object with a set of options and the IDs of the row and column:
Check the example below:
$$("ssheet").getCellFilter(2, 1, "Sheet1");
// -> { options: Array(4), row: 2, column: 1 }
SpreadSheet allows you to sort values within a selected or specified range of cells via the sortRange method. You can pass two optional parameters to it:
// sorts the specified range in the default ("asc") order
$$("ssheet").sortRange("B2:B4");
// sorts the specifed range in the descending order
$$("ssheet").sortRange("B2:B4", "desc");
// sorts the selected range in the descending order
$$("ssheet").sortRange(null,"desc");
There is a possibility to add a validation rule for the content of a cell. A validation rule can be added to a cell through the SpreadSheet interface, namely: via the Validation button on the Toolbar:
or via the Menu or Context Menu options. A click on the "Add data validation" option will open a dialog popup:
This popup contains a set of validation rule types and their attrubutes. After a user adds a validation rule to a cell, a click on it will call a textarea with the rule details:
Each validation rule includes a number of parameters:
A validation rule can have one of the following types:
Depending on the type of the rule, it can have the following attributes:
You can specify validation rules for a cell directly in a data source.
For this purpose, use the validation module of the data object. You can specify a set of validation rules in one array:
data.validation = [
[
"2",
"1",
{
"type": "number",
"integer": 1,
"empty": 1,
"condition": "greater",
"value": "0",
"inputMessage": "Rules:\n\n- integer greater than 0\n- include empty",
"errorHandle": "info",
"errorTitle": "Incorrect data!",
"errorMessage": "Should be integer greater than 0!"
}
],
[
"2",
"2",
{
"type": "date",
"empty": 0,
"condition": "greater",
"value": "45292",
"inputMessage": "Rules:\n\n- date after 01/01/2024\n- exclude empty",
"errorHandle": "stop",
"errorTitle": "Incorrect data!",
"errorMessage": "Should be date after 01/01/2024!"
}
]
];
The validation collection also allows you to manage validation rules:
You can add a validation rule to a cell by using the validation.add(row, column, rule, page) method. It takes the following parameters:
// adding a validation rule for the cell B3 of the page 2
$$("ssheet").validation.add(
3,
2,
{
"type": "number",
"integer": 1,
"empty": 1,
"condition": "greater",
"value": "0",
"inputMessage": "Rules:\n\n- integer greater than 0\n- include empty",
"errorHandle": "info",
"errorTitle": "Incorrect data!",
"errorMessage": "Should be integer greater than 0!"
},
2
);
To remove the applied validation rule from a cell, use the validation.remove(row, column, page) method. It takes the following parameters:
// removing the validation rule from the cell B3 on page 2
$$("ssheet").validation.remove(3, 2, "Sheet1");
To get the validation rule applied to a cell, use the validation.get(row, column, page) method. It takes the following parameters:
// getting the validation rule of the cell B3 on page 2
$$("ssheet").validation.get(3, 2, "Sheet1");
You can add/remove highlighting to/from a cell with applied validation rules. Use the validation.highlight(state, page) method. It takes the following parameters:
// removing highlighting from cells with applied validation rules from the page 2
$$("ssheet").validation.highlight(false, "Sheet1");
You can add a small chart into a cell to display tendencies of data values changing in a range of cells.
To insert a sparkline inside of a cell, use the addSparkline method with the following parameters:
$$("ssheet").addSparkline(rowId, columnId, config, page);
Let's insert a blue sparkline of the Line type into the cell E5. The passed parameters will be as follows:
$$("ssheet").addSparkline(5, 5, {type:"line", range:"B4:E4", color:"#6666FF"});
Related sample: Adding sparklines
You can add an image into a cell to illustrate data in the spreadsheet.
To insert an image into a cell, use the addImage method. You need to pass the following parameters to this method:
$$("ssheet").addImage(2,3, "http://docs.webix.com/media/desktop/image.png", "Sheet1");
There is a possibility to add a comment into a certain cell of SpreadSheet.
Use the add() method of the comments object. It takes three parameters:
// adding a comment into the cell B3
$$("ssheet").comments.add(3, 2, "text", "Sheet1");
The API of the comments object also makes it possible to get a comment of a particular cell or to delete a comment that is no longer needed:
// getting a comment for the cell B3 of Sheet1
$$("ssheet").comments.get(3, 2, "Sheet1");
// removing a comment from the cell B3 of Sheet1
$$("ssheet").comments.remove(3, 2, "Sheet1");
You can specify what data will be displayed in the SpreadSheet cells by using placeholders.
A placeholder is an object with data properties which can be set as SpreadSheet values. To define a placeholder use the setPlaceholder method:
$$("ssheet").setPlaceholder({value:"France", expense:1366, income:842});
To specify a placeholder's property in a cell instead of a value, use the ={{property}} construction. For example, for cells with the "expense" values you should specify placeholders as "={{expense}}".
If you specify a new placeholder for a SpreadSheet, values of all cells where properties of this placeholder are defined will be updated.
$$("ssheet").setPlaceholder({value:"Poland", expense:684, income:781});
Back to top