function addRowTotal()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var lastRow= sheet.getLastRow()-1;
var data = sheet.getRange(2,1,lastRow,3).getValues();
var totalsArray=[];
data.forEach(function(row)
{
var name= row[0];
var price= row[1];
var qty = row[2];
var tot=price*qty;
var doz=price*12;
row.push(tot,doz);
//Logger.log (data);
totalsArray.push([tot,doz]);
Logger.log(totalsArray);
})
sheet.getRange(2,4,lastRow,2).setValues(totalsArray);
}
function OnOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu("Custom Menu")
.addItem("Row totals","addRowTotal")
.addToUi;
}
You are very close besides the two following things:
OnOpen
should be onOpen
,addToUi
should be
addToUi()
.Try this instead:
function onOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu("Custom Menu")
.addItem("Row totals","addRowTotal")
.addToUi();
}