-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoweroutage-fetch.gs
More file actions
39 lines (35 loc) · 1.5 KB
/
poweroutage-fetch.gs
File metadata and controls
39 lines (35 loc) · 1.5 KB
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
function fetch() {
var url = atob("aHR0cHM6Ly9wb3dlcm91dGFnZS51cy9hcmVhL3JlZ2lvbnMvbmV3JTIwZW5nbGFuZA==");
var content = UrlFetchApp.fetch(url).getContentText();
var fetchTime = Utilities.formatDate(new Date(), 'Etc/GMT', "yyyy-MM-dd HH:mm:ss");
//Logger.log('website content: ' + content);
Logger.log('fetch time: ' + fetchTime);
//add data to the spreadsheet, into array of column header names
var row_data = {
DateTime:fetchTime,
MA:Number(content.match(/stateAbbr:"MA".*?outageCount:(\d+)/)?.[1] ?? 0),
CT:Number(content.match(/stateAbbr:"CT".*?outageCount:(\d+)/)?.[1] ?? 0),
VT:Number(content.match(/stateAbbr:"VT".*?outageCount:(\d+)/)?.[1] ?? 0),
NH:Number(content.match(/stateAbbr:"NH".*?outageCount:(\d+)/)?.[1] ?? 0),
};
insertRowInTracker(row_data)
}
function insertRowInTracker(rowData) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; //[0] is the first sheet
var rowValues = [];
var columnHeaders = sheet.getDataRange().offset(0, 0, 1).getValues()[0];
Logger.log("writing to: ", sheet);
Logger.log("writing: ", rowData);
columnHeaders.forEach((header) => {
rowValues.push(rowData[header]);
});
sheet.appendRow(rowValues);
}
function createTrigger() { //optional, you can make a trigger in the Apps Script
ScriptApp.newTrigger('fetch')
.timeBased()
.everyDays(1) // Frequency is required if you are using atHour() or nearMinute()
//.atHour(8)
.after(1000 * 60 * 15) //15 minutes in milliseconds
.create();
}