已存在范围底部的 ImportRange
ImportRange at the bottom of an already present range
我正在做一个项目,我试图让一个 sheet 每天自动更新来自一个新的 sheet 的信息,以便形成每周的数据聚合。新的 sheet 将被放入同一个文件夹中,并且每天都会被赋予相同的名称。目前该过程有效,当我将新的 sheet 放入文件夹时,它会获取密钥 ID 并从所述 sheet.
导入新的唯一数据
我快完成了,我的最后一个问题是确保代码不会覆盖前一天的数据。我需要这样做,以便代码读取 sheet,查看活动范围(有多少行),然后将 importrange 函数放在 sheet 上最后一个条目正下方的单元格中(也就是说,如果前一天的数据在第 166 行结束,则今天的导入范围功能将继续 A:167).
每天完成此范围功能的最佳方法是什么?我知道执行它可能就像将 +1 添加到 importrange 的变量一样简单,但我怎样才能完成这个范围读取功能? 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();
}
如果文件夹中一次只有一个文件,则无需遍历 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();
}
我正在做一个项目,我试图让一个 sheet 每天自动更新来自一个新的 sheet 的信息,以便形成每周的数据聚合。新的 sheet 将被放入同一个文件夹中,并且每天都会被赋予相同的名称。目前该过程有效,当我将新的 sheet 放入文件夹时,它会获取密钥 ID 并从所述 sheet.
导入新的唯一数据我快完成了,我的最后一个问题是确保代码不会覆盖前一天的数据。我需要这样做,以便代码读取 sheet,查看活动范围(有多少行),然后将 importrange 函数放在 sheet 上最后一个条目正下方的单元格中(也就是说,如果前一天的数据在第 166 行结束,则今天的导入范围功能将继续 A:167).
每天完成此范围功能的最佳方法是什么?我知道执行它可能就像将 +1 添加到 importrange 的变量一样简单,但我怎样才能完成这个范围读取功能? 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();
}
如果文件夹中一次只有一个文件,则无需遍历 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();
}