Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.1K
Views
Exception: Cannot call SpreadsheetApp.getUi() from this context

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 app script.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe what you are trying to do is you want 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

  • SpreadsheetApp.getUi()
  • Standalone Scripts
  • Container-bound Scripts
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!