4

I have been stuck here with this error for the past 3 hours.

Error Exception: Cannot call SpreadsheetApp.getUi() from this context.

I would appreciate the help.

function onOpen(e) {
      var menu = SpreadsheetApp.getUi().createMenu("Refresh");//The error is here
      menu.addItem("Refresh Week", "menu");
      menu.addToUi();
    }
//The same thing goes to this function as well
function menu(){
        //get the good week number
        var date = new Date();
        var wn = [(Math.floor(WEEKNUMBER(date))+3).toFixed(0)]
        do {  
            wn.push((wn[wn.length-1]-1).toFixed(0))
        } while(wn[wn.length-1] != 348);
        var line = "<select style='width:60px;height:40px;' id='select'>"
        for ( var x in wn ) 
          line +="<option>" + wn[x] + "</option>"
        line +="</select>"
        var ui = SpreadsheetApp.getUi();
        var html = HtmlService.createHtmlOutputFromFile('Selector')
            .setWidth(200)
            .setHeight(150).setContent("<div>"+line + "</div><br><div><button onclick='reset()'>Confirm</button></div><script>function reset(){var wn = document.getElementById('select').value;document.getElementsByTagName('Body')[0].style.cursor = 'wait';google.script.run.withSuccessHandler(function (){google.script.host.close();}).readWP2(wn);}</script>")
        SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
            .showModalDialog(html, 'Please select a week number');
}

So when I create a variable and set it to SpreadsheetApp.getUi() I start getting this error and the rest of the code just doesn't run anymore because of this error

enter image description here

I'm using Google Apps Script.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Omar
  • 181
  • 2
  • 14
  • What are steps to reproduce the issue? – Kos Jan 28 '22 at 14:19
  • I'm sorry, I didn't get your question – Omar Jan 28 '22 at 14:25
  • Please describe what are the actions person should do in a particular order to be able to see exact error message that you see on your computer? – Kos Jan 28 '22 at 14:27
  • Oh ok, Sorry. Just updated it – Omar Jan 28 '22 at 14:32
  • 1
    Sorry for my English, If I understand correctly from a screenshot, you're running `menu` function from the edttor using "run function"? That's why you got this error, UI is not available in a context of the editor, it must be called from a spreadsheet. – Kos Jan 28 '22 at 15:01
  • 2
    Which kind of script are you using? Standalone script or bound script? This error is usually returned when trying to invoke the user interface functions (getUI) from a document other than the active one. A bound script can interact only with the interface user for the current instance of the open file. So a standalone script will never work and a script bound to a document cannot affect the user interface of another document. – k4k4sh1 Jan 28 '22 at 15:02
  • Sorry for the late response. Yeah that makes sense, Thank you so much! – Omar Jan 28 '22 at 17:16

1 Answers1

7

I believe what you are trying to do is to create a dialog box that is similar to JavaScript Popup Boxes. Unfortunately, Apps Script does not have that feature. You need to bind it first to Google Sheets, Docs, Slides, or Forms file and the dialog box will appear on those apps.

I was able to replicate your issue by executing your code on a standalone script. A standalone script is any script that is not bound to a Google Sheets, Docs, Slides, or Forms file or Google Sites.

Standalone example:

enter image description here

SpreadsheetApp.getUi(); only interact with the UI for the current instance of an open spreadsheet, and only if the script is bound to the spreadsheet.

To solve your issue, make sure to:

  1. Create Spreadsheet.
  2. Select the menu item Extensions > Apps Script.
  3. Delete any code in the script editor and paste your code.
  4. Run your script and authenticate.

Your code should work on either Spreadsheet Menu or in Apps Script but the dialog box will always show on Spreadsheet.

Example:

enter image description here

enter image description here

References

Rubén
  • 34,714
  • 9
  • 70
  • 166
Nikko J.
  • 5,319
  • 1
  • 5
  • 14
  • Yeah that makes much more sense to me now. It worked! but I'm wondering why did I have to create a new excel file in order for it to work? Thank you!! – Omar Jan 28 '22 at 17:38
  • Ah I see. Thank you so much. I did, but I need at least 15 reputation to cast a vote. – Omar Jan 28 '22 at 18:04
  • My script showed under "overview" the name of it's container, which was a Google Sheet. Yet I still got this error. Shouldn't that be the name of it's bound container? If not, do you know how to determine if a script is bound to a container? – John Pankowicz Aug 13 '22 at 14:04