简体   繁体   中英

google sheets apps script

I wanted to ask I have a few lines of code. And until yesterday, everything worked perfectly for me.

Now when I start debugging the script, it ends up with an error message on line 24 => var ui = SpreadsheetApp.getUi(); .

Exception: Cannot call SpreadsheetApp.getUi() from this context. inputBox @ ulozDataDoDb.gs:24

But when I run this script from the button in the google sheet, it works.

Does anyone know what to do with it?

function inputBox() {
  
  //definice aktivního sešitu
  var ss = SpreadsheetApp.getActive();

  //definice aktivního listu
  var faktura = ss.getSheetByName("faktura");
  var dataBaze = ss.getSheetByName("db");

  //definice promenných pro dalši práci
  var cisloFaktury = faktura.getRange("I3").getValue();
  var varSymbol = faktura.getRange("E22").getValue();
  var datumVystaveni = faktura.getRange("datumVystaveni").getValue();
  var prijmeni = faktura.getRange("prijmeni").getValue();
  var jmeno = faktura.getRange("jmeno").getValue();
  var prijmeniJmeno = jmeno + " " + prijmeni;
  var castka = faktura.getRange("I55").getValue();
  var konstSymb = faktura.getRange("E23").getValue();
  var specSymb = faktura.getRange("E24").getValue();
  var formaUhrady = faktura.getRange("E19").getValue();
  
  //zapsat do DB
  //popis do DB
  var ui = SpreadsheetApp.getUi();
  var popis_v_DB = ui.prompt("Zadejte název do DB.").getResponseText();
  popis_v_DB = popis_v_DB.split(" ").join("_"); //vyhledá mezery a nahradí znakem

The Cannot call SpreadsheetApp.getUi() from this context. exception you are receiving is due to the fact that that prompt has to be actioned on by a user and not by a script.

According to the documentation :

prompt(prompt) - Will open an input dialog box in the user's editor with the given message and an "OK" button. This method suspends the server-side script while the dialog is open. The script resumes after the user dismisses the dialog.

Reference

Try to replace

var ui = SpreadsheetApp.getUi();
var popis_v_DB = ui.prompt("Zadejte název do DB.").getResponseText();

by

var popis_v_DB = Browser.inputBox("Informace.", "Zadejte název do DB.", Browser.Buttons.OK_CANCEL);

but as far as you ask somebody to enter a value, you will do it by the sheet not by the script editor !

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