docs.sheetjs.com/docz/docs/03-demos/09-cloud/04-netsuite.md
2023-05-02 23:40:40 -04:00

3.1 KiB

title pagination_prev pagination_next
NetSuite demos/local/index demos/extensions/index

import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';

The AMD script can be uploaded to the file cabinet and referenced in the define call in SuiteScripts.

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:

The library plays nice with each script type, including RESTlets and Suitelets.

Installation

The standalone script should be downloaded and uploaded to the File Cabinet.

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.

{
  "paths": {
    // highlight-next-line
    "xlsx": "/SuiteScripts/xlsx.full.min"
  }
}

This config file should be referenced in SuiteScripts using @NAmdConfig. This part is documented in "Import a third-party JavaScript Library":

/**
* @NApiVersion 2.x
// highlight-next-line
* @NAmdConfig  ./JsLibraryConfig.json
* ... more options ...
*/
// highlight-next-line
define(['N/file', 'xlsx'], function(file, XLSX) {
  // ... use XLSX here ...
});

Reading Files

N/file provides file.load for pulling files:

File#getContents returns the data as a Base64-encoded string which can be read with XLSX.read:

/* load file */
var f = file.load({ id: id_of_file });
/* parse */
var workbook = XLSX.read(f.getContents(), {type: "base64"});

Writing Files

N/file provides file.create and file.load for creating and loading files respectively.

Binary content must be Base64-encoded. Fortunately, XLSX.write with base64 type will generate compatible Base64 strings:

/* write XLSX workbook as Base64 string */
var out = XLSX.write(workbook, { bookType: "xlsx", type: "base64" });
/* create file */
var newfile = file.create({
  name: 'test.xlsx', // replace with desired name
  fileType: file.Type.EXCEL,
  contents: out
});
/* save */
newfile.save();