Access data in Google App Script from spread sheet modal html form

老子叫甜甜 提交于 2021-02-08 12:12:36

问题


I have Google spreadsheet script which will show modal dialog using SpreadsheetApp.getUi().showModalDialog using a html form which is created as a file in the SpreadSheet Script project.

This is how my Google Script project structure is :

--Code.gs
 |
 |- openFormModalDialog()
 |- fetchData()
 |- updateSheet()
 |
--form.html

When click button in my form.html which is showed in the modal from the Code.gs I wan to call a function in my Code.gs to process the data entered by the user in the form. I have multiple input fields in the form.

I want to access form input values to make external service call and fetch and updated sheet rows.

I am not able to figure out how to access the data in the form filed in App Script.

This is how I am opening my form:

function openFormModalDialog() {
  var ui = SpreadsheetApp.getUi();
  var htmlOutput = HtmlService
     .createHtmlOutputFromFile('form')
     .setWidth(400)
     .setHeight(300);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'User Information');
}

Is there a way I can pass my App-Script function as parameter to html? I am not able to find any relevant document related to this.


回答1:


I think you should use the google.script.run api

Example :

<button onClick = "google.script.run.test();">Click me to run test()</button>

This will run your test()

Check this out : https://developers.google.com/apps-script/guides/html/reference/run#myFunction(...)

We can pass arguments to function like this google.script.run.test("value1");

We can run another function(javascript function in web page/dialog here) after this function has completed execution, like this google.script.run.withSuccessHandler(nextFunctionName).test("value1");

We can also run another function(javascript function in web page/dialog here) if this function fails its execution, like this google.script.run.withFailureHandler(nextFunctionName).test("value1");



来源:https://stackoverflow.com/questions/49442770/access-data-in-google-app-script-from-spread-sheet-modal-html-form

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