forked from benlcollins/introductionToAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path016_IntroToAppsScript_CustomMenus.js
More file actions
49 lines (32 loc) · 960 Bytes
/
Copy path016_IntroToAppsScript_CustomMenus.js
File metadata and controls
49 lines (32 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// add total for each row into our Sheet
function addRowTotals() {
const ss = SpreadsheetApp.getActive();
const sheet1 = ss.getSheetByName("Sheet1");
const lastRow = sheet1.getLastRow();
const data = sheet1.getRange(1,1,lastRow,3).getValues();
console.log(data);
// remove header row
data.shift();
console.log(data);
const totalsArray = [];
data.forEach(row => {
const cost = row[1];
const quantity = row[2];
const total = cost * quantity;
//row.push(total);
//console.log(row);
totalsArray.push([total]);
});
console.log(totalsArray);
// paste data back into Sheet
sheet1.getRange(2,4,lastRow - 1,1).setValues(totalsArray);
}
// add menu
// onOpen is a special function
// runs when your Sheet opens
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu("Custom Menu")
.addItem("Add Row Totals","addRowTotals")
.addToUi();
}