首页 > 解决方案 > SuiteScript - 打开、读取文件

问题描述

这将是一个非常愚蠢的问题 - 我如何在文件柜中打开一个文件并使用 SuiteScript 逐行读取它?我可以在网上找到的每一个例子似乎都是从中间开始的,认为我不具备的知识是理所当然的。

我没有在网上找到一个简单的例子吗?我只需要它:

标签: filenetsuitesuitescript

解决方案


在 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
    }

});

推荐阅读