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();
}