file - SuiteScript - 打开、读取文件
问题描述
这将是一个非常愚蠢的问题 - 我如何在文件柜中打开一个文件并使用 SuiteScript 逐行读取它?我可以在网上找到的每一个例子似乎都是从中间开始的,认为我不具备的知识是理所当然的。
我没有在网上找到一个简单的例子吗?我只需要它:
- 打开文件(给出文件名和文件夹)
- 阅读第一行
- 阅读第二行
- ……
- 关闭文件。
解决方案
在 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
}
});
推荐阅读
- python - Python:如何滚动重新采样传入的数据流
- android - 我如何为 Kotlin 制作进度条对话框
- php - 使用“序列化”方法在 AJAX 中分别获取表单字段值
- java - java中如何使用ForkJoinPool来使用多核?
- android - 为什么我们在 ReactNative 中获得 ENOLOCAL
- c# - 是否可以创建一个可以从 EventHub 中的多个事件触发的编排持久功能?
- python - 用 Python 构造矩形
- postgresql - JSON 中的 Postgres 搜索
- arrays - 将新项目插入到 JSONB_SET 中嵌套数组的 json 对象中
- c# - 如何使用没有数据库的 C# 构建从 DataGridView 中删除行的方法