【问题标题】:SuiteScript - Open, Read FileSuiteScript - 打开、读取文件
【发布时间】:2021-04-19 17:16:41
【问题描述】:
这将是一个非常愚蠢的问题 - 我如何在文件柜中打开一个文件并使用 SuiteScript 逐行读取它?我在网上找到的每一个例子似乎都是从中间开始的,认为我不具备的知识是理所当然的。
在网上某个地方我没有找到一个简单的例子吗?我所需要的只是:
- 打开文件(给出文件名和文件夹)
- 阅读第一行
- 阅读第二行
- ....
- 关闭文件。
【问题讨论】:
标签:
file
netsuite
suitescript
【解决方案1】:
在 Suitescript 2.0 中使用 N/file 模块。该模块只能在服务器端(而不是客户端)脚本中使用。参考套件答案 ID:43524 用于 N/文件模块,套件答案 ID:43520 用于脚本类型。
require(['N/file', 'N/record'], function(file, record) {
//use file name and folder and 'N/search' module to find the file id if necessary
// load file
var myFile = file.load({
id: '__' //enter the file internal id, absolute or relative file path
})
//get the # of lines
var arrLines = myFile.getContents().split(/\n|\n\r/);
// loop through each line, skipping the header
for (var i = 1; i < arrLines.length - 1; i++) {
//split the 1 line of data into an array. If the file was a csv file, each array position holds a value from the columns of the 1 line of data
var content = arrLines[i].split(',');
// get values from the columns of a CSV file
var column1 = content[0]; //first column
var column2 = content[1]; //second column
//can use the column data above to i.e. create new record and set default value, update existing records, write the data elsewhere
//to check each line for a given value
arrLines[i].includes('keyword'); //returns true or false
}
});