Angular Spreadsheet JSON Import and Export
The Spreadsheet can serialize its entire workbook state, including cell values, formatting, formulas, and sheet configuration, to a plain JavaScript object and restore it at any time. This makes the JSON API ideal for scenarios like auto-saving drafts or persisting state to a database.
Importing JSON Data
To programmatically load a workbook from a JavaScript object, use the fromJSON method of SpreadsheetWidget. The object must conform to the DocumentDescriptor type, which describes the full workbook structure including sheets, rows, cells, and their properties.
public loadJSON(spreadsheet: SpreadsheetComponent): void {
spreadsheet.spreadsheetWidget.fromJSON({ sheets: data });
}
The following example demonstrates the fromJSON method in action.
Exporting to JSON
To serialize the current workbook state, use either the saveJSON or toJSON method of SpreadsheetWidget. Both methods return a JavaScript object that captures all sheets, cell values, formatting, and formulas.
The key difference is that saveJSON returns a Promise, which makes it the correct choice when the workbook contains embedded images that require asynchronous processing. Use toJSON for synchronous serialization when no images are present.
public onSave(spreadsheet: SpreadsheetComponent): void {
spreadsheet.spreadsheetWidget.saveJSON().then((data) => {
this.jsonData = data;
});
}
The following example demonstrates the saveJSON method in action.