Skip to main content

NetSuite

This demo discusses the key SheetJS operations. 本演示讨论了关键的SheetJS操作。Familiarity with SuiteScript 2 is assumed. 假设熟悉SuiteScript 2。The following sections of the SuiteScript documentation should be perused before reading this demo:在阅读本演示之前,应仔细阅读SuiteScript文档的以下部分:

  • SuiteScript 2.x API Introduction is an introduction that includes a simple example with deployment details,是一个介绍,包括一个简单的示例和部署详细信息,
  • SuiteScript 2.x Custom Modules covers custom modules and adding third party scripts to modules.介绍自定义模块和向模块添加第三方脚本。
  • N/file Module covers the N/file module. 模块覆盖了N/file模块。It is the main API for interacting with files.它是与文件交互的主要API。

The library plays nice with each script type, including RESTlets and Suitelets.该库可以很好地处理每种脚本类型,包括RESTlet和Suitelet。

Loading the SheetJS Standalone Script加载SheetJS独立脚本

This script plays nice with SuiteScript define. 与SuiteScript define配合得很好。It 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). 上传后,创建JSON配置文件(或将别名添加到现有配置文件)。The reference points to the file and omits the .js.引用指向该文件,省略了.js

{
"paths": {
"xlsx": "/SuiteScripts/xlsx.full.min"
}
}

This config file should be referenced in SuiteScripts using @NAmdConfig. 应在SuiteScripts中使用@NAmdConfig引用此配置文件。This part is documented in "Import a third-party JavaScript Library":这一部分记录在“导入第三方JavaScript库”中:

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

Reading Files读取文件

N/file provides file.load for pulling files:为拉取文件提供file.load

File#getContents returns the data as a Base64-encoded string which can be read with XLSX.read:以Base64编码字符串的形式返回数据,该字符串可以用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.提供file.createfile.load,分别用于创建和加载文件。

Binary content must be base64-encoded. 二进制内容必须是base64编码的。Fortunately, XLSX.write with base64 type will generate compatible Base64 strings:幸运的是,base64类型的XLSX.write将生成兼容的base64字符串:

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