问题
I'm looking to import rows from spreadsheets in a source folder that will be updated daily with new spreadsheets. Is it possible to pull a specific range from every new spreadsheet in the source folder into a Master Sheet?
I've been using the following code from an answer to a similar question here. This works perfectly except it duplicates the data that has already been imported to the Master Sheet. How can I fix this?
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);
};
};
回答1:
- You want to put the values of "Sheet1" of "Data File - TEST#" Spreadsheets to "Sheet1" of the "MASTER" Spreadsheet.
- You don't want to put the values of "Data File - TEST#", when the values of "Data File - TEST#" are existing in "MASTER".
If my understanding is correct, how about this modification? The flow of modified script is as follows. Please think of this as just one of several answers.
- Retrieve values from "Sheet1" of the "MASTER" Spreadsheet.
- Retrieve values from "Sheet1" of "Data File - TEST#" Spreadsheets.
- Compare both values. When there are NOT the same values of rows in both sheets, the values are put from "Data File - TEST#" to "MASTER".
Modified script:
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);
}
Note:
- In this sample script, your shared sample Spreadsheet of "MASTER", "Data File - TESTA", "Data File - TESTB" and "Data File - TESTC" were used. So if your actual situation uses the different names of Spreadsheet file names and sheet names, please modify them.
References:
- filter()
- some()
- every()
来源:https://stackoverflow.com/questions/56144667/google-sheets-import-folder-range-into-master-sheet