sheetjs Adaptor developer README.md
Source: https://github.com/OpenFn/adaptors/tree/main/packages/sheetjs
language-sheetjs 
An OpenFn adaptor for reading and writing spreadsheet files with SheetJS.
Unlike most adaptors, sheetjs does not talk to an external system. It is a
data-transformation utility (like common) which operates on spreadsheet
content already on state - a base64 string or buffer fetched by another
adaptor, or a webhook payload.
Documentation
View the docs site for full technical documentation.
Configuration
This adaptor needs no configuration - it makes no network calls and requires
no credentials.
Usage
Reading a file
sheetToJson() converts a worksheet into an array of objects. It reads the
first sheet unless sheetName or sheetIndex says otherwise:
sheetToJson($.data.fileContent);
sheetToJson($.data.fileContent, { sheetName: 'Orders' });
The content type is detected automatically for buffers, base64 strings and
data URLs. Pass type to override the detection. The one ambiguous case is a
single-column CSV that is also valid base64:
sheetToJson($.data.fileContent, { type: 'string' });
Cells come back as raw values: numbers as numbers, and dates as Excel serial
numbers. Pass cellDates: true to get dates as JS Date objects, or
raw: false to get each cell's formatted text:
sheetToJson($.data.fileContent, { cellDates: true });
sheetToJson($.data.fileContent, { raw: false });
Any other SheetJS parse option or array output option is passed through to SheetJS.
Other common options:
// rows as arrays of cell values, rather than objects
sheetToJson($.data.fileContent, { header: 1 });
// read a fixed range and fill empty cells
sheetToJson($.data.fileContent, { range: 'A2:D40', defval: null });
sheetToCsv() converts a worksheet into a CSV string:
sheetToCsv($.data.fileContent);
sheetToCsv($.data.fileContent, { FS: ';' });
Writing a file
jsonToSheet() builds a spreadsheet from an array of objects and writes it to
state.data as a buffer:
jsonToSheet($.data.records, { sheetName: 'Export' });
// state.data = { buffer, bookType: 'xlsx', sheetName: 'Export' }
Ask for base64 instead when the destination expects a string:
jsonToSheet($.data.records, { sheetName: 'Export', output: 'base64' });
// state.data = { base64, bookType: 'xlsx', sheetName: 'Export' }
The keys of the objects become the column headers. Use header to fix the
column order, and bookType to choose the format:
jsonToSheet($.data.records, {
bookType: 'csv',
header: ['id', 'name', 'dateOfBirth'],
});
SheetJS prefixes csv output with a UTF-8 BOM so that Excel opens it with the
right encoding.
Using SheetJS directly
For anything the operations above don't cover, the full SheetJS library is
available in job code as xlsx. Its functions are not operations, so call them
inside fn(), not at the top level of a job:
fn(state => {
const workbook = xlsx.read(state.data.fileContent, { type: 'base64' });
state.rows = xlsx.utils.sheet_to_json(workbook.Sheets['Sheet1'], {
header: 1,
raw: false,
});
return state;
});
See the SheetJS API docs for everything it can do.
Notes
Working with untrusted files
SheetJS does not throw on content it cannot identify. It falls back to parsing
it as plaintext and returns a workbook with garbage in it. Pass type
explicitly when you know the format, so a bad guess can't pass silently.
There is no size limit on parsing. sheetRows caps how many rows are
materialized, but the archive is still decompressed in full first, so cap file
size upstream if you accept spreadsheets from an untrusted source.
Values are written as text, never as formulas. But csv has no cell types, so a
spreadsheet app opening a CSV will treat a value like =1+1 as a formula.
Sanitize untrusted data before writing a CSV that a person will open.
Performance
Each operation parses the file it is given, so reading several sheets from one file parses it several times. That takes around 470ms per call on a 10MB file, and a few milliseconds on a typical one.
Development
Clone the adaptors monorepo. Follow the "Getting Started" guide inside to get set up.
Run tests using pnpm run test or pnpm run test:watch
Build the project using pnpm build.
To build only the docs run pnpm build docs.
Dependencies
SheetJS is not published to the public npm registry. This package installs it
from the official SheetJS CDN tarball, pinned in package.json:
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
Test fixtures
test/fixtures/sample.xlsx is a generated binary. To change it, edit and re-run
node test/fixtures/generate.js.