因此,我的问题是,如果我在一个文件夹中有一堆单一的谷歌工作表文件,那么从脚本的角度来看,将所有这些单独的文件复制到一个主编译电子表格中的最佳方式是什么?我现在正在手动编写每个文件,但我确信有一种使用循环的更快的方法,比如.不同的文件名,但我不知道怎么做。任何帮助都将不胜感激。
function ArrayBuilder() {
    var filesource = DriveApp.searchFiles('title contains "X" and parents in "File_ID"');
    while(filesource.hasNext()){
        var File = filesource.next();
        var ID = File.getId();
    }
    var name = File.getName()
    var source = SpreadsheetApp.openById(ID);
    var sheet = source.getSheets()[0];
    var destinationID = "File_ID";
    var destination = SpreadsheetApp.openById(destinationID);
     sheet.copyTo(destination).setName(name); 
    var filesource = DriveApp.searchFiles('title contains "Y" and parents in "File_ID"');
    while(filesource.hasNext()){
        var File = filesource.next();
        var ID = File.getId();
    }
    var name = File.getName()
    var source = SpreadsheetApp.openById(ID);
    var sheet = source.getSheets()[0];
    var destinationID = "File_ID";
    var destination = SpreadsheetApp.openById(destinationID);
     sheet.copyTo(destination).setName(name); 
    }发布于 2019-11-08 09:36:26
您可以使用以下代码:
var DESTINATION_SPREADSHEET_ID = '';
var PARENT_FOLDER_ID = '';
function buildMasterSpreadsheet() {
  var destinationSpreadsheet = SpreadsheetApp.openById(DESTINATION_SPREADSHEET_ID);
  var searchQuery = Utilities.formatString("'%s' in parents and mimeType = 'application/vnd.google-apps.spreadsheet'", PARENT_FOLDER_ID);
  var sourceFiles = DriveApp.searchFiles(searchQuery);
  while(sourceFiles.hasNext()){
    var file = sourceFiles.next();
    var sourceSheet = SpreadsheetApp.openById(file.getId()).getSheets()[0];
    sourceSheet.copyTo(destinationSpreadsheet).setName(file.getName());
  }
}当然,您必须将顶部的变量设置为适当的值。此脚本将:
Sheets且PARENT_FOLDER_ID位于父文件中的文件。找到的每个文件的Sheets中。
还可以将可能需要的任何其他子句添加到搜索查询中。
https://stackoverflow.com/questions/58754537
复制相似问题