在云端硬盘中查找特定的Google文档

时间:2016-12-14 23:53:45

标签: file google-apps-script find drive

我正在尝试编写一个脚本来获取云端硬盘中特定但动态的Google文档的网址。 Google文档每周二创建,其日期位于Google文档标题的末尾。

“TypeError:无法在对象FileIterator中找到函数getUrl。”错误显示在第var agendaURL = file.getUrl();行。不知道如何调试它。

  var ssUrl = 'LINK BUFFERED';
  var sheetName = 'SHEET1';   // name of sheet to use
  var rangeName = 'C30';    // range of values to include 
  var dateRange = SpreadsheetApp.openByUrl(ssUrl)
                                .getSheetByName(sheetName)
                                .getRange(rangeName)
                                .getValues(); 


  // NEED TO find how to find file by name below!
  var file = DriveApp.getFilesByName("Weekly Agenda | " +dateRange);
  var agendaURL = file.getUrl();

2 个答案:

答案 0 :(得分:1)

这是因为您需要遍历满足搜索条件的所有文件。解决方案如下:https://developers.google.com/apps-script/reference/drive/drive-app

   var files = DriveApp.getFilesByName("Weekly Agenda | " +dateRange);
     while (files.hasNext()) {
       var file = files.next();
       var agendaURL = file.getUrl();  
       return agendaURL //for first one, or you can return each one, push to an array, etc...
    }

答案 1 :(得分:0)

DriveApp.getFilesByName()获取的值是FileIterator。因此,有必要从值中检索文件名,如下所示。

var filename = "Weekly Agenda | " + dateRange;
var file = DriveApp.getFilesByName(filename);
while (file.hasNext()) {
  var file = files.next();
  if (file.getName() == filename) {
    var agendaURL = file.getUrl();
  }
}