Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wasm-xlsxwriter to be used from both from web and nodejs #22

Merged
merged 24 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
441c8f8
Allow wasm-xlsxwriter to be used from both from web and nodejs
henrikekblad Nov 25, 2024
7e89c41
Add setFreezePanes, setFreezePanesTopCell
henrikekblad Nov 25, 2024
7b173d0
Fix feedback, add test
henrikekblad Nov 25, 2024
d222fe0
Add test for setFreezePanesTopCell
henrikekblad Nov 25, 2024
65f5443
cargo fmt
henrikekblad Nov 25, 2024
b010ec7
fix paths in tests
henrikekblad Nov 25, 2024
4df663e
Merge branch 'main' into multi-target
henrikekblad Nov 25, 2024
a7bb13a
Fix build fail
henrikekblad Nov 25, 2024
2dd6f17
Add Node.js demo code to README
henrikekblad Nov 26, 2024
00a47b4
Update README.md
henrikekblad Nov 26, 2024
43ec692
Fix setFreezePanes. The arguments should be 1,0 to freeze header row.
henrikekblad Nov 26, 2024
f5342c3
Merge branch 'main' into multi-target
henrikekblad Nov 27, 2024
8e030d9
add build script
henrikekblad Nov 27, 2024
0934ff5
fix too aggressive cleanup
henrikekblad Nov 27, 2024
4590db0
Merge branch 'estie-inc:main' into multi-target
henrikekblad Nov 27, 2024
8608eb9
fix RUSTFLAGS env variable problem
henrikekblad Nov 27, 2024
811c095
Merge branch 'estie-inc:main' into multi-target
henrikekblad Nov 28, 2024
dc2dde6
revert cross-env
henrikekblad Nov 28, 2024
fad0fb2
Fix path in next import, remove module-build
henrikekblad Dec 11, 2024
9654b2e
Fix paths in tests
henrikekblad Dec 11, 2024
e15ef10
Update import path
henrikekblad Dec 13, 2024
3c304a5
newly generated package-loch-file
henrikekblad Dec 13, 2024
a9c7c9a
re-generate lock file ith newer npm-version
henrikekblad Dec 13, 2024
91fd35c
Fix missing end-code block in README
henrikekblad Dec 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
rust/target
rust/pkg
pkg.*

**/node_modules
**/*.xlsx
Expand Down
58 changes: 55 additions & 3 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 Down Expand Up @@ -82,6 +82,58 @@ 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
21 changes: 21 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import shell from 'shelljs';

if (!shell.which('wasm-pack')) {
console.error("Run npm install to install all dependencies first");
}

// Clean up any existing build
shell.rm('-rf', 'pkg.*');

// Create the node output
shell.exec('wasm-pack build ./rust --target nodejs --out-dir ../pkg.node --release');

// Create the web output
shell.exec('wasm-pack build ./rust --target web --out-dir ../pkg.web --release');

// Create the bundler output
// shell.exec('wasm-pack build ./rust --target bundler --out-dir ../pkg.bundler --release');

// Clean up
shell.rm('pkg.*/.gitignore');
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/pkg.web";

export default function Page() {
return (
Expand Down
124 changes: 64 additions & 60 deletions examples/nextjs/package-lock.json

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

Loading
Loading