docs.sheetjs.com/docz/docs/08-api/07-utilities/index.md
2023-06-13 13:49:52 -04:00

3.2 KiB

sidebar_position title pagination_prev
9 Utility Functions api/write-options

The utility functions in this section fall into two categories:

Data Packaging

The "Input" functions create SheetJS data structures (worksheets or workbooks) from rows of data or other common JS data representations

These functions are paired with write or writeFile to create exports. For example, the following diagram shows the steps to export an HTML TABLE to XLSX:

flowchart LR
  html{{HTML\nTABLE}}
  ws(SheetJS\nWorksheet)
  wb(SheetJS\nWorkbook)
  file[(workbook\nfile)]
  html --> |table_to_sheet\n\n| ws
  ws --> |book_new\nbook_append_sheet| wb
  wb --> |writeFile\n\n| file

Data Extraction

The "Output" functions extract data from worksheets to friendlier structures.

These functions are paired with read or readFile to process data from files. The following diagram shows the steps to generate an HTML TABLE from a URL:

flowchart LR
  url[(Remote\nFile)]
  ab[(Data\nArrayBuffer)]
  wb(SheetJS\nWorkbook)
  ws(SheetJS\nWorksheet)
  html{{HTML\nTABLE}}
  url --> |fetch\n\n| ab
  ab --> |read\n\n| wb
  wb --> |wb.Sheets\nselect sheet| ws
  ws --> |sheet_to_html\nfirst worksheet| html

Array Output

Generate rows of data from a worksheet

var arr = XLSX.utils.sheet_to_json(ws, opts);

This functions is described in a dedicated page

Array of Arrays Input

Create a worksheet from an array of arrays

var ws = XLSX.utils.aoa_to_sheet(aoa, opts);

Add data from an array of arrays to an existing worksheet

XLSX.utils.sheet_add_aoa(ws, aoa, opts);

These functions are described in a dedicated page

Array of Objects Input

Create a worksheet from an array of objects

var ws = XLSX.utils.json_to_sheet(aoo, opts);

Add data from an array of objects to an existing worksheet

XLSX.utils.sheet_add_json(ws, aoo, opts);

These functions are described in a dedicated page

HTML Table Input

Create a worksheet or workbook from a TABLE element

var ws = XLSX.utils.table_to_sheet(elt, opts);
var wb = XLSX.utils.table_to_book(elt, opts);

Add data from a TABLE element to an existing worksheet

XLSX.utils.sheet_add_dom(ws, elt, opts);

These functions are described in a dedicated page

HTML Output

Display worksheet data in a HTML table

var html = XLSX.utils.sheet_to_html(ws, opts);

This function are described in a dedicated page

Delimiter-Separated Output

Generate CSV from a Worksheet

var csv = XLSX.utils.sheet_to_csv(ws, opts);

Export worksheet data in "UTF-16 Text" or Tab-Separated Values (TSV)

var txt = XLSX.utils.sheet_to_txt(ws, opts);

These functions are described in a dedicated page

Formulae Output

Extract all formulae from a worksheet

var fmla_arr = XLSX.utils.sheet_to_formulae(ws);

This function is described in a dedicated page