sheetjs/docbits/25_manip.md
SheetJS dc2128caca version bump 0.12.6: BrtUid
- `BrtUid` record (fixes #1044 h/t @gustavosimil)
- `sheet_to_json` allow default for errors (fixes #1035 h/t @arijitkanrar)
- docs and demos update
2018-03-19 17:42:55 -04:00

2.3 KiB

Working with the Workbook

The full object format is described later in this README.

Reading a specific cell (click to show)

This example extracts the value stored in cell A1 from the first worksheet:

var first_sheet_name = workbook.SheetNames[0];
var address_of_cell = 'A1';

/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];

/* Find desired cell */
var desired_cell = worksheet[address_of_cell];

/* Get the value */
var desired_value = (desired_cell ? desired_cell.v : undefined);
Adding a new worksheet to a workbook (click to show)

This example uses XLSX.utils.aoa_to_sheet to make a sheet and XLSX.utils.book_append_sheet to append the sheet to the workbook:

var new_ws_name = "SheetJS";

/* make worksheet */
var ws_data = [
  [ "S", "h", "e", "e", "t", "J", "S" ],
  [  1 ,  2 ,  3 ,  4 ,  5 ]
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);

/* Add the worksheet to the workbook */
XLSX.utils.book_append_sheet(wb, ws, ws_name);
Creating a new workbook from scratch (click to show)

The workbook object contains a SheetNames array of names and a Sheets object mapping sheet names to sheet objects. The XLSX.utils.book_new utility function creates a new workbook object:

/* create a new blank workbook */
var wb = XLSX.utils.book_new();

The new workbook is blank and contains no worksheets. The write functions will error if the workbook is empty.

Parsing and Writing Examples

The node version installs a command line tool xlsx which can read spreadsheet files and output the contents in various formats. The source is available at xlsx.njs in the bin directory.

Some helper functions in XLSX.utils generate different views of the sheets:

  • XLSX.utils.sheet_to_csv generates CSV
  • XLSX.utils.sheet_to_txt generates UTF16 Formatted Text
  • XLSX.utils.sheet_to_html generates HTML
  • XLSX.utils.sheet_to_json generates an array of objects
  • XLSX.utils.sheet_to_formulae generates a list of formulae