【问题标题】:Google code does not work谷歌代码不起作用
【发布时间】:2016-02-18 10:13:09
【问题描述】:

我是 Google Apps 脚本编程的新手。最近,我尝试关注this tutorial by Google,但看起来他们的代码已经过时了。但是,我仍然想做教程所做的事情,只是它现在将在 Google Drive 中搜索文件。

以下是我的代码:

function onOpen() {
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var searchMenuEntries = [
        {
            name: "Search in all files",
            functionName: "search"
        }
    ];
    spreadsheet.addMenu("Search Document", searchMenuEntries);
}

function search() {
    // Prompt the user for a search term
    var searchTerm = Browser.inputBox("Enter the string to search for:");

    // Get the active sheet
    var sheet = SpreadsheetApp.getActiveSheet();

    // Set up the spreadsheet to display the results
    var headers = [
        ["File Name",
         "File Type",
         "URL"
        ]
    ];
    sheet.clear();
    sheet.getRange("A1:C1").setValues(headers);

    // Search the files in the user's Drive for the search term
    var files = DriveApp.getFilesByName(searchTerm);

    // Loop through the results and display the file name, file type, and URL
    var i = 0;
    while (files.hasNext()) {
        files = files.next();
        sheet.getRange(i+2, 1, 1, 1).setValue(files.getName());
        sheet.getRange(i+2, 2, 1, 1).setValue(files.getMimeType());
        sheet.getRange(i+2, 3, 1, 1).setValue(files.getDownloadUrl());
        i++;
    }
}

当我尝试运行代码时,没有出现任何给我警告的重大错误。但是,代码不起作用,Google Sheets 中的输出如下:

+---------------+----------------+----------------+
| File name     | File type      | URL            |
+---------------+----------------+----------------+
|               |                |                |
+---------------+----------------+----------------+

即使我很确定我的云端硬盘中有文件匹配项。我试图调试代码,但 while 循环似乎没有执行。任何帮助将不胜感激!

【问题讨论】:

    标签: google-apps-script google-sheets google-apps


    【解决方案1】:

    此代码中有一些拼写错误,我不知道您在哪里/如何复制它...这是工作代码(请参阅代码中的 cmets):

    function onOpen() {
        var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
        var searchMenuEntries = [
            {
                name: "Search in all files",
                functionName: "search"
            }
        ];
        spreadsheet.addMenu("Search Document", searchMenuEntries);
    }
    
    function search() {
        // Prompt the user for a search term
        var searchTerm = Browser.inputBox("Enter the string to search for:");
    
        // Get the active sheet
        var sheet = SpreadsheetApp.getActiveSheet();
    
        // Set up the spreadsheet to display the results
        var headers = [
            ["File Name",
             "File Type",
             "URL"
            ]
        ];
        sheet.clear();
        sheet.getRange("A1:C1").setValues(headers);
    
        // Search the files in the user's Drive for the search term
        var files = DriveApp.getFilesByName(searchTerm);
    
        // Loop through the results and display the file name, file type, and URL
        var i = 0;
        while (files.hasNext()) { // files is the iterator
            var file = files.next();// file is the drive document object
            sheet.getRange(i+2, 1, 1, 1).setValue(file.getName());// file is the drive document object
            sheet.getRange(i+2, 2, 1, 1).setValue(file.getMimeType());// file is the drive document object
            sheet.getRange(i+2, 3, 1, 1).setValue(file.getDownloadUrl());// file is the drive document object
            i++;
        }
    }
    

    编辑:

    顺便说一句,这个脚本实现得很糟糕,它在循环中使用 API 调用,效率非常低... 下面是一个更快的版本(也更紧凑)

    function search() {
      // Prompt the user for a search term
      var searchTerm = Browser.inputBox("Enter the string to search for:");
      
      // Get the active sheet
      var sheet = SpreadsheetApp.getActiveSheet();
      
      // Set up the spreadsheet to display the results
      var result = [];
      result.push(["File Name","File Type","URL"])
     // Search the files in the user's Drive for the search term
      var files = DriveApp.getFilesByName(searchTerm);
      
      // Loop through the results and display the file name, file type, and URL
      while (files.hasNext()) {
        var file = files.next();
        result.push([file.getName(),file.getMimeType(),file.getDownloadUrl()]);
      }
    sheet.getRange(1,1,result.length,result[0].length).setValues(result);
    }
    

    注意:

    要更改搜索条件,只需使用 DriveApp.search 调用并添加参数as specified in the documentation.

    查看文件内容的示例:

      var files = DriveApp.searchFiles('fullText contains "'+searchTerm+'"');
    

    【讨论】:

    • 感谢@Serge insas!但是是否可以找到包含查询的文件名,而不仅仅是找到具有该名称的文件?因为当前搜索xyz 仅适用于存在完全命名为xyz 的文件(xyza 不会包含在结果中)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2011-07-24
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多