3.8 KiB
title | pagination_prev | pagination_next |
---|---|---|
Go + Goja | demos/bigdata/index | solutions/input |
import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';
Goja is a pure Go implementation of ECMAScript 5.
The SheetJS Standalone scripts can be parsed and evaluated in a Goja context.
Integration Details
Initialize Goja
Goja does not provide a global
variable. It can be created in one line:
/* initialize */
vm := goja.New()
/* goja does not expose a standard "global" by default */
// highlight-next-line
v, err := vm.RunString("var global = (function(){ return this; }).call(null);")
Load SheetJS Scripts
The shim and main libraries can be loaded by reading the scripts from the file system and evaluating in the Goja context:
func safe_run_file(vm *goja.Runtime, file string) {
data, err := ioutil.ReadFile(file)
if err != nil { panic(err) }
src := string(data)
_, err = vm.RunString(src)
if err != nil { panic(err) }
}
// ...
safe_run_file(vm, "shim.min.js")
safe_run_file(vm, "xlsx.full.min.js")
To confirm the library is loaded, XLSX.version
can be inspected:
/* get version string */
v, err := vm.RunString("XLSX.version")
fmt.Printf("SheetJS library version %s\n", v)
Reading Files
Files can be read into []byte
:
/* read file */
data, _ := ioutil.ReadFile("sheetjs.xlsx")
[]byte
should be converted to an ArrayBuffer
from Go:
/* load into engine */
vm.Set("buf", vm.ToValue(vm.NewArrayBuffer(data)))
/* parse */
wb, _ = vm.RunString("wb = XLSX.read(buf, {type:'buffer'});")
Writing Files
"base64"
strings can be passed from the JS context to Go code:
/* write to Base64 string */
b64str, _ := vm.RunString("XLSX.write(wb, {type:'base64', bookType:'xlsx'})")
/* pull data back into Go and write to file */
buf, _ := base64.StdEncoding.DecodeString(b64str.String())
_ = ioutil.WriteFile("sheetjs.xlsx", buf, 0644)
Complete Example
:::note
This demo was tested in the following deployments:
Architecture | Git Commit | Go version | Date |
---|---|---|---|
darwin-x64 |
28ee0ee |
1.19.3 |
2023-06-05 |
darwin-arm |
28ee0ee |
1.20.4 |
2023-06-05 |
win10-x64 |
81d7606 |
1.20.2 |
2023-08-27 |
win11-arm |
fc55792 |
1.21.1 |
2023-09-25 |
linux-x64 |
fc55792 |
1.21.3 |
2023-10-11 |
linux-arm |
3dbe69d |
1.21.1 |
2023-08-30 |
At the time of writing, Goja did not have proper version numbers. Versions are identified by Git commit hashes.
:::
- Create a module and install dependencies:
mkdir SheetGoja
cd SheetGoja
go mod init SheetGoja
go get github.com/dop251/goja
- Download the SheetJS Standalone script, shim script and test file. Move all three files to the project directory:
- xlsx.full.min.js
- shim.min.js
- pres.numbers
{\ curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/shim.min.js curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js curl -LO https://sheetjs.com/pres.numbers
}
- Download
SheetGoja.go
:
curl -LO https://docs.sheetjs.com/goja/SheetGoja.go
- Build the standalone
SheetGoja
binary:
go build SheetGoja.go
- Run the demo:
./SheetGoja pres.numbers
If the program succeeded, the CSV contents will be printed to console and the
file sheetjsw.xlsb
will be created. That file can be opened with Excel.