我希望从源文件夹中的电子表格中导入行,该文件夹每天都会使用新的电子表格进行更新。是否可以将源文件夹中每个新电子表格中的特定范围拉入母版表?
我一直在使用下面的代码来回答类似的问题。除了复制已经导入到母版表中的数据之外,这非常完美。我该如何解决?
function getdata() {
//declare multiple variables in one statement - Initial value will
//be undefined
var destinationSpreadsheet,destinationSS_ID,destsheet,destrange,file,files,
sourcerange,sourceSS,sourcesheet,srcSheetName,sourcevalues;
srcSheetName = "Type the Name of Source Sheet Tab Here";
destinationSS_ID = "Type the Name of destination spreadsheet file ID Here";
files = DriveApp.getFolderById("Folder ID").getFiles();
destinationSpreadsheet = SpreadsheetApp.openById(destinationSS_ID);
destsheet = destinationSpreadsheet.getSheetByName('Sheet1');
while (files.hasNext()) {
file = files.next();
if (file.getMimeType() !== "application/vnd.google-apps.spreadsheet") {
continue;
};
sourceSS = SpreadsheetApp.openById(file.getId());
sourcesheet = sourceSS.getSheetByName(srcSheetName);
//sourcesheet.getRange(start row, start column, numRows, number of Columns)
sourcerange = sourcesheet.getRange(2,1,sourcesheet.getLastRow()-1,6);
sourcevalues = sourcerange.getValues();
//Write all the new data to the end of this data
destrange = destinationSpreadsheet.getSheetByName("Sheet1")
.getRange(destsheet.getLastRow()+1,1,sourcevalues.length,sourcevalues[0].length);
destrange.setValues(sourcevalues);
};
};
答案 0 :(得分:0)
如果我的理解是正确的,那么该修改如何?修改后的脚本流程如下。请认为这只是几个答案之一。
function getdata() {
var masterSpreadsheetId = "###"; // Spreadsheet ID of "MASTER" spreadsheet.
var folderId = "###"; // Folder ID including "Data File" files.
var dataFileSheetName = "Sheet1";
var files = DriveApp.getFolderById(folderId).getFiles();
var masterSpreadsheet = SpreadsheetApp.openById(masterSpreadsheetId);
var destSheet = masterSpreadsheet.getSheetByName('Sheet1');
var destSheetValues = destSheet.getRange(2, 1, destSheet.getLastRow() - 1, 6).getValues();
var values = [];
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() !== "application/vnd.google-apps.spreadsheet") continue;
var dataFileSS = SpreadsheetApp.openById(file.getId());
var dataFileSheet = dataFileSS.getSheetByName(dataFileSheetName);
var dataFileValues = dataFileSheet.getRange(2, 1, dataFileSheet.getLastRow()-1, 6).getValues();
Array.prototype.push.apply(values, dataFileValues);
};
var res = values.filter(function(e) {
return !destSheetValues.some(function(f) {
return e.every(function(g, k) {
var temp = f[k];
if (k < 2) {
g = new Date(g).getTime();
temp = new Date(temp).getTime();
}
return g == temp;
});
});
});
if (res.length > 0) destSheet.getRange(destSheet.getLastRow()+1,1,res.length,res[0].length).setValues(res);
}