【问题标题】:How do you get a selection from a DocsListDialogue?如何从 DocsListDialogue 中获得选择?
【发布时间】:2013-10-13 18:40:57
【问题描述】:

我有一个文档列表对话框,这是我目前的代码。我如何从DocListDialogue 获得实际选择?我一直在尝试 eventInfo.parameter,但它只返回了一个通用对象,我需要返回一个文件。这是我的代码:

function init() {
   var app = UiApp.createApplication().setTitle("WriteWell");
   var selectionHandler = app.createServerHandler("selectHandler");
   app.createDocsListDialog().showDocsPicker().setDialogTitle("Select File to Open").addSelectionHandler(selectionHandler);
   app.add(app.createVerticalPanel().setId("Panel"));
   return app;
 }

 function doGet(e) {
   return init();
 }

function selectHandler(eventInfo){
  var parameter = eventInfo.parameter;//Selection???
  var app = UiApp.getActiveApplication();
  var panel = app.getElementById("Panel");
  panel.add(app.createLabel(parameter.getId()));//Returns an error
}

【问题讨论】:

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


    【解决方案1】:

    在检查eventInfo.parameter 的内容时,我们看到返回如下内容:

    {
        source=u01234567890,
        items=[
          {
            id=0Abcd-efgH_ijKLLLmnOPQr0stuvwX,
            name=file_name,
            url=https://docs.google.com/file/d/0Abcd-efgH_ijKLLLmnOPQr0stuvwX/edit?usp=drive_web
          }
        ],
        u01234567890=[
          {
            id=0Abcd-efgH_ijKLLLmnOPQr0stuvwX,
            name=file_name,
            url=https://docs.google.com/file/d/0Abcd-efgH_ijKLLLmnOPQr0stuvwX/edit?usp=drive_web
          }
        ],
        eventType=selection
    }
    

    如果您需要所选文件的 ID,则需要以下内容:

    ...
    eventInfo.parameter.items[0].id;
    ...
    

    【讨论】:

      【解决方案2】:

      如果您想查看 eventInfo 中的内容,可以使用

      Logger.log(Utilities.jsonStringify(eventInfo));

      在这种情况下会返回类似的东西:

      [13-10-13 21:25:21:722 CEST] {"parameter":{"source":"u16052058908","items":[{"id":"0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE","name":"Tracker locaux","url":"https://docs.google.com/a/insas.be/spreadsheet/ccc?key\u003d0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE\u0026usp\u003ddrive_web"}],"eventType":"selection","u16052058908":[{"id":"0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE","name":"Tracker locaux","url":"https://docs.google.com/a/insas.be/spreadsheet/ccc?key\u003d0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE\u0026usp\u003ddrive_web"}]}}
      

      看看它你会发现你可以得到你想要使用的对象属性(例如):

      var docsInfo = eventInfo.parameter.items;
      

      这将返回一个包含文件名、ID 和 URL 的对象数组(每个选定文件一个)

      只需迭代此对象数组即可从每个项目中获取您想要的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        相关资源
        最近更新 更多