Skip to main content

Sheets in DanfoJS

SheetJS is a JavaScript library for reading and writing data from spreadsheets.

DanfoJS is a library for processing structured data. It uses SheetJS under the hood for reading and writing spreadsheets.

This demo covers details elided in the official DanfoJS documentation.

Tested Deployments

This example was last tested on 2024 April 25 against DanfoJS 1.1.2.

Browser integration

The live demos on this page include the DanfoJS browser bundle:

  <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js"></script>

There are known issues with the documentation generator. If a demo explicitly prints "RELOAD THIS PAGE", please reload or refresh the page.

DataFrames and Worksheetsโ€‹

The DanfoJS DataFrame1 represents two-dimensional tabular data. It is the starting point for most DanfoJS data processing tasks. A DataFrame typically corresponds to one SheetJS worksheet2.

SpreadsheetDanfoJS DataFrame

pres.xlsx data

โ•”โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ โ”‚ Name โ”‚ Index โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 0 โ”‚ Bill Clinton โ”‚ 42 โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 1 โ”‚ GeorgeW Bush โ”‚ 43 โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 2 โ”‚ Barack Obama โ”‚ 44 โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 3 โ”‚ Donald Trump โ”‚ 45 โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 4 โ”‚ Joseph Biden โ”‚ 46 โ•‘
โ•šโ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•

DanfoJS SheetJS Integrationโ€‹

The official documentation inconsistently names the library object danfo and dfd. Since dfd is the browser global, the demos use the name dfd.

Methods to read and write spreadsheets are attached to the main dfd object.

Importing DataFramesโ€‹

readExcel3 accepts two arguments: source data and options.

The source data must be a string or File object. Strings are interpreted as URLs while File objects are treated as data.

Selecting a Worksheet

DanfoJS will generate a dataframe from one worksheet. The parser normally uses the first worksheet. The sheet property of the options object controls the selected worksheet. It is expected to be a zero-indexed number:

const first_sheet  = await dfd.readExcel(url, {sheet: 0});
const second_sheet = await dfd.readExcel(url, {sheet: 1});

More Parsing Options

The parsingOptions property of the options argument is passed directly to the SheetJS read method4.

For example, the sheetRows property controls how many rows are extracted from larger worksheets. To pull 3 data rows, sheetRows must be set to 4:

const first_three_rows = await dfd.readExcel(url, { parsingOptions: {
// see https://docs.sheetjs.com/docs/api/parse-options for details
sheetRows: 4
} });

URL sourceโ€‹

The following example fetches a test file, parses with SheetJS and generates a DanfoJS dataframe.

function DanfoReadExcelURL() {
const [text, setText] = React.useState("");
React.useEffect(() => { (async() => {
if(typeof dfd === "undefined") return setText("RELOAD THIS PAGE!");
const df = await dfd.readExcel("https://docs.sheetjs.com/pres.xlsx");
setText("" + df.head());
})(); }, []);
return (<pre>{text}</pre>);
}

File sourceโ€‹

The following example uses a file input element. The "File API"5 section of the "Local File Access" demo covers the browser API in more detail.

function DanfoReadExcelFile() {
const [text, setText] = React.useState("Select a spreadsheet");

return (<><pre>{text}</pre><input type="file" onChange={async(e) => {
if(typeof dfd === "undefined") return setText("RELOAD THIS PAGE!");

/* get first file */
const file = e.target.files[0];

/* create dataframe and pretty-print the first 10 rows */
const df = await dfd.readExcel(file);
setText("" + df.head());
}}/></>);
}

Exporting DataFramesโ€‹

toExcel6 accepts two arguments: dataframe and options. Under the hood, it uses the SheetJS writeFile method7.

Exported File Name

The relevant property for the file name depends on the platform:

PlatformProperty
NodeJSfilePath
BrowserfileName

The exporter will deduce the desired file format from the file extension.

Worksheet Name

The sheetName property specifies the name of the worksheet in the workbook:

dfd.toExcel(df, {
fileName: "test.xlsx", // generate `test.xlsx`
sheetName: "Export" // The name of the worksheet will be "Export"
});

The DanfoJS integration forces the .xlsx file extension. Exporting to other file formats will require low-level operations.

More Writing Options

The writingOptions property of the options argument is passed directly to the SheetJS writeFile method8.

