-
Notifications
You must be signed in to change notification settings - Fork 5
/
Code.gs
185 lines (151 loc) · 5.49 KB
/
Code.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***********************************************************************************
* PLAID TRANSACTIONS TO GOOGLE SHEETS
* ---
* Author: Frank Harris (frank@hirefrank.com)
* Initial Date: Mar 9, 2019
* MIT License
*
* https://github.com/hirefrank/plaid-txns-google-sheets/blob/master/README.md
***********************************************************************************/
var SHEET = PropertiesService.getScriptProperties().getProperty('sheet');
/**
* Main function that grabs transactions daily
*/
function run() {
// days in reverse to scan for transactions
// pending transactions could take up to 5 days to clear
const DAYS_IN_REVERSE = 6;
// create date ranges
var start_date = new Date();
var end_date = new Date();
start_date.setDate(start_date.getDate() - DAYS_IN_REVERSE);
getTransactionHistory(formatDate(start_date), formatDate(end_date));
// 5s pause to prevent the functions running synchronously
Utilities.sleep(5000);
cleanup();
}
/**
* Grabs transactions for specified date range
*/
function batchRun() {
var start_date = '2019-01-01';
var end_date = '2019-04-05';
getTransactionHistory(start_date, end_date);
}
/**
* Get the Transactions via the Plaid API
*/
function getTransactionHistory(start_date, end_date) {
const ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty('access_token');
const CLIENT_ID = PropertiesService.getScriptProperties().getProperty('client_id');
const SECRET = PropertiesService.getScriptProperties().getProperty('secret');
const COUNT = PropertiesService.getScriptProperties().getProperty('count') || 500;
// headers are a parameter plaid requires for the post request
// plaid takes a contentType parameter
// google app script takes a content-type parameter
var headers = {
'contentType': 'application/json',
'Content-Type': 'application/json',
};
// data is a parameter plaid requires for the post request
// created via the plaid quickstart app (node)
var data = {
'access_token': ACCESS_TOKEN,
'client_id': CLIENT_ID,
'secret': SECRET,
'start_date': start_date,
'end_date': end_date,
'options': {count: COUNT, offset: 0,}
};
// pass in the necessary headers
// pass the payload as a json object
var parameters = {
'headers': headers,
'payload': JSON.stringify(data),
'method': 'post',
'muteHttpExceptions': true,
};
// api host + endpoint
var url = "https://development.plaid.com/transactions/get";
var response = UrlFetchApp.fetch(url, parameters);
// parse the response into a JSON object
var json_data = JSON.parse(response);
// get the transactions from the JSON
var transactions = json_data.transactions;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(SHEET);
var last_row = sheet.getLastRow();
var txns_ids = getTransactionIds(sheet.getRange(2,7,last_row,1));
// add each transaction as a new row in the sheet
for (i in transactions){
if (transactions[i].pending == false) {
// transaction id doesn't match existing ids
if (txns_ids.indexOf(transactions[i].transaction_id) == -1) {
// will put each in the row's columns in order
var row = [
transactions[i].date,
transactions[i].name,
transactions[i].amount,
transactions[i].iso_currency_code,
transactions[i].category.toString(),
transactions[i].transaction_type,
transactions[i].transaction_id,
transactions[i].account_id,
transactions[i].category.toString() == 'Transfer,Credit' ? 'true' : 'false'
]
// added below the lowest row in the sheet
sheet.appendRow(row);
}
}
}
}
/**
* Removes transactions from the spreadsheet
*/
function reset() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(SHEET);
var last_row = sheet.getLastRow();
sheet.getRange('2:' + last_row).activate();
sheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
};
/**
* Returns transaction_ids
*/
function getTransactionIds(range) {
var ids = [];
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var cv = range.getCell(i,j).getValue();
if (cv !== '') {
ids.push(cv);
}
}
}
return ids;
};
/**
* Left aligns all cells in the spreadsheet and sorts by date
*/
function cleanup() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(SHEET);
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
sheet.getActiveRangeList().setHorizontalAlignment('left');
sheet.getRange('A:A').activate();
sheet.sort(1, false);
};
/**
* Returns the date in a Plaid friendly format, e.g. YYYY-MM-DD
*/
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}