我正在尝试将多个源电子表格中的信息发布到Google表格中的目标电子表格中。当我用Logger.log()测试时,一切都应该正常工作,但是只有最后一个源电子表格的信息才会发布在目标电子表格上。我在做什么错了?
function retrieveData(ssA,ssB) {
/* Gets the source sheets' id values typed manually in a range of cells on active sheet*/
var ssA = SpreadsheetApp.getActiveSheet().getSheetValues(2, 3, 4,1);
/*Gets the source sheets'names typed manually in a range of cells on active sheet*/
var sourceSpreadsheetName = SpreadsheetApp.getActiveSheet().getSheetValues(2, 4, 4, 1);
/* loops through an array containing source sheets' ids*/
ssA.forEach(function(ssA){
var sss = SpreadsheetApp.openById(ssA)
/*loops through an array containing source sheets' names */
sourceSpreadsheetName.forEach(function(sourceSpreadsheetName){
var ss = sss.getSheetByName(sourceSpreadsheetName)
if(ss != null) {
/* gets range on each source sheet */
var SRange = ss.getRange(2, 1, 50, 2);
/*gets A1 notation on each source sheet*/
var A1Range = SRange.getA1Notation();
/*gets cell values on each source sheet */
var SData = SRange.getValues();
/* target sheet id */
var ssB = '1Dp1dOcwaup184mt1zWU-lcA5Nw-jFguWrr_H2hrtKSg';
var tss = SpreadsheetApp.openById(ssB);
var ts = tss.getSheetByName('Customers need to contact');
/* sets values retrieved from source sheets on target sheet*/
ts.getRange(A1Range).setValues(SData);
}
})
})
}
答案 0 :(得分:0)
function retrieveData(ssA,ssB) {
/* Gets the source sheets' id values typed manually in a range of cells on active sheet*/
var ssA = SpreadsheetApp.getActiveSheet().getSheetValues(2, 3, 4,1);
/*Gets the source sheets'names typed manually in a range of cells on active sheet*/
var sourceSpreadsheetName = SpreadsheetApp.getActiveSheet().getSheetValues(2, 4, 4, 1);
/* loops through an array containing source sheets' ids*/
ssA.forEach(function(ssA){
var sss = SpreadsheetApp.openById(ssA)
/*loops through an array containing source sheets' names */
sourceSpreadsheetName.forEach(function(sourceSpreadsheetName){
var ss = sss.getSheetByName(sourceSpreadsheetName)
if(ss != null) {
Logger.log('spreadsheet and sheet: '+sss.getName()+ss.getName());
/* gets range on each source sheet */
var SRange = ss.getRange(2, 1, 50, 2);
/*gets A1 notation on each source sheet*/
var A1Range = SRange.getA1Notation();
Logger.log('a1 notation: '+A1Range);
/*gets cell values on each source sheet */
var SData = SRange.getValues();
/* target sheet id */
var ssB = '1Dp1dOcwaup184mt1zWU-lcA5Nw-jFguWrr_H2hrtKSg';
var tss = SpreadsheetApp.openById(ssB);
var ts = tss.getSheetByName('Customers need to contact');
/* sets values retrieved from source sheets on target sheet*/
ts.getRange(A1Range).setValues(SData);
}
})
})
}
,您将看到从每个源工作表中检索范围A2:B51
,并将其粘贴到目标工作表的范围A2:B51
中。这意味着您将在每个迭代步骤中覆盖粘贴的值。
var lastRow=ts.getLastRow();
ts.getRange(lastRow,1,50,2).setValues(SData);