Google Picker - Return the File ID to my Google Script

≡放荡痞女 提交于 2021-02-07 03:54:33

问题


I have a fairly basic spreadsheet that uses some Google Scripts to accomplish various tasks. I was trying to cleanup the interface for the end user, and decided to implement the Google Picker. Originally the user had to manually import a CSV into the spreadsheet. The new goal here is to select the CSV via the Google Picker, upload it, import it, then delete it. I already have all the code working to import it and delete it. I just worked up the code for the picker, and it seems to work fine. However, and I think I'm just missing something small, how do I pass the File ID back from the Picker.html to my Google Scripts in order to continue my process?

If it helps, I'm using the basic callback provided in the Google documentation right now. I'm assuming this is where the change will be made. Just not sure what to do.

  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.';
    }
  }

回答1:


This should probably work:

In your pickerCallback(data) function:

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
}

In Code.gs, create a function to handle the input from the picker:

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



回答2:


First of all, open the chrome developer console when you are running this so you can see any errors that happen client side (when the picker is active). You can also use console.log to report any variable values in the Chrome console.

secondly, the call to the server works asynchronously, so it means that in your code, you'll get your message 'script was run', when it fact it hasn't yet. All that's happened is that google.script.run has asked for your server side function to execute.

That's why you have withSuccessHandler and withFailureHandler.

so you should do

google.script.run

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

.withFailureHandler (function (err) {
  document.getElementById('result').innerHTML = err;
})
.justatest (fileId);

and back in the server script

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

If you then go back and look in the script log file, you should see the fileId.



来源:https://stackoverflow.com/questions/23074352/google-picker-return-the-file-id-to-my-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!