How to apply cell formats and number formats globally in spreadsheet?
This article explains how to apply the cell formatting and number formatting globally in a JavaScript Spreadsheet. Use the queryCellInfo client-side event to achieve this requirement. Apply cell formats and number formats using the setCell method. Row height or column width can also be applied globally using the setRow and setColumn methods respectively.
[HTML]
<div id="spreadsheet"></div>
[TS]
//Initialize the spreadsheet component.
let spreadsheet: Spreadsheet = new Spreadsheet({
sheets: [
{
name: 'Car Sales Report',
ranges: [{ dataSource: (dataSource as any).defaultData }],
},
],
created: (): void => {
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:F1'
);
},
queryCellInfo: (args): void => {
let sheet = spreadsheet.getActiveSheet();
setCell(
args.rowIndex,
args.colIndex,
sheet,
{
format: '0.00E+00',
style: {
borderBottom: '1px solid #000000',
borderLeft: '1px solid #000000',
borderRight: '1px solid #000000',
borderTop: '1px solid #000000',
fontSize: '14pt',
fontWeight: 'bold',
textAlign: 'right',
},
},
true
);
if (
!sheet.columns[args.colIndex] ||
sheet.columns[args.colIndex].height != 120
) {
//Condition to skip already applied columns.
setColumn(sheet, args.colIndex, { width: 120, customWidth: true });
}
if (!sheet.rows[args.rowIndex] || sheet.rows[args.rowIndex].height != 30) {
//Condition to skip already applied rows.
setRow(sheet, args.rowIndex, { height: 30, customHeight: true });
}
},
});
//Render the initialized spreadsheet component.
spreadsheet.appendTo('#spreadsheet');
Sample Link: https://stackblitz.com/edit/lbgncs
Screenshot:

Documentation:
https://help.syncfusion.com/document-processing/excel/spreadsheet/javascript-es6/formatting
API Links:
https://ej2.syncfusion.com/documentation/api/spreadsheet/#cellformat
https://ej2.syncfusion.com/documentation/api/spreadsheet/#numberformat
Demo Links:
https://ej2.syncfusion.com/demos/#/bootstrap5/spreadsheet/cell-formatting.html
https://ej2.syncfusion.com/demos/#/bootstrap5/spreadsheet/number-formatting.html
Conclusion
We hope you enjoyed learning about how to apply cell formats and number formats globally in spreadsheet.
You can refer to our JavaScript Spreadsheet page to learn about its other groundbreaking feature representations. You can also explore our JavaScript Spreadsheet Documentation to understand how to 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!