Skip to content

jsf2xlsx

jsf2xlsx is a TypeScript package to convert a JSF spreadsheet to an Excel workbook (.xlsx file).

To use jsf2xlsx as a project dependency, run:

Terminal window
npm install @grid-is/jsf2xlsx

Save this as xlsx.js and run it with node xlsx.js for a quick example of how to emit a .xlsx file using the library.

import { Buffer } from "node:buffer";
import { writeFileSync } from "node:fs";
import { toXlsx } from "@grid-is/jsf2xlsx";
import JSZip from "jszip";
// Create the simplest workbook.
const jsf = {
name: "demo.xlsx",
sheets: [
{
name: "Sheet1",
cells: {
A1: { v: 100 },
},
},
],
};
// Construct a file container.
const container = new JSZip();
// Pass the JSF and container to the exporter.
toXlsx(jsf, container);
// Compress to XLSX archive.
const zip = await container.generateAsync({
type: "blob",
compression: "DEFLATE",
compressionOptions: { level: 9 },
});
// Save the XLSX to a local file.
writeFileSync(jsf.name, Buffer.from(await zip.arrayBuffer()));

The package includes a tool to convert JSF files into XLSX from the command line:

Terminal window
$ npx jsf2xlsx -h
Usage: jsf2xlsx <input> [-o <output>]
Converts JSF JSON to .xlsx via toXlsx.
Options:
-o, --output <file> Write XLSX output to a file (defaults to input name with .xlsx)
-h, --help Show this help text

The public interface to jsf2xlsx is the toXlsx function which relies heavily on the JSF Workbook type. Read all about them!