For example, the compression property enables ZIP compression for XLSX and other formats:

dfd.toExcel(df, {fileName: "export.xlsx", writingOptions: {
// see https://docs.sheetjs.com/docs/api/write-options for details
compression: true
}});

Export to Fileโ€‹

The following example exports a sample dataframe to a XLSX spreadsheet.

function DanfoToExcel() {
if(typeof dfd === "undefined") return (<b>RELOAD THIS PAGE</b>);
/* sample dataframe */
const df = new dfd.DataFrame([{Sheet:1,JS:2},{Sheet:3,JS:4}]);
return ( <><button onClick={async() => {
/* dfd.toExcel calls the SheetJS `writeFile` method */
dfd.toExcel(df, {fileName: "SheetJSDanfoJS.xlsx", writingOptions: {
compression: true
}});
}}>Click to Export</button><pre>{"Data:\n"+df.head()}</pre></> );
}

Low-Level Operationsโ€‹

DanfoJS and SheetJS provide methods for processing arrays of objects.

flowchart LR
ws((SheetJS\nWorksheet))
aoo[[array of\nobjects]]
df[(DanfoJS\nDataFrame)]
ws --> |sheet_to_json\n\n| aoo
aoo --> |\njson_to_sheet| ws
df --> |\ndfd.toJSON| aoo
aoo --> |new DataFrame\n\n| df

Creating DataFramesโ€‹

The DataFrame constructor9 creates DataFrame objects from arrays of objects. Given a SheetJS worksheet object, the sheet_to_json method10 generates compatible arrays of objects:

function ws_to_df(ws) {
const aoo = XLSX.utils.sheet_to_json(ws);
return new dfd.DataFrame(aoo);
}

Generating Filesโ€‹

toJSON11 accepts two arguments: dataframe and options.

The format key of the options argument dictates the result layout. The column layout generates an array of objects in row-major order. The SheetJS json_to_sheet12 method can generate a worksheet object from the result:

function df_to_ws(df) {
const aoo = dfd.toJSON(df, { format: "column" });
return XLSX.utils.json_to_sheet(aoo);
}

The SheetJS book_new method creates a workbook object from the worksheet13 and the writeFile method14 will generate the file:

const ws = df_to_ws(df);
const wb = XLSX.utils.book_new(ws, "Export");
XLSX.writeFile(wb, "SheetJSDanfoJS.xlsb", { compression: true });

The following demo exports a sample dataframe to XLSB. This operation is not supported by the DanfoJS toExcel method since that method enforces XLSX.

function DanfoToXLS() {
if(typeof dfd === "undefined") return (<b>RELOAD THIS PAGE</b>);
/* sample dataframe */
const df = new dfd.DataFrame([{Sheet:1,JS:2},{Sheet:3,JS:4}]);
return ( <><button onClick={async() => {
/* generate worksheet */
const aoo = dfd.toJSON(df, { format: "column" });
const ws = XLSX.utils.json_to_sheet(aoo);

/* generate workbook */
const wb = XLSX.utils.book_new(ws, "Export");

/* write to XLS */
XLSX.writeFile(wb, "SheetJSDanfoJS.xlsb", { compression: true });
}}>Click to Export</button><pre>{"Data:\n"+df.head()}</pre></> );
}

Footnotesโ€‹

  1. See "Dataframe" in the DanfoJS documentation โ†ฉ

  2. See "Sheet Objects" โ†ฉ

  3. See "danfo.readExcel" in the DanfoJS documentation. โ†ฉ

  4. See "Reading Files" for the full list of parsing options. โ†ฉ

  5. See "File API" in "Local File Access" for more details. โ†ฉ

  6. See "danfo.toExcel" in the DanfoJS documentation. โ†ฉ

  7. See writeFile in "Writing Files" โ†ฉ

  8. See "Writing Files" for the full list of writing options. โ†ฉ

  9. See "Creating a DataFrame" in the DanfoJS documentation. โ†ฉ

  10. See sheet_to_json in "Utilities" โ†ฉ

  11. See "danfo.toJSON" in the DanfoJS documentation. โ†ฉ

  12. See json_to_sheet in "Utilities" โ†ฉ

  13. See book_new in "Utilities" โ†ฉ

  14. See writeFile in "Writing Files" โ†ฉ