简体   繁体   English

如果文件夹名称匹配,则在 Google 表格中获取 Google 云端硬盘链接

[英]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.

相关问题 当文件夹名称在 Google 表格中匹配时,在 Google 表格中获取 Google 云端硬盘链接 - Get Google Drive link in Google Sheets when the name of the folder matches in Google Sheets 如何根据Google表格中的单元格值命名Google云端硬盘文件夹 - How to name a Google Drive folder based on cell value in Google Sheets 从 Google Sheets 的内容链接到 Google Drive - Link to Google Drive from Content of Google Sheets 如何通过 Google Apps 脚本获取 Google Drive 文件的“可共享链接”并将链接粘贴到 Google 表格中的单元格中? - How to get the “shareable link” of a Google Drive file via Google Apps Script and paste the link into a cell in Google Sheets? 使用 Google Apps 脚本从 google drive 文件夹中获取 Google 表格中列出的文件夹和文件 - Get folders and files listed in Google Sheets from a google drive folder using a Google Apps script Google 工作表将工作表复制到驱动器文件夹 - Google sheets make a copy of a worksheet to drive folder Sheets中的Google Script App列表驱动器文件夹 - Google Script App list drive folder in Sheets 如何从Google云端硬盘中文件所在的位置获取文件夹以显示在Google表格中 - How do you get the folder from where the file is from in Google Drive to display in Google Sheets 文件夹getParents无法在Google Script中获取Team Drive名称 - Folder getParents fails to get Team Drive name in Google Script 将条件格式应用于驱动器中谷歌工作表文件夹中的所有工作表 - apply conditional formattiong to all sheets in a folder in google sheets in a drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM