我正在尝试在 12 小时计时器触发器上创建一个基本脚本,该脚本通过其 ICAL URL 循环遍历我的每个 Google 日历,并为我的 Google Drive 上的文件夹下载 ICAL(用于备份目的)。它抛出这个错误
"No item with the given ID could be found, or you do not have permission to access it. (line 23, file "Code")" (Line #23 is var folder... )
运行脚本确实会在第一次运行循环时下载并保存 ICAL 文件(如果我手动传入每个唯一的 ICAL URL,一次一个),但错误会终止循环。鉴于我已经获得访问权限并且是这里所有内容的所有者,我不确定我还需要什么。
var calendarsToSave = [
"https://calendar.google.com/calendar/ical/inXXXXXXX.com/privateXXXX/basic.ics",
"https://calendar.google.com/calendar/ical/XXXXX.com_XXXXXXup.calendar.google.com/private-XXXXXXX/basic.ics"
];
var folder = '123xxxxxxxxv789'; // my gdrive folder
function downloadFile(calendarURL,folder) {
var fileName = "";
var fileSize = 0;
for (var i = 0; i < calendarsToSave.length; i++) {
var calendarURL = calendarsToSave[i];
var response = UrlFetchApp.fetch(calendarURL, {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob()
var folder = DriveApp.getFolderById(folder); // << returns a permissions error thus terminating the for loop
var file = folder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
return fileInfo;
} // end for loop
}
最佳答案
更新:您还从参数中重新初始化了一个已经存在的变量,并将其作为全局变量,这样我们就可以删除参数,如果你想保留全局变量。
我们还可以移动您获取 Google 文件夹对象的位置。它每次都保持不变,因此我们不需要再次检索它。
var calendarsToSave = [
"https://calendar.google.com/calendar/ical/inXXXXXXX.com/privateXXXX/basic.ics",
"https://calendar.google.com/calendar/ical/XXXXX.com_XXXXXXup.calendar.google.com/private-XXXXXXX/basic.ics"
];
var folder = '123xxxxxxxxv789'; // my gdrive folder
function downloadFile(calendarURL) {
var fileName = "";
var fileSize = 0;
var gfolder = DriveApp.getFolderById(folder);
for (var i = 0; i < calendarsToSave.length; i++) {
var calendarURL = calendarsToSave[i];
var response = UrlFetchApp.fetch(calendarURL, {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob()
var file = gfolder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
return fileInfo;
} // end for loop
}
让我们看看它能把我们带到哪里。
关于javascript - 即使我是所有者并获得许可,Google Drive + Script 也会抛出权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48936453/