【问题标题】:Show a list of Drive files and allow the user to select one显示云端硬盘文件列表并允许用户选择一个
【发布时间】:2015-03-31 17:13:43
【问题描述】:

我想创建一个网络应用程序以在 Google 网站中使用。该应用程序将允许用户在 Google Drive 中选择一个电子表格并将文件 ID 传递给脚本,然后使用 getByID 函数来完成邮件合并。除了文件的选择,我所有的代码都在工作。有没有一种方法可以让用户从他们的 Google Drive 浏览到文件,在树上上/下等,就像在文件打开场景中一样?我们正在使用 Google Apps for Non-profits,并将列出域文件。

【问题讨论】:

    标签: google-apps-script google-drive-api google-sites


    【解决方案1】:

    您可以使用 DriveApp 查询所有用户表。

    https://developers.google.com/apps-script/reference/drive/drive-app#getFilesByType(String).

    您可以构建一个可以发送到您的网络应用程序的对象,以便用户可以选择适当的工作表。

    function getAllSpreadsheets(){
         var sheetList = [];
          var allSheets = DriveApp.getFilesByType(MimeType.GOOGLE_SHEETS);
          while(allSheets.hasNext()){
            var sheet = allSheets.next();
            sheetList.push({"name":sheet.getName(), "id":sheet.getId()});
        }
    return sheetList;
    }
    

    在您的 webapp 端,您可以执行以下操作:

    <label for="selected-sheet">
          <b>Select spreadsheet</b></label>
         <select id="selected-sheet">
         </select>
    <script>
    $(function(){
      google.script.run
       .withSuccessHandler(addSheets)
       .withFailureHandler(function(e){alert("Error: "+ e)})
       .getAllSpreadsheets();
    
    function addSheets(options){
    
    var select = $('#selected-sheet');
    $.each(options, function (i, item) {
        select.append($('<option>', { 
            value: item.id,
            text : item.name 
        }));
    });
    }
    </script>
    

    【讨论】:

      【解决方案2】:

      您可以使用 Google 选择器对话框来实现此功能。阅读我们的Opening Files 文档。

      更多关于Google Picker API 的信息也可以在我们的文档中找到。

      【讨论】:

      • 谢谢。 @丹麦格拉思。看来我需要弄清楚 API 密钥,即使这全部用于我们的域。可能会解释为什么其他一些事情不能正常工作,例如获取用户名。或使用服务帐户。我想在把代码放在一起之前我需要做更多的研究。我希望涉及的东西少一点,但会看看 Picker API
      • Picker api 是正确的答案。唯一的缺点是用户只能选择她有权访问的文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多