Skip to content

Commit

Permalink
Merge pull request #33 from estie-inc/staging-22
Browse files Browse the repository at this point in the history
Staging changes
  • Loading branch information
kenkoooo authored Dec 17, 2024
2 parents 87601eb + 71c7454 commit 151a9e1
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ jobs:
cd examples/nextjs
npm ci
npm run build
- run: |
cd examples/nodejs
npm ci
npm run build
npm run start
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
rust/target
rust/pkg
/nodejs/
/web/

**/node_modules
**/*.xlsx
Expand All @@ -8,3 +9,4 @@ rust/pkg
**/dist

docs/

66 changes: 62 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# wasm-xlsxwriter [![NPM Version](https://img.shields.io/npm/v/wasm-xlsxwriter)](https://www.npmjs.com/package/wasm-xlsxwriter)

The `wasm-xlsxwriter` library is a lightweight wrapper around the write API of Rust's [`rust_xlsxwriter`](https://crates.io/crates/rust_xlsxwriter), compiled to WebAssembly (Wasm) with minimal setup to make it easily usable from JavaScript.
The `wasm-xlsxwriter` library is a lightweight wrapper around the write API of Rust's [`rust_xlsxwriter`](https://crates.io/crates/rust_xlsxwriter), compiled to WebAssembly (Wasm) with minimal setup to make it easily usable from JavaScript or Node.js.

With this library, you can generate Excel files in the browser using JavaScript, complete with support for custom formatting, formulas, links, images, and more.
With this library, you can generate Excel files in the browser or Node.js using JavaScript, complete with support for custom formatting, formulas, links, images, and more.

## Getting Started

Expand All @@ -12,7 +12,7 @@ To get started, install the library via npm:
npm install wasm-xlsxwriter
```

### Usage
### Usage Web

Here’s an example of how to use `wasm-xlsxwriter` to create an Excel file:

Expand All @@ -25,7 +25,7 @@ import xlsxInit, {
Workbook,
Image,
Url,
} from "wasm-xlsxwriter";
} from "wasm-xlsxwriter/web";

// Load the WebAssembly module and initialize the library.
await xlsxInit();
Expand Down Expand Up @@ -82,6 +82,64 @@ worksheet.insertImage(1, 2, image);
const buf = workbook.saveToBufferSync();
```

### Usage Node.js

```ts
import {
Color,
DocProperties,
Format,
FormatAlign,
Workbook,
} from "wasm-xlsxwriter";

/**
* Demo function that create a xlsx buffer from a header array and data rows
*
* @param {string[]} header - Sheet header strings
* @param {(string | number)[][]} rows - Array of arrays containing sheet rows
* @returns {Buffer} Excel file data
*/
function writeExcel(header: string[], rows: (string | number)[][]): Buffer {
// Create a new Excel file object.
const workbook = new Workbook();

// Set a doc property
const props = new DocProperties().setCompany("Test, Inc.");
workbook.setProperties(props);

// Add a worksheet with name to the workbook.
const worksheet = workbook.addWorksheet();
worksheet.setName("Export");

// Create some formats to use in the worksheet.
const headerStyle = new Format();
headerStyle
.setAlign(FormatAlign.Top)
.setTextWrap()
.setBackgroundColor(Color.red());

// Write sheet header
worksheet.writeRowWithFormat(0, 0, header, headerStyle);

// Write sheet data
worksheet.writeRowMatrix(1, 0, rows);

// Autofit columns
worksheet.autofit();

// Freeze header
worksheet.setFreezePanes(1, 0);

// Add autofilter to header
worksheet.autofilter(0, 0, rows.length, header.length - 1);

// Return buffer with xlsx data
const uint8Array = workbook.saveToBufferSync();
return Buffer.from(uint8Array);
}
```

## License

MIT
2 changes: 1 addition & 1 deletion examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import initModule, { Workbook } from "wasm-xlsxwriter";
import initModule, { Workbook } from "wasm-xlsxwriter/web";

