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

Improvements to javascript and build process #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 120,
"singleQuote": false,
"trailingComma": "all"
}
168 changes: 89 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ Excelize-wasm is a pure WebAssembly / Javascript port of Go [Excelize](https://g

## Environment Compatibility

Browser | Version
---|---
Chrome | ≥57
Chrome for Android and Android Browser | ≥105
Edge | ≥16
Safari on macOS and iOS | ≥11
Firefox | ≥52
Firefox for Android | ≥104
Opera | ≥44
Opera Mobile | ≥64
Samsung Internet | ≥7.2
UC Browser for Android | ≥13.4
QQ Browser | ≥10.4
Node.js | ≥8.0.0
Deno | ≥1.0
| Browser | Version |
| -------------------------------------- | ---------- |
| Chrome | ≥71 |
| Chrome for Android and Android Browser | ≥108 |
| Edge | ≥79 |
| Safari on macOS and iOS | ≥12.2 |
| Firefox | ≥65 |
| Firefox for Android | ≥65 |
| Opera | ≥58 |
| Opera Mobile | ≥50 |
| Samsung Internet | ≥10.1 |
| UC Browser for Android | ≥13.4 |
| QQ Browser | ≥13.1 |
| Node.js | ≥12.0.0 |
| Deno | ≥1.0 |

## Basic Usage

### Installation

#### Node.js
#### Node.js or browser

```bash
npm install --save excelize-wasm
```

#### Browser
#### Browser using script tag

```html
<script src="excelize-wasm/index.js"></script>
Expand All @@ -59,17 +59,17 @@ const fs = require("fs");
init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => {
const f = excelize.NewFile();
// Create a new sheet.
const { index } = f.NewSheet("Sheet2")
const { index } = f.NewSheet("Sheet2");
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
f.SetCellValue("Sheet2", "A2", "Hello world.");
f.SetCellValue("Sheet1", "B2", 100);
// Set active sheet of the workbook.
f.SetActiveSheet(index)
f.SetActiveSheet(index);
// Save spreadsheet by the given path.
const { buffer, error } = f.WriteToBuffer();
if (error) {
console.log(error);
return
return;
}
fs.writeFile("Book1.xlsx", buffer, "binary", (error) => {
if (error) {
Expand All @@ -86,42 +86,42 @@ Create spreadsheet in browser:

```html
<html>
<head>
<meta charset="utf-8">
<script src="https://<your_hostname>/excelize-wasm/index.js"></script>
</head>
<body>
<div>
<button onclick="download()">Download</button>
</div>
<script>
function download() {
excelizeWASM.init("https://<your_hostname>/excelize-wasm/excelize.wasm.gz").then((excelize) => {
const f = excelize.NewFile();
// Create a new sheet.
const { index } = f.NewSheet("Sheet2")
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.")
f.SetCellValue("Sheet1", "B2", 100)
// Set active sheet of the workbook.
f.SetActiveSheet(index)
// Save spreadsheet by the given path.
const { buffer, error } = f.WriteToBuffer();
if (error) {
console.log(error);
return
<head>
<meta charset="utf-8" />
<script src="https://<your_hostname>/excelize-wasm/index.js"></script>
</head>
<body>
<div>
<button onclick="download()">Download</button>
</div>
<script>
function download() {
excelizeWASM.init("https://<your_hostname>/excelize-wasm/excelize.wasm.gz").then((excelize) => {
const f = excelize.NewFile();
// Create a new sheet.
const { index } = f.NewSheet("Sheet2");
// Set value of a cell.
f.SetCellValue("Sheet2", "A2", "Hello world.");
f.SetCellValue("Sheet1", "B2", 100);
// Set active sheet of the workbook.
f.SetActiveSheet(index);
// Save spreadsheet by the given path.
const { buffer, error } = f.WriteToBuffer();
if (error) {
console.log(error);
return;
}
const link = document.createElement("a");
link.download = "Book1.xlsx";
link.href = URL.createObjectURL(
new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }),
);
link.click();
});
}
const link = document.createElement("a");
link.download = "Book1.xlsx";
link.href = URL.createObjectURL(
new Blob([buffer],
{ type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })
);
link.click();
});
}
</script>
</body>
</script>
</body>
</html>
```

</details>
Expand All @@ -137,22 +137,22 @@ const fs = require("fs");
init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => {
const f = excelize.OpenReader(fs.readFileSync("Book1.xlsx"));
// Set value of a cell.
var { value, error } = f.GetCellValue("Sheet1", "B2")
var { value, error } = f.GetCellValue("Sheet1", "B2");
if (error) {
console.log(error);
return;
}
console.log(value)
console.log(value);
// Get all the rows in the Sheet1.
var { result, error } = f.GetRows("Sheet1");
if (error) {
console.log(error);
return;
}
result.forEach(row => {
row.forEach(colCell => {
process.stdout.write(`${colCell}\t`)
})
result.forEach((row) => {
row.forEach((colCell) => {
process.stdout.write(`${colCell}\t`);
});
console.log();
});
});
Expand Down Expand Up @@ -238,40 +238,40 @@ init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => {
const f = excelize.OpenReader(fs.readFileSync("Book1.xlsx"));
if (f.error) {
console.log(f.error);
return
return;
}
// Insert a picture.
var { error } = f.AddPictureFromBytes("Sheet1", "A2",
"Picture 1", ".png", fs.readFileSync("image.png"), {})
var { error } = f.AddPictureFromBytes("Sheet1", "A2", "Picture 1", ".png", fs.readFileSync("image.png"), {});
if (error) {
console.log(error);
return
return;
}
// Insert a picture to worksheet with scaling.
var { error } = f.AddPictureFromBytes("Sheet1", "D2", "Picture 2", ".png",
fs.readFileSync("image.jpg"), {ScaleX: 0.5, ScaleY: 0.5});
var { error } = f.AddPictureFromBytes("Sheet1", "D2", "Picture 2", ".png", fs.readFileSync("image.jpg"), {
ScaleX: 0.5,
ScaleY: 0.5,
});
if (error) {
console.log(error);
return
return;
}
// Insert a picture offset in the cell with printing support.
var { error } = f.AddPictureFromBytes("Sheet1", "H2", "Picture 3", ".png",
fs.readFileSync("image.gif"), {
OffsetX: 15,
OffsetY: 10,
PrintObject: true,
LockAspectRatio: false,
Locked: false
var { error } = f.AddPictureFromBytes("Sheet1", "H2", "Picture 3", ".png", fs.readFileSync("image.gif"), {
OffsetX: 15,
OffsetY: 10,
PrintObject: true,
LockAspectRatio: false,
Locked: false,
});
if (error) {
console.log(error);
return
return;
}
// Save spreadsheet by the given path.
var { buffer, error } = f.WriteToBuffer();
if (error) {
console.log(error);
return
return;
}
fs.writeFile("Book1.xlsx", buffer, "binary", (error) => {
if (error) {
Expand All @@ -285,6 +285,16 @@ init("./node_modules/excelize-wasm/excelize.wasm.gz").then((excelize) => {

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

### Build locally

Ensure you have node and go installed.

Install all dependencies with `npm install` and build with `npm run build`. This will compile the javascript to three different build targets and will build and generate the wasm gzip file.

To build just the JS, you can run `npm run build:js`

Run tests by running `npm run test`

## Licenses

This program is under the terms of the BSD 3-Clause License. See [https://opensource.org/licenses/BSD-3-Clause](https://opensource.org/licenses/BSD-3-Clause).
Expand Down
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
"script": "./dist/index.js",
"default": "./dist/main.js"
},
"scripts": {
"build": "npm-run-all clean build:js build:go",
"build:js": "rollup -c",
"build:go": "cd cmd && GOOS=js GOARCH=wasm go build -ldflags=\"-s -w\" -o ../dist/excelize.wasm && cd ../dist && gzip -9 -v -c excelize.wasm > excelize.wasm.gz && rm excelize.wasm",
"clean": "rm -rf dist",
"test": "node test/test-nodejs.js"
},
Comment on lines +20 to +26
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any of these scripts can be executed by running npm run x - e.x. npm run build.
npm-run-all is a library that allows running multiple npm commands without having to chain them together.

gzip -9 -v -c excelize.wasm > excelize.wasm.gz && rm excelize.wasm is probably not cross-platform compatible, likely won't work on Windows.

"files": [
"excelize.wasm.gz",
"index.d.ts",
"index.js",
"index.js.map",
"main.cjs",
"main.js"
"main.cjs.map",
"main.js",
"main.js.map"
],
"types": "index.d.ts",
"typings": "index.d.ts",
Expand Down Expand Up @@ -71,7 +81,11 @@
"@rollup/plugin-node-resolve": "15.0.1",
"@rollup/plugin-terser": "0.2.1",
"pako": "2.1.0",
"rollup-plugin-polyfill-node": "0.11.0",
"rollup": "3.8.0"
"rollup": "3.8.0",
"rollup-plugin-polyfill-node": "0.11.0"
},
"dependencies": {
"npm-run-all": "^4.1.5",
"rollup-plugin-dts": "^5.1.1"
}
}
Loading