Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

2.1K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda