0

I have the same problem as discused here: How to pass a parameter to html?

In other words, I need to pass argument to Picker so he know what file I'm selecting. Based on this file type different function will be run as a part of callback.

I'm using Picker sample as provided by Google: https://developers.google.com/apps-script/guides/dialogs

Picker is showed with the following code:

function showPicker() {
  var html = HtmlService.createHtmlOutputFromFile('Picker.html')
      .setWidth(600)
      .setHeight(425)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi().showModalDialog(html, 'Select a file');
}

Of course right now I would like to pass argument to picker so it knows what file I am trying to open. Based on the example provided in the previous post (indeed it looks like the best approach) I neded up with the following:

function doGet() {
    var htmlTemplate = HtmlService.createTemplateFromFile('Picker');
    htmlTemplate.dataFromServerTemplate = { first: "hello", last: "world" };
    var htmlOutput = htmlTemplate.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
        .setTitle('sample');
    return htmlOutput;
}

Unfortunately Picker window does not show up. How to fix it?

Any help will be much appreciated.

Hikari
  • 21
  • 3
  • Did you check the browser's console window? Did you see any errors? – Amit Agarwal Jul 12 '18 at 12:21
  • What is going on in your Javascript? The HTML is the most trivial part. There's no need to "pass the argument to picker". When you construct the new PickerBuilder object, you set the callback function by calling 'setCallback(callBack)'. When the callback function fires, it receives the event object with details about your selection. Simply inspect the selected data in your callback function and execute other code based on that. There are millions of reasons as to why the picker doesn't show up so please post your Javascript code and console.log() output – Anton Dementiev Jul 12 '18 at 13:47
  • Hi, Thank you for your reply. Here you have the codes: 1) Showing Picker (this code works): function showPicker() { var html = HtmlService.createHtmlOutputFromFile('Picker.html') .setWidth(600) .setHeight(425) .setSandboxMode(HtmlService.SandboxMode.IFRAME); SpreadsheetApp.getUi().showModalDialog(html, 'Select a file'); } I can invoke Picker for two different purposes (2 items in menu). How my code.gs can understand which out of 2 items was used? – Hikari Jul 12 '18 at 20:16
  • Please use this notation @Hikari to reply to comments. Otherwise, I can't see the notifications for your comments. – Anton Dementiev Jul 13 '18 at 16:34

1 Answers1

0

You can handle this in client-side Javascript of the HTML template. The example provided by Google displays the Picker immediately upon loading the page, but it doesn't have to be this way.

You can tie the createPicker() function to events in your client-side HTML (e.g. selecting a list item) and pass the DOM element's unique ID to that function.

HTML

<span id="item1" onClick="createPicker(this.id)"> Item1 </span> 

Javascript

function createPicker(id){
  // build the picker based on the id of the item selected
}
Anton Dementiev
  • 5,451
  • 4
  • 20
  • 32
  • Hi @Anton Google example creates empty HTML and then API is loaded once you press button in Picker.html `` `
    ` `

    ` `` `

    ` `
    ` `` `` Then it runs function getOAuthToken() and finally function createPicker(token). Sorry for such silly questions, but I'm new to Google Script. How to implement your idea in those files?
    – Hikari Jul 15 '18 at 08:54
  • @Hikari you simply call the token function at loading and store the token on the client-side (e.g. var token = token;) In the createPicker() function, you check if the token is set via 'if (token) { // do something. }' (provided 'var token' is a global variable accessible to the function). Google Apps Script is basically Javascript, so you need to understand the basic concepts of JS well enough to be able to refactor someone else's code. Otherwise, making headway will be very difficult. – Anton Dementiev Jul 15 '18 at 12:00