google-apps-script - 工作表 - 脚本 - 使用脚本限制从 CSV 导入的行数?

标签 google-apps-script google-sheets

我有一个脚本可以从 G-Drive 上的某个位置导入 CSV - 我只需要它来导入前 300 行,而不是所有 2000 行。我错过了什么?

function importCSVFromGoogleDrive() {

  var fSource = DriveApp.getFolderById('xxxxxxxxxxxx'); // reports_folder_id = id of folder where csv is saved
  var file = DriveApp.getFilesByName("content.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

}

最佳答案

csv 文件的前 300 行

function importCSVFromGoogleDrive() {
  var folder = DriveApp.getFolderById('xxxxxxxxxxxx'); 
  var files = folder.getFilesByName("content.csv");
  var n=0;
  while(files.hasNext()) {
    var file=files.next();
    n++;
  }
  if(n==1) {
    var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()).slice(0,300);
    SpreadsheetApp.getActiveSheet().getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
  }else{
    SpreadsheetApp.getUi().alert("More Than One File with that name");
  }
}

Array.slice()

我用自己的 csv 文件进行了测试。

关于google-apps-script - 工作表 - 脚本 - 使用脚本限制从 CSV 导入的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285366/

相关文章:

firebase - 使用 Google Apps 脚本将 Google Sheets 数据保存到 Firebase 中

youtube - Youtube Content ID使用Apps脚本通过API获取所有权信息

ssl - 安全 JDBC 和 Google Apps 脚本

google-sheets - 如何将具有固定值的列添加到导入的数据Google电子表格

google-sheets - Google 表格 - 将一个值与逗号分隔列表中的每个值连接起来,并生成单个结果列表

javascript - https ://docs. google.com 请求失败返回代码 400。服务器响应被 chop :

javascript - 是否值得运行两个 While 循环来确定 For 循环的初始起点和迭代次数?

javascript - 根据 IF 语句测试范围值数组的最佳方法

javascript - Google Sheet AppScrip setFormula 语法错误 : SyntaxError: missing )

javascript - 使用 appendRow 错误地粘贴数组