export default function Page() {
return (
Expand Down
3 changes: 3 additions & 0 deletions examples/nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.xlsx
node_modules/
main.js
63 changes: 63 additions & 0 deletions examples/nodejs/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
Color,
DocProperties,
Format,
FormatAlign,
Workbook,
} from "wasm-xlsxwriter";
import * as fs from "fs";

/**
* Demo function that create a xlsx buffer from a header array and data rows
*
* @param {string[]} header - Sheet header strings
* @param {(string | number)[][]} rows - Array of arrays containing sheet rows
* @returns {Buffer} Excel file data
*/
function writeExcel(header: string[], rows: (string | number)[][]): Buffer {
// Create a new Excel file object.
const workbook = new Workbook();

// Set a doc property
const props = new DocProperties().setCompany("Test, Inc.");
workbook.setProperties(props);

// Add a worksheet with name to the workbook.
const worksheet = workbook.addWorksheet();
worksheet.setName("Export");

// Create some formats to use in the worksheet.
const headerStyle = new Format();
headerStyle
.setAlign(FormatAlign.Top)
.setTextWrap()
.setBackgroundColor(Color.red());

// Write sheet header
worksheet.writeRowWithFormat(0, 0, header, headerStyle);

// Write sheet data
worksheet.writeRowMatrix(1, 0, rows);

// Autofit columns
worksheet.autofit();

// Freeze header
worksheet.setFreezePanes(1, 0);

// Add autofilter to header
worksheet.autofilter(0, 0, rows.length, header.length - 1);

// Return buffer with xlsx data
const uint8Array = workbook.saveToBufferSync();
return Buffer.from(uint8Array);
}

const buf = writeExcel(
["Name", "Age", "City"],
[
["John", 30, "New York"],
["Jane", 25, "Los Angeles"],
]
);
fs.writeFileSync("export.xlsx", buf);
65 changes: 65 additions & 0 deletions examples/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"devDependencies": {
"@types/node": "^22.10.2",
"typescript": "^5.7.2"
},
"dependencies": {
"wasm-xlsxwriter": "file:../.."
},
"scripts": {
"build": "tsc main.ts",
"start": "node main.js"
}
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
"name": "wasm-xlsxwriter",
"version": "0.3.0",
"type": "module",
"main": "rust/pkg/wasm_xlsxwriter.js",
"types": "rust/pkg/wasm_xlsxwriter.d.ts",
"main": "nodejs/wasm_xlsxwriter.js",
"types": "nodejs/wasm_xlsxwriter.d.ts",
"browser": "web/wasm_xlsxwriter.js",
"files": [
"rust/pkg/wasm_xlsxwriter_bg.wasm",
"rust/pkg/wasm_xlsxwriter.js",
"rust/pkg/wasm_xlsxwriter.d.ts"
"web",
"nodejs"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/estie-inc/wasm-xlsxwriter"
},
"scripts": {
"build": "RUSTFLAGS='-C target-feature=+bulk-memory' wasm-pack build ./rust --target web --release && rm rust/pkg/.gitignore",
"build": "npm run build:web && npm run build:nodejs",
"build:web": "RUSTFLAGS='-C target-feature=+bulk-memory' wasm-pack build ./rust -t web -d ../web --release && rm ./web/.gitignore",
"build:nodejs": "RUSTFLAGS='-C target-feature=+bulk-memory' wasm-pack build ./rust -t nodejs -d ../nodejs --release && rm ./nodejs/.gitignore",
"test": "vitest"
},
"devDependencies": {
Expand Down
6 changes: 2 additions & 4 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import fs from "fs";
import path from "path";
import { assert } from "console";
import unzipper from "unzipper";
import initWasmBindgen from "../rust/pkg";
import initWasmBindgen from "../web";

export async function initWasModule() {
const wasmSource = await fs.promises.readFile(
"rust/pkg/wasm_xlsxwriter_bg.wasm"
);
const wasmSource = await fs.promises.readFile("web/wasm_xlsxwriter_bg.wasm");
const wasmModule = await WebAssembly.compile(wasmSource);
await initWasmBindgen({ module_or_path: wasmModule });
}
Expand Down
2 changes: 1 addition & 1 deletion test/empty.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Workbook } from "../rust/pkg";
import { Workbook } from "../web";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";

Expand Down
14 changes: 8 additions & 6 deletions test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
FormatUnderline,
Formula,
Workbook,
} from "../rust/pkg";
} from "../web";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile, saveFile } from "./common";

Expand Down Expand Up @@ -168,7 +168,7 @@ describe("xlsx-wasm test", () => {
});

describe("xlsx-wasm test", () => {
test('use width and height', async () => {
test("use width and height", async () => {
// Arrange
const workbook = new Workbook();
const worksheet = workbook.addWorksheet();
Expand Down Expand Up @@ -197,13 +197,13 @@ describe("xlsx-wasm test", () => {
const worksheet = workbook.addWorksheet();
const format = new Format();
format
.setFontName('Meiryo UI')
.setFontName("Meiryo UI")
.setFontSize(16)
.setAlign(FormatAlign.Center)
.setBorder(FormatBorder.Thin);

worksheet.writeWithFormat(0, 0, 'foo', format);
worksheet.writeWithFormat(1, 1, 'bar', format);
worksheet.writeWithFormat(0, 0, "foo", format);
worksheet.writeWithFormat(1, 1, "bar", format);

// Assert
const actual = await readXlsx(workbook.saveToBufferSync());
Expand Down Expand Up @@ -270,7 +270,9 @@ describe("xlsx-wasm test", () => {

// Assert
const actual = await readXlsx(workbook.saveToBufferSync());
const expected = await readXlsxFile("./expected/format_locked_unlocked.xlsx");
const expected = await readXlsxFile(
"./expected/format_locked_unlocked.xlsx"
);
expect(actual).matchXlsx(expected);
});
});
2 changes: 1 addition & 1 deletion test/image.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Workbook, Image } from "../rust/pkg";
import { Workbook, Image } from "../web";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, loadFile, readXlsx, readXlsxFile } from "./common";

Expand Down
8 changes: 1 addition & 7 deletions test/table.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Workbook,
TableFunction,
TableColumn,
Formula,
Table,
} from "../rust/pkg";
import { Workbook, TableFunction, TableColumn, Formula, Table } from "../web";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";

Expand Down
2 changes: 1 addition & 1 deletion test/write.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Workbook, Format, Formula, RichString } from "../rust/pkg";
import { Workbook, Format, Formula, RichString } from "../web";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";

Expand Down

0 comments on commit 151a9e1

Please sign in to comment.