Spreadsheet JSON support with SheetJSON
Document Authoring uses SheetJSON as its native spreadsheet format. SheetJSON is the spreadsheet counterpart of DocJSON. It stores supported XLSX data as plain JSON.
Saving a spreadsheet as SheetJSON and loading it again preserves all supported data. Use SheetJSON as your working copy, and export to XLSX only when another app needs the spreadsheet.
Store and load SheetJSON, but don’t read or change its contents in your code.
Before you start, install and run the Document Authoring library. For setup instructions, refer to the getting started guide.
Working with SheetJSON in the editor
The examples omit error handling. Add error handling before you use this code in production.
Loading SheetJSON
Call DocAuthSystem.loadSpreadsheet to load SheetJSON:
// Assuming the `system` instance exists.
const spreadsheet = await system.loadSpreadsheet(fetch('./spreadsheet.json'));
const editor = await system.createSpreadsheetEditor( document.getElementById('editor'), { spreadsheet },);Pass a Response, Blob, or bytes instead of a parsed object. Document Authoring reads large spreadsheets from these inputs in parts, but a parsed object must fit in memory.
Saving SheetJSON
Call DocAuthSpreadsheet.saveSpreadsheet or DocAuthSpreadsheet.saveSpreadsheetJSONString to save SheetJSON:
// Assuming the `editor` instance exists.
const currentSpreadsheet = editor.currentSpreadsheet();
const blob = await currentSpreadsheet.saveSpreadsheet();console.log('Blob', blob);
const sheetJSON = await currentSpreadsheet.saveSpreadsheetJSONString();console.log('JSON string', sheetJSON);saveSpreadsheet() works for spreadsheets of any size. saveSpreadsheetJSONString() returns an empty string when the spreadsheet is too large for a JavaScript string.
Example
This example shows a minimal valid SheetJSON spreadsheet for the current format version. It contains one sheet named Sheet1 and the number 42 in cell A1:
{ "type": "https://pspdfkit.com/document-authoring/persistence/spreadsheet", "version": 0, "spreadsheet": { "sheets": [ { "name": "Sheet1", "sheetId": 1, "rows": [ { "row": 1, "cells": [ { "type": "number", "col": 1, "v": 42 } ] } ] } ] }}Rows and columns start at 1. Rows and cells are sparse, so SheetJSON writes only those with values or formatting.
Exploring more
To export a spreadsheet to XLSX or PDF, refer to the save and export spreadsheets guide.
If you need more SheetJSON format details, contact Sales.