1

I just want to tweak some menus after performing some actions on this HTML modal, using google.script.run API

my gasp code is something essentially like this:

function some() {
  const output = HtmlService.createHtmlOutput(someHTML).setWidth(600).setHeight(425);
  SpreadsheetApp.getUi().showModalDialog(output, 'some title')
}

function tweakMenu(){
  const ui = SpreadsheetApp.getUi()
  const mainMenu = ui
    .createMenu('Mainn')
    .addItem('So nice', 'function').addToUi()
  return 'some thing'
}

Then I have the following chunk along with the html loaded by HTMLService:

var selfClose = (e) => {
  console.log(e) // does not log  'some thing'
  google.script.host.close();
}
$('#done-button').on('click', () => {
  google.script.run
    .withSuccessHandler(selfClose)
    .withFailureHandler(selfClose)
    .tweakMenu();
})

The function that tweaks the menu works well when called directly, however, when I call it by that JS click handler it does not return any data, either by success or failure handler, but the modal itself closes.

This is a google sheets public add-on but I'm currently testing this code in a staging context.

I have a feeling that some GAS APIs aren't available in this way, I've also tried to use URLFetchApp in the past and it didn't work either.

Is there any way to accomplish this? The bottomline is: I'm trying to tweak some stuff after the user upgrades their plan.

