简体   繁体   English

Google Apps脚本可遍历电子表格ID列表,并将范围复制到主文件

[英]Google Apps Script to Iterate through a list of Spreadsheet ID's and copy range to a master file

I have a folder in my drive where I create a folder (based on a template structure) for each new client. 我的驱动器中有一个文件夹,在其中为每个新客户端创建一个文件夹(基于模板结构)。 I need to extract information from one file in particular from each of their folders. 我需要从一个文件中提取信息,尤其是从每个文件夹中提取信息。 I have the Spreadsheet IDs in a list, and I know the sheet name, I am just not sure how to create a loop to go through each file and append it to a master file. 我在列表中有电子表格ID,而且我知道工作表名称,但我不确定如何创建循环遍历每个文件并将其附加到主文件中。

I found a script recently here which would do the trick, but it assumes that all the spreadsheets are in one folder: 我最近在这里找到了一个可以解决问题的脚本,但它假定所有电子表格都在一个文件夹中:

function getDataToMaster2() {
    var comp = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("System_Control").getRange("d7").getValue();
  var folder = DriveApp.getFolderById(comp); //Define id of folder
  var contents = folder.getFiles();
  var file; 
  var data;
  var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form_Responses"); //first sheet of the file, change by getSheetByName("NAME") if you want
  while(contents.hasNext()){  
    file = contents.next();
    if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
      var sheet = SpreadsheetApp.openById(file.getId()).getSheetByName("Form_Responses");//first sheet of the file, change by getSheetByName("NAME") if you want
      var startRow = 2; 
      var data = sheet.getDataRange().getValues();
      var colToCheck = 14;
      for(var j = 1; j < data.length;j++){
        if(data[j][colToCheck-1] != "copied"){
          sheetMaster.appendRow(data[j]);
          sheet.getRange((j+1), colToCheck).setValue("copied");
          SpreadsheetApp.flush();
        }
      }
    } 
  }
}

Here is a function that loops over idList, an array holding the Id of each spreadsheet to be copied. 这是一个循环遍历idList的函数,该数组包含要复制的每个电子表格的ID。 It grabs all data from "Sheet1" in each of those, and copies to sheet "Master" in the active spreadsheet. 它从每个“ Sheet1”中获取所有数据,并复制到活动电子表格中的“ Master”表中。

The data copying happens at once, without looping over rows as in your example; 数据复制立即发生,而不会像您的示例中那样遍历行。 it is more efficient to move larger chunks at once. 一次移动较大的块会更有效。

function getDataToMaster2() {
  var idList = ['id1', 'id2', 'id3'];
  var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
  for (var i = 0; i < idList.length; i++) {  
    var sheet = SpreadsheetApp.openById(idList[i]).getSheetByName("Sheet1"); 
    var data = sheet.getDataRange().getValues();
    var lastRow = sheetMaster.getLastRow();
    sheetMaster.insertRowsAfter(lastRow, data.length);
    sheetMaster.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);
  }
}

If the ids are stored, say, in column B of sheet "Ids", then the beginning of function would be different: 如果将id存储在表“ Ids”的B列中,则函数的开头将不同:

  var sheetIds = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ids");
  var values = sheetIds.getRange('B:B').getValues();
  var idList = values.map(function (row) {
    return row[0];
  }).filter(function (id) {
    return id;
  });

and then proceed for the for loop as above. 然后继续进行上述for循环。

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

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