-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathsum_by_last_column.gs
34 lines (32 loc) · 1019 Bytes
/
sum_by_last_column.gs
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
// ex: =sumByLastColumn(A1;{11;12})
function sumByLastColumn(name = 'test', arrayRange = []) {
const activeSheet = SpreadsheetApp.getActiveSpreadsheet();
const currentSheetName = activeSheet.getActiveSheet().getName();
let sum = 0;
let textFinder, row, column, thisSheetName, checkExists;
const checkHaveRange = arrayRange.length > 0;
activeSheet.getSheets().map(function(x) {
thisSheetName = x.getName();
if(checkHaveRange){
checkExists = false;
arrayRange.map((el, key) => {
if(thisSheetName.search(el) != -1){
checkExists = true;
delete arrayRange[key];
}
})
if(!checkExists){
return false;
}
}
if(thisSheetName !== currentSheetName){
textFinder = x.createTextFinder(name).findNext();
if(textFinder){
row = textFinder.getRow();
column = x.getLastColumn();
sum += x.getRange(row,column).getValue();
}
}
});
return sum;
}