-
Notifications
You must be signed in to change notification settings - Fork 0
/
Treasurer.gs
52 lines (45 loc) · 1.69 KB
/
Treasurer.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const EXPENSE_COLUMNS = Object.freeze(
{'Date':0, 'MsgId':1, 'TxnFrom':2, 'TxnWith':3, 'DebitAmt':4, 'CreditAmt': 5});
const METADATA_COLUMNS = Object.freeze(
{'From':0, 'Name':1, 'TxnWithRegex':2});
let activeSpreadsheet, templateSpreadsheet;
function initializeGlobals_() {
activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
}
function main_(month, year) {
try {
initializeGlobals_();
addExpenses_(month, year);
console.log('Successfully created expense report for date :: %s/%s', month, year);
activeSpreadsheet.toast('Expenses updated successfully')
} catch(e) {
console.error('Failed in creating expense report due to ', e.stack);
activeSpreadsheet.toast('Some error occured. Please try again later');
}
}
function addExpenses_(month, year) {
let expensesSheet = getExpensesSheet_(month, year);
activeSpreadsheet.setActiveSheet(expensesSheet);
activeSpreadsheet.moveActiveSheet(1);
let metadataSheet = getMetadataSheet_();
let metaDataValues = metadataSheet.getDataRange().getValues();
for (let i = 1; i < metaDataValues.length; i++) {
appendExpenses_(expensesSheet, metaDataValues[i], month, year);
}
expensesSheet.showSheet();
}
function enableAutoUpdate_() {
ScriptApp.newTrigger('lastMonthTreasurer')
.timeBased()
.onMonthDay(1)
.create();
SpreadsheetApp.getUi().alert('Auto update enabled successfully');
}
function disableAutoUpdate_() {
let allTriggers = ScriptApp.getProjectTriggers();
for (let i = 0; i < allTriggers.length; i++) {
if (allTriggers[i].getHandlerFunction() === 'lastMonthTreasurer')
ScriptApp.deleteTrigger(allTriggers[i]);
}
SpreadsheetApp.getUi().alert('Auto update disabled successfully');
}