Google apps script Google Picker-将文件ID返回到我的Google脚本

Google apps script Google Picker-将文件ID返回到我的Google脚本,google-apps-script,google-apps,google-picker,Google Apps Script,Google Apps,Google Picker,我有一个相当基本的电子表格,它使用一些谷歌脚本来完成各种任务。我试图为最终用户清理界面,并决定实现GooglePicker。最初,用户必须手动将CSV导入电子表格。这里的新目标是通过Google Picker选择CSV,上传,导入,然后删除。我已经有了导入和删除它的所有代码。我刚刚为选择器设计了代码,它似乎工作得很好。然而,我想我只是遗漏了一些小的东西,我如何将文件ID从Picker.html传递回我的Google脚本以继续我的过程 如果有帮助的话,我现在正在使用Google文档中提供的基本回调

我有一个相当基本的电子表格,它使用一些谷歌脚本来完成各种任务。我试图为最终用户清理界面,并决定实现GooglePicker。最初,用户必须手动将CSV导入电子表格。这里的新目标是通过Google Picker选择CSV,上传,导入,然后删除。我已经有了导入和删除它的所有代码。我刚刚为选择器设计了代码,它似乎工作得很好。然而,我想我只是遗漏了一些小的东西,我如何将文件ID从Picker.html传递回我的Google脚本以继续我的过程

如果有帮助的话,我现在正在使用Google文档中提供的基本回调。我想这就是改变的地方。只是不知道该怎么办

  function pickerCallback(data) {
    var action = data[google.picker.Response.ACTION];
    if (action == google.picker.Action.PICKED) {
      var doc = data[google.picker.Response.DOCUMENTS][0];
      var id = doc[google.picker.Document.ID];
      var url = doc[google.picker.Document.URL];
      var title = doc[google.picker.Document.NAME];
      document.getElementById('result').innerHTML =
          '<b>You chose:</b><br>Name: <a href="' + url + '">' + title + '</a><br>ID: ' + id;
    } else if (action == google.picker.Action.CANCEL) {
      document.getElementById('result').innerHTML = 'Picker canceled.';
    }
  }
函数选择器回调(数据){
var action=data[google.picker.Response.action];
if(action==google.picker.action.PICKED){
var doc=data[google.picker.Response.DOCUMENTS][0];
var id=doc[google.picker.Document.id];
var url=doc[google.picker.Document.url];
var title=doc[google.picker.Document.NAME];
document.getElementById('result').innerHTML=
“您选择:
名称:
ID:”+ID; }else if(action==google.picker.action.CANCEL){ document.getElementById('result').innerHTML='Picker cancelled'; } }
这可能会起作用:

在pickerCallback(数据)函数中:

if (data.action == google.picker.Action.PICKED) {
  var fileId = data.docs[0].id;
  google.script.run
  .withSuccessHandler(useData) // this will call the google apps script function in your Code.gs file
  .doSomething(fileId); // this is a function in your JavaScript section where you will do something with the code you got from your apps script function  
}

function useData(data) {
 // do something with the data
}
在Code.gs中,创建一个函数来处理选择器的输入:

function doSomething(fileId) {
  // do an operation in Drive with the fileId
  var file = DriveApp.getFileById(fileId);
  var fileName = file.getName();
  return fileName;
}

首先,在运行该程序时打开chrome developer控制台,这样您就可以看到客户端发生的任何错误(当选择器处于活动状态时)。您还可以使用console.log在Chrome控制台中报告任何变量值

其次,对服务器的调用是异步工作的,因此这意味着在代码中,您将得到消息“scriptwasrunt”,而实际上它还没有运行。所发生的只是google.script.run请求执行服务器端函数

这就是为什么您有withSuccessHandler和withFailureHandler

所以你应该这么做

google.script.run

.withSuccessHandler (function (response) {
  document.getElementById('result').innerHTML = 'it worked'
})

.withFailureHandler (function (err) {
  document.getElementById('result').innerHTML = err;
})
.justatest (fileId);
然后回到服务器脚本中

function justatest(fileId) {
  Logger.log (fileId);
}

如果您随后返回并查看脚本日志文件,您应该会看到文件ID。

您甚至不需要调用.withSuccessHandler(useData)、google.script.run.doSomething(文件ID)就足够了。