How to Restrict the user by entering only the limited formulas in JavaScript Spreadsheet?
This article explains how to restrict the user to entering only limited formulas in a JavaScript Spreadsheet. You can achieve this requirement using the actionBegin event and the closeEdit method. In the actionBegin event, maintain the list of formulas to be supported. 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:
Refer to the working sample for additional details and implementation: Sample
Output:
Documentation link:
https://ej2.syncfusion.com/documentation/spreadsheet/formulas
Conclusion
We 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 learn 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, BoldDesk Support, or feedback portal. We are always happy to assist you!