Restrict the user by entering only the limited formulas in JavaScript Spreadsheet.
This knowledge base shows how to restrict the user by entering only the limited formulas in JavaScript Spreadsheet. You can achieve this requirement using the actionBegin event and closeEdit method. On the actionBegin event maintain the list of formula to be supported. And use the boolean value to check whether the entered formula is supported or not and restrict the save action using the closeEdit method.
[HTML]
<div id="spreadsheet"> </div>
[TS]
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';
import * as dataSource from './default-data.json';
//Initialize Spreadsheet component.
let spreadsheet: Spreadsheet = new Spreadsheet({
sheets: [
{
name: 'Car Sales Report',
ranges: [{ dataSource: (dataSource as any).defaultData }],
columns: [{ width: 130 }],
},
],
created: (): void => {
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:B1'
);
},
actionBegin: (args: {action: string, args : any}): void => {
// List of supported formulas that you need in your end.
let formulaList: string[]= ['MAX', 'MIN', 'AVERAGE'];
let isFormulaValue: boolean = false;
// Condition for cellSave action and formula value.
if (
args.action == 'cellSave' &&
args.args.eventArgs.value.indexOf('=') > -1
) {
for (let i:number = 0; i < formulaList.length; i++) {
// Check whether the entered formula contains the supported formula list and set boolean variable.
if (args.args.eventArgs.value.indexOf(formulaList[i]) > -1)
isFormulaValue = true;
}
if (!isFormulaValue) {
args.args.eventArgs.cancel = true;
// Close the edit action for unsupported formulas.
spreadsheet.closeEdit();
}
}
},
});
spreadsheet.appendTo('#spreadsheet');
Sample Link:
https://stackblitz.com/edit/krkfcv-sq8lhw?file=index.ts
Output:
Documentation link:
https://ej2.syncfusion.com/documentation/spreadsheet/formulas
Conclusion
I hope you enjoyed learning about Adding a custom font family dropdown in the JavaScript Spreadsheet.
You can refer to our JavaScript Spreadsheet feature tour page to know about its other groundbreaking feature representations and
documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Spreadsheet example to understand how to create and manipulate data.
For current customers, you can check out our components from the
License and Downloads page. If you are new to Syncfusion, you can try our 30-day
free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!