Skip to content

Instantly share code, notes, and snippets.

@interdigitize
Last active April 4, 2020 06:42
Show Gist options
  • Save interdigitize/7f1640b250c0ebb2a65c65d76f1a69d6 to your computer and use it in GitHub Desktop.
Save interdigitize/7f1640b250c0ebb2a65c65d76f1a69d6 to your computer and use it in GitHub Desktop.
Scrape and save data to Google Sheets with Apps Script - add data to the spreadsheet
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