docs.sheetjs.com/docz/docs/03-demos/30-cloud/02-netsuite.md
2024-01-03 01:47:00 -05:00

6.6 KiB

title sidebar_label description pagination_prev pagination_next
Spreadsheets in NetSuite SuiteScripts NetSuite Automate the NetSuite ERP platform with SuiteScripts. Effortlessly read and write spreadsheets using SheetJS. Modernize Excel-powered business processes with confidence. demos/local/index demos/extensions/index

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

NetSuite is a suite of cloud-based software systems for Enterprise Resource Planning (ERP). It has a robust scripting interface.1

SheetJS is a JavaScript library for reading and writing data from spreadsheets.

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.

:::note Tested Deployments

This demo was verified by NetSuite consultants in the following deployments:

@NScriptType @NApiVersion Date
ScheduledScript 2.1 2023-12-13
Restlet 2.1 2023-10-05
Suitelet 2.1 2023-12-22
MapReduceScript 2.1 2023-12-07

:::

Installation

In SuiteScript parlance, third-party scripts are "Custom Modules"2.

The SheetJS AMD script 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 JavaScript engine3 and work in both SuiteScript 2.0 and SuiteScript 2.1 deployments.

:::

Adding SheetJS Scripts

The SheetJS standalone script should be uploaded to the File Cabinet.

:::note pass

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 relative4.

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":

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

Relative references are also supported. If the entire project is stored in one folder, the config can use "./xlsx.full.min":

{
  "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:

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

Sheets in the File Cabinet

The NetSuite File Cabinet5 is the primary feature for storing documents.

N/file is the primary module for interacting with the File Cabinet6. This section assumes that N/file is bound to the variable file:

define(
  ['N/file', 'xlsx'],
  function(
    // highlight-next-line
    file, // 'N/file'
    XLSX  // 'xlsx'
  ) {
    // ...
  }
);

Reading Files

There are three steps to reading files:

  1. Pull files from the file cabinet using file.load7. The method returns a file.File object which represents the file metadata.

  2. Read raw data from the file using File#getContents8. The method returns the data as a Base64-encoded string.

  3. Parse the data with the SheetJS read method9. This method returns a SheetJS workbook object.

file.load expects an id property, which can be the internal ID (displayed in the File Cabinet web interface) or an absolute or relative path string.

/* 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 functions10 can extract data from the workbook object.

Writing Files

There are three steps to writing files:

  1. Write the data with the SheetJS write method11. Using the base64 output type12, the method will return a Base64 string.

  2. Create a new file using file.create13. The recommended file type is file.Type.EXCEL. The method returns a file.File object.

  3. Upload data to the File Cabinet with File#save14

/* 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();

  1. See "SuiteScript 2.x API Introduction" in the NetSuite documentation. ↩︎

  2. See "SuiteScript 2.x Custom Modules" in the NetSuite documentation. ↩︎

  3. See "Java + Rhino" demo ↩︎

  4. See "Module Dependency Paths" in the NetSuite documentation. ↩︎

  5. See "File Cabinet Overview" in the NetSuite documentation. ↩︎

  6. See N/file Module in the NetSuite documentation. ↩︎

  7. See file.load in the NetSuite documentation. ↩︎

  8. See File.getContents() in the NetSuite documentation. ↩︎

  9. See read in "Reading Files" ↩︎

  10. See "Utility Functions" ↩︎

  11. See write in "Writing Files" ↩︎

  12. See "Supported Output Formats" ↩︎

  13. See file.create in the NetSuite documentation. ↩︎

  14. See File.save() in the NetSuite documentation. ↩︎