将一堆单页文件编译为大型电子表格的最简单方法是什么



所以我的问题是,如果我在一个文件夹中有一堆单页谷歌工作表文件,那么在脚本方面,将所有这些单独的文件表复制到主编译电子表格中的最佳方法是什么?我现在正在按文件手动编写,但我相信有一种更快的方法可以使用类似[0][1]的循环。。。对于不同的文件名,但我不知道如何做到这一点。如有任何帮助,我们将不胜感激。

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); 
}

您可以使用以下代码:

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());
}
}

当然,您必须将顶部的变量设置为适当的值。此脚本将:

  • 搜索类型为SheetsPARENT_FOLDER_ID位于其父项中的文件
  • 对于找到的每个文件:
    • 将其第一张工作表复制到目标电子表格中

您还可以将可能需要的任何其他子句添加到搜索查询中。

最新更新