[英]Get Google Drive Link in Google Sheets if the folder name matches
I'm trying to find a script that can retrieve the folder link (of Google Drive) in Google Sheets when the folder name matches.我正在尝试找到一个脚本,该脚本可以在文件夹名称匹配时检索 Google 表格中的文件夹链接(Google 云端硬盘)。 Below is the code which I used but it is only showing the first 2 drive links and after that, it is showing the error "Cannot retrieve the next object: iterator has reached the end"下面是我使用的代码,但它只显示前 2 个驱动器链接,之后显示错误“无法检索下一个 object:迭代器已到达结尾”
function onOpen() {
var sheet = SpreadsheetApp.getActive();
var menu = [{name: "Get Folder Links", functionName: "getFolderLinks"}];
sheet.addMenu("Custom Menu", menu);
}
function getFolderLinks() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3;
var data = sheet.getRange(startRow, 7, sheet.getLastRow() - startRow + 1, 1).getValues();
for (var i = 0; i < data.length; i++) {
var folderName = data[i][0];
var folder = DriveApp.getFoldersByName(folderName).next();
var link = folder.getUrl();
sheet.getRange(i+startRow, 15).setValue(link);
logger.log(query);
}
}
Folder Names are in Column G3:G and the link to the folder will be shown in O3:O.文件夹名称在 G3:G 列中,文件夹的链接将显示在 O3:O 中。
Need advice from community experts on the above code.需要社区专家对上述代码的建议。
PS: Can the code work on a real-time basis (without clicking on the button which is created by the above code) and without any limitations? PS:代码可以实时工作(无需单击由上述代码创建的按钮)并且没有任何限制吗? Thank you in advance.先感谢您。
Cannot retrieve the next object: iterator has reached the end无法检索下一个 object:迭代器已到达末尾
This error means the folder does not exist when you call next()
.此错误意味着当您调用next()
时该文件夹不存在。
I've modified your script to check if a folder exists:我修改了您的脚本以检查文件夹是否存在:
function getFolderLinks() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3;
var data = sheet.getRange(startRow, 7, sheet.getLastRow() - startRow + 1, 1).getValues();
for (var i = 0; i < data.length; i++) {
var folderName = data[i][0];
Logger.log(folderName);
var folderIterator = DriveApp.getFoldersByName(folderName);
if(folderIterator.hasNext()) //check if the folder exists
{
var link = folderIterator.next().getUrl();
sheet.getRange(i+startRow, 15).setValue(link);
} else
{
Logger.log("Folder not exist: " + folderName);
}
}
}
Can the code work on a real-time basis (without clicking on the button which is created by the above code) and without any limitations代码是否可以实时工作(无需单击由上述代码创建的按钮)并且没有任何限制
To achieve this, you can schedule a Time-Driven trigger shown in the below picture:为此,您可以安排下图所示的时间驱动触发器:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.