1

I have this error exception when i try to create a menu into a spreadsheet.

Exception: No se puede usar SpreadsheetApp.getUi() desde este contexto. (línea 151, archivo "Código")

This the extract for my code:

function addRowTotals() {
 var ss = SpreadsheetApp.getActive();
 var sheet1 = ss.getSheetByName('Inventario');
 var lastRow = sheet1.getLastRow();
 var data = sheet1.getRange(2, 1, lastRow-1, 3).getValues();

 Logger.log(data);

 var totalsArray = [];
 data.forEach(function(row){

 var name = row[0];
 var cost = row[1];
 var quantity = row[2];

  var total = cost * quantity;
  //Logger.log(total);
  row.push(total);
  totalsArray.push([total]);
  Logger.log(row);

  });

Logger.log(totalsArray);

 //* Paste data back into a sheet

 sheet1.getRange(2, 4, lastRow - 1, 1).setValues(totalsArray);

 }


 // ***** Add menu

 function onOpen(){

 var ui = SpreadsheetApp.getUi();
 ui.createMenu("Custom Menu")
.addItem("Add Row Totals", "addRowTotals")
.addToUi();

 }
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Please be more precise in your question: what error exception are you getting? Where is it triggered in your code? No one will be able to help you without those types of specifics. – QualiT Mar 17 '20 at 21:40
  • 1
    Does this answer your question? ["SpreadsheetApp.getUi() cannot be called from this context"](https://stackoverflow.com/questions/36117119/spreadsheetapp-getui-cannot-be-called-from-this-context) – Rubén Apr 26 '20 at 00:28

0 Answers0