enapupe
  • 15,691
  • 3
  • 29
  • 45
  • 1
    ehm... what do you mean by "returning data"? Your `tweakMenu` does not have a `return` statement to return anything... Do you mean it does not change the menu? – Oleg Valter is with Ukraine Mar 04 '21 at 23:23
  • 1
    yes, the menu doesn't change. i'll also edit the code to reflect the not returning condition. – enapupe Mar 04 '21 at 23:39
  • 1
    hm... Which `authMode` you are testing your add-on in right now? However, you should be able to add menu items even in `NONE` mode. Does the `tweakMenu` look exactly like this, or is there something more to it? Just trying to triage possible issues – Oleg Valter is with Ukraine Mar 04 '21 at 23:54
  • 1
    `URLFetchApp` should only be inaccessible in triggers running in `NONE` or `LIMITED` modes, so if you say it does not work for you from a `google.script.run`, that's strange. Can you check if the same code works in an incognito mode of your browser of choice? – Oleg Valter is with Ukraine Mar 04 '21 at 23:59
  • 1
    tweakMenu is not exactly like that but it not doing anything fancy nor accessing any other APIs. But I think your hunch on user/auth/scope is a good place to start investigating. – enapupe Mar 05 '21 at 00:03
  • 1
    That's usually the main problem with Google Apps Script :) Haven't been doing editor add-ons in a while, but it looks like `google.script.run` should work fine in your case. Off-note: it is semantically more correct to call the `createAddonMenu` method (it won't fix the issue, though) in an editor add-on, so you might want to consider switching to that – Oleg Valter is with Ukraine Mar 05 '21 at 00:10
  • 1
    tested a similar setup - seems to create a menu just fine and return the resulting data as expected. One thing to clarify, though: is your add-on an *editor* add-on, or a *google workspace* add-on? – Oleg Valter is with Ukraine Mar 05 '21 at 00:20
  • 1
    Thanks, that means it must be scope related. It's a workspace add-on published via workspace marketplace – enapupe Mar 05 '21 at 00:31
  • Hm, I mean, is it an old-style editor workspace add-on (with `HTMLService`-based interface) or the new workspace add-on (with `CardService`-based user interface) - because if it is the latter, this might narrow down the possible reason – Oleg Valter is with Ukraine Mar 05 '21 at 00:39
  • Write your menu code in onOpen event and call to onopen event via script – Knowledge Game Mar 05 '21 at 00:42
  • 1
    @KnowledgeGame - enapupe is talking about *dynamically* changing menu items based on a user action, not about an initial menu creation. Dynamic menu changes are perfectly possible and can be invoked with `google.script.run` - the issue they are having is with the procedure failing for some reason. – Oleg Valter is with Ukraine Mar 05 '21 at 00:51
  • btw, @enapupe, can you also check which handler gets to handle the response, the success or the failure one? – Oleg Valter is with Ukraine Mar 05 '21 at 00:51
  • 1
    When you say its in a "staging" context, what do you mean exactly? – iansedano Mar 05 '21 at 11:28
  • @iansedano - "test deployment", most likely - we do not have too many options for testing add-ons after all. That said, I think it is important to discern whether it is a proper workspace add-on or a workspace editor add-on (thanks Google! /s) - I've been bitten by issues with the former multiple times – Oleg Valter is with Ukraine Mar 05 '21 at 12:45
  • @OlegValter Probably, just the word "staging" could mean many things. I've seen an issue with a add-on at the review stage which broke some of the functionality due to the changing `AuthMode` https://developers.google.com/workspace/add-ons/concepts/editor-auth-lifecycle – iansedano Mar 05 '21 at 12:58
  • @iansedano - yup, `AuthMode` was my first thought, but I ruled it out once looked again at the table accessible services under `NONE` and `LIMITED` modes - menu item creation is among them, only a very limited subset of contexts can't access UI (and it also seems that either of the handlers does fire [however we do not know yet which one]) – Oleg Valter is with Ukraine Mar 05 '21 at 13:17
  • I know it should add scopes automatically, and I suppose it did, currently it has `https://www.googleapis.com/auth/script.container.ui`, are there any other scopes related to this action? – enapupe Mar 05 '21 at 13:52
  • @enapupe - nope, this is the only relevant one. Besides, if you take a look at the [auth modes table](https://developers.google.com/workspace/add-ons/concepts/editor-auth-lifecycle#authorization_modes), menu items addition should work even in the `NONE` and `LIMITED` mode. I am inclined to think your issue is related to either bad behavior of `google.script.run` with multiple accounts or workspace add-ons specifics (btw, can you clarify which type of add-on it is? Thanks to the new branding, I am not sure you meant a workspace add-on or an editor workspace add-on :)) – Oleg Valter is with Ukraine Mar 05 '21 at 13:59
  • 1
    I'm not really sure how to distinguish between workspace add-on or editor workspace add-on (thanks google! /s). I'm using just `HTMLService`. This project was started a few months ago using latest documentation recommendations. BTW the request fired by the `run` call has response that contains (along with many other thinsg) : "ScriptError" and a localized message saying that "Authorization is required in order to execute that action" – enapupe Mar 05 '21 at 14:40
  • @enapupe - then it is most likely to be an old-style editor add-on. There are 2 ways to discern between them: 1. You use `CardService` for the sidebar interface, 2. The manifest contains a special resource defining properties and triggers of the addon. If both are missing - it is the older editor add-on :) – Oleg Valter is with Ukraine Mar 05 '21 at 14:50
  • @enapupe - that's a good catch - if you are logged in to several Google accounts, try to log out of them and then log in only to the one that authorized the add-on. If the issue goes away, bad luck, you landed into the several years old problem that has no known solutions (at least to me, and I bump into it regularly). If not, then we narrowed it down to a context problem - and I can take a further look – Oleg Valter is with Ukraine Mar 05 '21 at 14:52
  • 1
    It just worked after logging-in into incognito mode. ** giant facepalm **. If you like to compile couple of your hints and post it as answer I'll accept it – enapupe Mar 05 '21 at 15:02
  • @enapupe - ah, the time-tested "try incognito" solution, I like this one very much :) The issue probably exists for a decade (half a decade for sure) without any solution apart from the "tell the clients to avoid multi-login and good luck with explaining why they should do that". If you did not do GAS before, congrats on passing the rite of passage, I guess. Re: answer, I will look for a dupe target to link to instead, there are a lot of Q&As with useful info on debugging this issue - might save someone a couple of hours (I certainly did scratch my head for a while the first time it happened). – Oleg Valter is with Ukraine Mar 05 '21 at 15:10
  • like this (adding to "linked" questions): https://stackoverflow.com/q/60442915/, and this: https://stackoverflow.com/q/65124449/, and this: https://stackoverflow.com/q/60230776 – Oleg Valter is with Ukraine Mar 05 '21 at 15:12

0 Answers0