Last active
October 17, 2023 05:29
-
-
Save interdigitize/53e827a4aa0e6722c5e4c140228b125a to your computer and use it in GitHub Desktop.
Scrape and save data to Google Sheets with Apps Script - create trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createTrigger() { | |
// Trigger once a day | |
ScriptApp.newTrigger('getCovidDataAndUpdateSpreadSheet') | |
.timeBased() | |
.atHour(8) | |
.everyDays(1) // Frequency is required if you are using atHour() or nearMinute() | |
.create(); | |
} | |
function getCovidDataAndUpdateSpreadSheet() { | |
var url = "http://publichealth.lacounty.gov/media/Coronavirus/locations.htm" | |
//fetch site content | |
var websiteContent = UrlFetchApp.fetch(url).getContentText(); | |
var fetchTime = Utilities.formatDate(new Date(), 'Etc/GMT', "yyyy-MM-dd HH:mm:ssZ"); // "yyyy-MM-dd'T'HH:mm:ss'Z'" | |
//extract data | |
var laCasesRegExp = new RegExp(/(Laboratory Confirmed Cases \(LCC\))([tdh<>\/]+)([0-9]+)/m); | |
var laDeathsRegExp = new RegExp(/(Deaths)([tdh<>\/]+)([0-9]+)/m); | |
var laCasesMatchText = laCasesRegExp.exec(websiteContent); | |
var laDeathsMatchText = laDeathsRegExp.exec(websiteContent); | |
//add data to the spreadsheet | |
var la_row_data = { | |
date_time:fetchTime, | |
number_of_cases:laCasesMatchText[3], | |
number_of_deaths:laDeathsMatchText[3] | |
}; | |
insertRowInTracker(la_row_data) | |
} | |
function insertRowInTracker(rowData) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment