This commit is contained in:
SheetJS 2023-07-21 05:17:32 -04:00
parent 272cecf23c
commit be15eb3620
6 changed files with 142 additions and 39 deletions

@ -30,7 +30,7 @@ function SheetJSXSpread() {
const [url, setUrl] = React.useState("https://sheetjs.com/pres.numbers");
const [done, setDone] = React.useState(false);
const ref = React.useRef(); // ref to DIV container
const set_url = React.useCallback((evt) => setUrl(evt.target.value));
const set_url = (evt) => setUrl(evt.target.value);
return ( <>
<div height={300} width={300} ref={ref}/>

@ -29,7 +29,7 @@ function SheetJSCDG() {
const [url, setUrl] = React.useState("https://sheetjs.com/pres.numbers");
const [done, setDone] = React.useState(false);
const ref = React.useRef(); // ref to DIV container
const set_url = React.useCallback((evt) => setUrl(evt.target.value));
const set_url = (evt) => setUrl(evt.target.value);
const [cdg, setCdg] = React.useState(null); // reference to grid object
return ( <>

@ -94,7 +94,7 @@ is strongly recommended to convert that array to a worksheet directly.
```jsx live
function SheetJStorage() {
const [url, setUrl] = React.useState("https://sheetjs.com/pres.numbers");
const set_url = React.useCallback((evt) => setUrl(evt.target.value));
const set_url = (evt) => setUrl(evt.target.value);
const [out, setOut] = React.useState("");
const xport = React.useCallback(async() => {
// get first worksheet data as array of objects

@ -119,7 +119,7 @@ function SheetJSQLWriter() {
})).filter(x => x).slice(0, 6);
}
const [url, setUrl] = React.useState("https://sheetjs.com/data/cd.xls");
const set_url = React.useCallback((evt) => setUrl(evt.target.value));
const set_url = (evt) => setUrl(evt.target.value);
const [out, setOut] = React.useState("");
const xport = React.useCallback(async() => {
const ab = await (await fetch(url)).arrayBuffer();

@ -1,37 +1,72 @@
---
title: NetSuite
sidebar_title: NetSuite
description: Automate the NetSuite ERP platform with SuiteScripts. Effortlessly read and write spreadsheets using SheetJS. Modernize Excel-powered business processes with confidence.
pagination_prev: demos/local/index
pagination_next: demos/extensions/index
---
# Spreadsheets in NetSuite SuiteScripts
import current from '/version.js';
import CodeBlock from '@theme/CodeBlock';
The [AMD script](/docs/getting-started/installation/amd) can be uploaded to the
file cabinet and referenced in the `define` call in SuiteScripts.
[NetSuite](https://www.netsuite.com/) is a suite of cloud-based software systems
for Enterprise Resource Planning (ERP). It has a robust scripting interface.[^1]
This demo discusses the key SheetJS operations. Familiarity with SuiteScript 2
is assumed. The following sections of the SuiteScript documentation should be
perused before reading this demo:
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
- [SuiteScript 2.x API Introduction](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/chapter_4387172221.html)
is an introduction that includes a simple example with deployment details,
- [SuiteScript 2.x Custom Modules](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/chapter_4704097697.html)
covers custom modules and adding third party scripts to modules.
- [`N/file` Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4205693274.html)
covers the `N/file` module. It is the main API for interacting with files.
This demo explores the SuiteScript scripting features in NetSuite. We'll explore
how to use SheetJS in SuiteScripts for reading and writing files in NetSuite.
The library plays nice with each script type, including RESTlets and Suitelets.
:::note
This demo was verified by NetSuite consultants in the following deployments:
| `@NScriptType` | `@NApiVersion` | Date |
|:----------------|:---------------|:-----------|
| ScheduledScript | 2.1 | 2023-03-09 |
| Restlet | 2.1 | 2023-04-20 |
| Suitelet | 2.1 | 2023-07-21 |
:::
## Installation
In SuiteScript parlance, third-party scripts are "Custom Modules"[^2].
The [SheetJS AMD script](/docs/getting-started/installation/amd) can be uploaded
to the file cabinet and referenced in the `define` call in SuiteScripts.
:::info pass
SheetJS scripts have been tested against the Rhino engine[^3] and work in both
SuiteScript 2.0 and SuiteScript 2.1.
:::
#### Adding SheetJS Scripts
<p><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>The
standalone script</a> should be downloaded and uploaded to the File Cabinet.</p>
SheetJS standalone script</a> should be uploaded to the File Cabinet.</p>
After uploading, create a JSON configuration file (or add the alias to an
existing config file). The reference points to the file and omits the `.js`.
:::note pass
```json
It is strongly recommended to keep the original filename `xlsx.full.min.js`.
:::
#### JSON Configuration
Assuming the uploaded file was named `xlsx.full.min.js`, the `paths` object in
the JSON configuration should reference `xlsx.full.min`. The reference can be
absolute or relative[^4].
For example, if the script `xlsx.full.min.js` was placed in the `SuiteScripts`
top-level directory, the config should use `"/SuiteScripts/xlsx.full.min"`:
```json title="JsLibraryConfig.json"
{
"paths": {
// highlight-next-line
@ -40,8 +75,22 @@ existing config file). The reference points to the file and omits the `.js`.
}
```
This config file should be referenced in SuiteScripts using `@NAmdConfig`. This
part is documented in ["Import a third-party JavaScript Library"](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4704111062.html#bridgehead_4738199877):
Relative references are also supported. If the entire project is stored in one
folder, the config can use `"./xlsx.full.min"`:
```json title="JsLibraryConfig.json"
{
"paths": {
// highlight-next-line
"xlsx": "./xlsx.full.min"
}
}
```
#### SuiteScript Usage
The JSON configuration file should be referenced in SuiteScripts using
`@NAmdConfig`. The path alias `"xlsx"` should be passed to `define`:
```js
/**
@ -56,28 +105,67 @@ define(['N/file', 'xlsx'], function(file, XLSX) {
});
```
## Reading Files
## Sheets in the File Cabinet
`N/file` provides [`file.load`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4226574300.html)
for pulling files:
The NetSuite File Cabinet[^5] is the primary feature for storing documents.
[`File#getContents`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4229269811.html)
returns the data as a Base64-encoded string which can be read with `XLSX.read`:
`N/file` is the primary module for interacting with the File Cabinet[^6].
This section assumes that `N/file` is bound to the variable `file`:
```js
/* load file */
var f = file.load({ id: id_of_file });
/* parse */
var workbook = XLSX.read(f.getContents(), {type: "base64"});
define(
['N/file', 'xlsx'],
function(
// highlight-next-line
file, // 'N/file'
XLSX // 'xlsx'
) {
// ...
}
);
```
## Writing Files
### Reading Files
`N/file` provides [`file.create`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4223861820.html)
and `file.load` for creating and loading files respectively.
There are three steps to reading files:
Binary content must be Base64-encoded. Fortunately, `XLSX.write` with `base64`
type will generate compatible Base64 strings:
1) Pull files from the file cabinet using `file.load`[^7]. The method returns a
`file.File` object which represents the file metadata.
2) Read raw data from the file using `File#getContents`[^8]. The method returns
the data as a Base64-encoded string.
3) Parse the data with the SheetJS `read` method[^9]. This method returns a
SheetJS workbook object.
`file.load` expects an `id` property, which can be be the internal ID (displayed
in the File Cabinet web interface) or an absolute or relative path string.
```js
/* file ID or path */
var id_of_file = 7262; // Internal ID 7262
/* load file */
var f = file.load({ id: id_of_file });
/* read file */
var b64 = f.getContents();
/* parse */
var workbook = XLSX.read(b64, { type: "base64" });
```
At this point, standard SheetJS utility functions[^10] can extract data from the
workbook object.
### Writing Files
There are three steps to writing files:
1) Write the data with the SheetJS `write` method[^11]. Using the `base64` output
type[^12], the method will return a Base64 string.
2) Create a new file using `file.create`[^13]. The recommended file type is
`file.Type.EXCEL`. The method returns a `file.File` object.
3) Upload data to the File Cabinet with `File#save`[^14]
```js
/* write XLSX workbook as Base64 string */
@ -91,3 +179,18 @@ var newfile = file.create({
/* save */
newfile.save();
```
[^1]: See ["SuiteScript 2.x API Introduction"](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/chapter_4387172221.html) in the NetSuite documentation.
[^2]: See ["SuiteScript 2.x Custom Modules"](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/chapter_4704097697.html) in the NetSuite documentation.
[^3]: See ["Java + Rhino" demo](/docs/demos/engines/rhino)
[^4]: See ["Module Dependency Paths"](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4430268304.html#Module-Dependency-Paths) in the NetSuite documentation.
[^5]: See ["File Cabinet Overview"](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/chapter_N541319.html) in the NetSuite documentation.
[^6]: See [`N/file` Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4205693274.html) in the NetSuite documentation.
[^7]: See [`file.load`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4226574300.html) in the NetSuite documentation.
[^8]: See [`File.getContents()`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4229269811.html) in the NetSuite documentation.
[^9]: See [`read` in "Reading Files"](/docs/api/parse-options)
[^10]: See ["Utility Functions"](/docs/api/utilities/)
[^11]: See [`write` in "Writing Files"](/docs/api/write-options)
[^12]: See ["Supported Output Formats"](/docs/api/write-options#supported-output-formats)
[^13]: See [`file.create`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4223861820.html) in the NetSuite documentation.
[^14]: See [`File.save()`](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4229271179.html) in the NetSuite documentation.

@ -115,7 +115,7 @@ This demo was tested in the following deployments:
| Architecture | Date |
|:-------------|:-----------|
| `darwin-x64` | 2023-07-05 |
| `darwin-x64` | 2023-07-19 |
| `darwin-arm` | 2023-07-05 |
| `linux-x64` | 2023-07-05 |
@ -129,10 +129,10 @@ cd sheetjs-boa
cargo run
```
2) Add the `boa` crate from the Git repository:
2) Add the `boa_engine` crate:
```bash
cargo add --git https://github.com/boa-dev/boa boa_engine
cargo add boa_engine
```
3) Download the [Standalone build](/docs/getting-started/installation/standalone):