简体   繁体   中英

Google Apps Script Popup Dialog, library issues

So I have a library that is used by several dozen sheets, which call the individual functions like this:

(from a menu)

.addItem('What are the variables for this sheet', 'LibraryName.logMyVariables')

This is all well and good, but now I have an issue where I cant seem to operate a Modal Dialog.

These two parts work fine:

.addItem('Show dialog (BETA)', 'LibraryName.showDialog')

(on the library side, backend gs)

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('projectDialog')
      .setWidth(900)
      .setHeight(900);
  SpreadsheetApp.getUi()
      .showModalDialog(html, 'Project Update Thingy');
}

so far so good. Where I run into issues is when the frontend dialog of the library script tries to run its populate numbers function. Although it works just fine when run from the spreadsheet that owns the library, I'm getting an error from other spreadsheets running the library.

This is in the dialog html.

google.script.run.withSuccessHandler(addOptions).withFailureHandler(errorOutput).getExistingProjectNumbers();

I get this error:

google.script.run.withSuccessHandler(...).withFailureHandler(...).getExistingProjectNumbers is not a function at userCodeAppPanel:2

My instinct was that this means the dialog is running locally, so I need to call it like I call the other library functions (this breaks it working on the actual library but I don't care) so I tried:

google.script.run.withSuccessHandler(addOptions).withFailureHandler(errorOutput).LibraryName.getExistingProjectNumbers();

but that turned into this error

VM34:2 Uncaught TypeError: Cannot read property 'getExistingProjectNumbers' of undefined

I feel like I'm very close I just need one more insight to fix this. Let me reiterate that this function does run just fine within the library script spreadsheet itself, it is only when you access the dialog through the library that I have this problem.

It turned out that the pages running the library just needed wrapper functions.

All I had to add to the local page was four functions like this

function getExistingProjectNumbers(){Library.getExistingProjectNumbers()}

I found this solution in a random blog. If you have more than four functions you can make it complicated by passing the function as an argument to one generic wrapper function but this was tidier for me and reasonable since the ui was only using 4 backend functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM