gpt4 book ai didi

javascript - 尝试将变量放入字符串中,以便函数导入一个键,然后在工作表中执行

转载 作者:行者123 更新时间:2023-12-03 06:26:15 24 4
gpt4 key购买 nike

我正在开发一个项目,我试图让一张工作表每天自动更新新工作表中的信息。新工作表将被放入同一文件夹中,并且每天都会被赋予相同的名称。但是,我需要每天获取新的工作表 key ,以使代码在新工作表导入数据的情况下运行。

我快完成了,现在我只需要将被推送到变量 key 的字符串获取到第 37 行的 importrange 函数中。棘手的部分是代码将其作为字符串导入到目标表中,其中然后,当它到达工作表时,它会执行实际的导入范围函数。

我需要一种方法将变量键放入该字符串中,以便它仍然会在工作表中执行,或者转储变量在某处保存的 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 函数,您必须“允许访问”才能提取数据。在您执行此操作之前,它将显示#REF。如果您单击该单元格,则会显示一个弹出窗口,其中包含标题为“允许访问”的按钮。

当我在测试中执行此操作后,您的原始命令 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);
}

关于javascript - 尝试将变量放入字符串中,以便函数导入一个键,然后在工作表中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38643458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com