我正在开展一个项目,我正在尝试每天使用新工作表中的信息进行自动更新,以形成每周的数据汇总。新工作表将被删除在同一文件夹中,并且每天都会被赋予相同的名称。目前该流程有效,当我将新工作表放入该文件夹时,它获取密钥ID并从该工作表中导入新的唯一数据。
我差不多完成了,我的最后一个问题就是让代码不会覆盖前一天的数据。我需要这样做,以便代码读取工作表,查看活动范围(有多少行),然后将importrange函数放在工作表中最后一个条目正下方的单元格中(即前一天的数据是否结束)第166行,今天的导入范围函数为A:167)。
每天完成此范围功能的最佳方法是什么?我知道执行它可能就像为importrange的变量添加+1一样简单,但我怎样才能完成这个范围读取功能? getRange会不会有这个伎俩,还是有更具体的与行号有关的东西?
以下是代码:
//trigger from user action upload new sheet
// get sheet key from new sheet
//specify new cell in monthly for import range
// put new key in import range function in sheet
//execute import range
//var counter = 0;
//var import= "IMPORTRANGE""(""1K7Rj4QK-EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc""," "sheet1!A1:G6"")"
var counter = 0;
function test() {
var files = [];
var keyID = [];
function searchFolder() {
var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
// Log the name of every file in the folder.
var filesN = DriveApp.getFolderById(folderId).getFiles(); //log files in folder
while (filesN.hasNext()) {
var file = filesN.next();
keyID.push(file.getId())
files.push(file.getName());
}
Logger.log(keyID)
var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); //defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as active
var s = ss.getSheetByName('Sheet1');
for (var i = 0; i < keyID.length; i++) {
var range = s.getRange(1,i+1); //sets range in target. ONLY CHOOSE ONE
range.setValue('=IMPORTRANGE("'+keyID[i]+'","sheet1!A1:167")')
}
autoUpdate(keyID);
}
function autoUpdate(){ //updates monthly from newly imported daily
if (counter == 1){ //counter is made to be 1 when day is uploaded to monthly
var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); //defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as active
var range= ss.getRange("A1:A1"); //sets range in target. ONLY CHOOSE ONE CELL FOR IMPORTRANGE- IF MORE THAN 1 IS CHOSEN YOU WILL GET A #REF ERROR BECAUSE IT WILL PUT IMPORTRANGE IN ALL CELLS
range.activate; // activates range
//range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","sheet1!A1:167")');
range.setValue(('=IMPORTRANGE("'+keyID+'","sheet1!A1:167")'));//Puts in IMPORTRANGE into target as a STRING value (just words). Once it hits the sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet, range is selected to import to target (ie A1:G6)
counter=(counter-1)
}
}
function timeStamp(){
if (files == "Daily") {
counter= (counter+1)
}
}
searchFolder();
timeStamp();
autoUpdate();
}
答案 0 :(得分:0)
如果您不需要循环访问keyID数组,文件夹中只有一个文件...您只需按keyID [0]获取第一个(也是唯一的元素) ] ...要获取导入范围的下一行,您可以使用s.getLastRow()
。有关文档,请参阅here。您的完整代码将更改为以下内容。注意第二个循环所在的变化。
//trigger from user action upload new sheet
// get sheet key from new sheet
//specify new cell in monthly for import range
// put new key in import range function in sheet
//execute import range
//var counter = 0;
//var import= "IMPORTRANGE""(""1K7Rj4QK-EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc""," "sheet1!A1:G6"")"
var counter = 0;
function test() {
var files = [];
var keyID = [];
function searchFolder() {
var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
// Log the name of every file in the folder.
var filesN = DriveApp.getFolderById(folderId).getFiles(); //log files in folder
while (filesN.hasNext()) {
var file = filesN.next();
keyID.push(file.getId())
files.push(file.getName());
}
Logger.log(keyID)
var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); //defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as active
var s = ss.getSheetByName('Sheet1');
s.getRange(s.getLastRow()+1,1).setValue('=IMPORTRANGE("'+keyID[0]+'","sheet1!A1:167")')
autoUpdate(keyID);
}
function autoUpdate(){ //updates monthly from newly imported daily
if (counter == 1){ //counter is made to be 1 when day is uploaded to monthly
var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); //defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as active
var range= ss.getRange("A1:A1"); //sets range in target. ONLY CHOOSE ONE CELL FOR IMPORTRANGE- IF MORE THAN 1 IS CHOSEN YOU WILL GET A #REF ERROR BECAUSE IT WILL PUT IMPORTRANGE IN ALL CELLS
range.activate; // activates range
//range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","sheet1!A1:167")');
range.setValue(('=IMPORTRANGE("'+keyID+'","sheet1!A1:167")'));//Puts in IMPORTRANGE into target as a STRING value (just words). Once it hits the sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet, range is selected to import to target (ie A1:G6)
counter=(counter-1)
}
}
function timeStamp(){
if (files == "Daily") {
counter= (counter+1)
}
}
searchFolder();
timeStamp();
autoUpdate();
}