尝试将变量放入字符串中,以便函数导入一个键,然后在 sheet 中执行
Trying to put a variable into string so that the function imports a key, and then executes in a sheet
我正在做一个项目,试图让 sheet 每天自动更新来自新 sheet 的信息。新的 sheet 将被放入同一个文件夹中,并且每天都会被赋予相同的名称。但是,我需要每天获取新的 sheet 密钥,以使用新的 sheet 导入数据来制作代码 运行。
我快完成了,现在我只需要在第 37 行将被推送到变量 key 的字符串输入到 importrange 函数中。棘手的部分是代码将它导入目标 sheet 作为一个字符串,然后它在遇到 sheet.
时执行实际的导入范围函数
我需要一种方法将变量键放入该字符串中,这样它仍将在 sheet 中执行,或者转储变量在某处保存的 ID,然后自动将其放入该字符串中。非常感谢!
var counter = 0;
var files = [];
var key = (" ");
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()) files.push(filesN.next().getName());
while (filesN.hasNext())
keyID.push(filesN.next().getId());
}
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
range.activate; // activates range
//HELP HERE PLEASE
range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","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();
您是否点击了#REF?因为通常使用 IMPORTRANGE 函数,您必须 "Allow Access" 才能提取数据。在您执行此操作之前,它将显示 #REF。如果单击该单元格,弹出窗口应显示一个标题为 "Allow Access".
的按钮
一旦我在测试您的原始命令时执行了此操作,range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","sheet1!A1:167")')
就起作用了。
同样要使用变量,您的命令将如下所示...
range.setValue(('=IMPORTRANGE("'+key+'","sheet1!A1:167")'))
根据评论更新
改变
var files = [];
var key = ("");
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()) files.push(filesN.next().getName());
while (filesN.hasNext())
keyID.push(filesN.next().getId());
到...
var files = [];
var key = [];
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();
key.push(file.getId())
files.push(file.getName());
}
Logger.log(key)
这将创建一个文件键数组和一个名称数组...然后您可以遍历键数组以单独获取每个键并在命令中使用键变量,如前所示。我不太确定你到底想做什么,所以更具体的帮助需要更多信息。
2016 年 8 月 14 日更新
这就是我关于遍历键数组的意思。我已经测试过并且有效。
function test() {
var files = [];
var keys = [];
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();
keys.push(file.getId())
files.push(file.getName());
}
Logger.log(keys)
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 < keys.length; i++) {
var range = s.getRange(1,i+1); //sets range in target. ONLY CHOOSE ONE
range.setValue('=IMPORTRANGE("'+keys[i]+'","sheet1!A1:167")')
}
autoUpdate(keys);
}
我正在做一个项目,试图让 sheet 每天自动更新来自新 sheet 的信息。新的 sheet 将被放入同一个文件夹中,并且每天都会被赋予相同的名称。但是,我需要每天获取新的 sheet 密钥,以使用新的 sheet 导入数据来制作代码 运行。
我快完成了,现在我只需要在第 37 行将被推送到变量 key 的字符串输入到 importrange 函数中。棘手的部分是代码将它导入目标 sheet 作为一个字符串,然后它在遇到 sheet.
时执行实际的导入范围函数我需要一种方法将变量键放入该字符串中,这样它仍将在 sheet 中执行,或者转储变量在某处保存的 ID,然后自动将其放入该字符串中。非常感谢!
var counter = 0;
var files = [];
var key = (" ");
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()) files.push(filesN.next().getName());
while (filesN.hasNext())
keyID.push(filesN.next().getId());
}
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
range.activate; // activates range
//HELP HERE PLEASE
range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","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();
您是否点击了#REF?因为通常使用 IMPORTRANGE 函数,您必须 "Allow Access" 才能提取数据。在您执行此操作之前,它将显示 #REF。如果单击该单元格,弹出窗口应显示一个标题为 "Allow Access".
的按钮一旦我在测试您的原始命令时执行了此操作,range.setValue('=IMPORTRANGE("1hVv6ldHEaCCI_uptr0MpzAyP60x7on8YR_brWwWXTWo","sheet1!A1:167")')
就起作用了。
同样要使用变量,您的命令将如下所示...
range.setValue(('=IMPORTRANGE("'+key+'","sheet1!A1:167")'))
根据评论更新
改变
var files = [];
var key = ("");
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()) files.push(filesN.next().getName());
while (filesN.hasNext())
keyID.push(filesN.next().getId());
到...
var files = [];
var key = [];
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();
key.push(file.getId())
files.push(file.getName());
}
Logger.log(key)
这将创建一个文件键数组和一个名称数组...然后您可以遍历键数组以单独获取每个键并在命令中使用键变量,如前所示。我不太确定你到底想做什么,所以更具体的帮助需要更多信息。
2016 年 8 月 14 日更新
这就是我关于遍历键数组的意思。我已经测试过并且有效。
function test() {
var files = [];
var keys = [];
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();
keys.push(file.getId())
files.push(file.getName());
}
Logger.log(keys)
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 < keys.length; i++) {
var range = s.getRange(1,i+1); //sets range in target. ONLY CHOOSE ONE
range.setValue('=IMPORTRANGE("'+keys[i]+'","sheet1!A1:167")')
}
autoUpdate(keys);
}