简体   繁体   English

应用脚本新的 Google 表格创建问题

[英]App Script new Google Sheet creation issue

I am trying to write an App Script that takes string data from multiple different spreadsheets (completely separate documents) and puts them all in a new spreadsheet.我正在尝试编写一个应用程序脚本,该脚本从多个不同的电子表格(完全独立的文档)中获取字符串数据并将它们全部放入一个新的电子表格中。 When I run the logger, it shows me all the data I want.当我运行记录器时,它会显示我想要的所有数据。 I want each piece of data to show up in Column A, but when I run my script, it only puts 1 data point in the spreadsheet instead of all of them.我希望每条数据都显示在 A 列中,但是当我运行我的脚本时,它只在电子表格中放置 1 个数据点,而不是全部。 Can someone give me some guidance?有人可以给我一些指导吗? Here is my code:这是我的代码:

function pullTogether() {
  var files = DriveApp.getFolderById('Folder ID').searchFiles('title != "nothing"');
  const rangeName = 'Sheet1!B2:C';
  while(files.hasNext()){
    var xFile = files.next();
    var name = xFile.getId();
    
    const values = Sheets.Spreadsheets.Values.get(name, rangeName).values;
    for (const row in values) {
      var a1 = (values[row][0]);
      Logger.log(a1);
    
  var ss = SpreadsheetApp.openById("ID of new spreadsheet"); //I have the real ID in my code
  var cell = ss.getRange("A2");
  cell.setValue(a1);
     
}
}

}

I believe your goal is as follows.我相信你的目标如下。

  • You want to retrieve the values from the column "B" of each Spreadsheet under the specific folder.您想从特定文件夹下每个电子表格的“B”列中检索值。
  • You want to put the retrieved values to the column "A" of the destination sheet.您想将检索到的值放入目标工作表的“A”列。

Modification points:修改点:

  • About but when I run my script, it only puts 1 data point in the spreadsheet instead of all of them.关于but when I run my script, it only puts 1 data point in the spreadsheet instead of all of them. , when I saw your script, the retrieved value is always put to the cell "A2" of the destination sheet. ,当我看到你的脚本时,检索到的值总是放在目标工作表的单元格“A2”中。 I think that this might be the reason for your issue.我认为这可能是您的问题的原因。

  • In your script, I thought that when the following flow is used, the process cost will become low.在您的脚本中,我认为当使用以下流程时,流程成本会变低。 By this flow, your issue can be also removed.通过此流程,您的问题也可以解决。

  • In your situation, even when Sheets API is not used, the script might work using getValues() .在您的情况下,即使不使用 Sheets API,脚本也可能使用getValues()工作。

When these points are reflected in your script, it becomes as follows.当这些点反映在你的脚本中时,它变成如下。

Modified script:修改后的脚本:

Please set the folder ID and the destination Spreadsheet ID.请设置文件夹 ID 和目标电子表格 ID。

function pullTogether() {
  // Retrieve values from each Spreadsheet.
  var values = [];
  var files = DriveApp.getFolderById('Folder ID').searchFiles(`title != 'nothing' and mimeType='${MimeType.GOOGLE_SHEETS}'`);
  var sheetName = 'Sheet1'
  while (files.hasNext()) {
    var xFile = files.next();
    var sheet = SpreadsheetApp.open(xFile).getSheetByName(sheetName);
    if (sheet) {
      var v = sheet.getRange("B2:B" + sheet.getLastRow()).getValues();
      values = [...values, ...v];
    }
  }

  // Put values to the destination sheet.
  var ss = SpreadsheetApp.openById("ID of new spreadsheet"); //I have the real ID in my code
  var dstSheet = ss.getSheets()[0];
  dstSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
}

Note:笔记:

  • Although I'm not sure about your actual situation, when the above script didn't work by the large data, please modify as follows.虽然我不确定你的实际情况,但是当上面的脚本在大数据下不起作用时,请进行如下修改。

    • From

       dstSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
    • To

       Sheets.Spreadsheets.Values.update({ values }, ss.getId(), `'${dstSheet.getSheetName()}'!A2`, { valueInputOption: "USER_ENTERED" });

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM