|
/** |
|
* Routines.gs |
|
* ----------------------------------------------------------------------- |
|
* |
|
* A script for function that don't return anything, they just perform |
|
* operations on the active spreadsheet. |
|
*/ |
|
|
|
var SHEET_ID = '189X1mZ3IxzOMxr6Y7WMnyRk9JaPFA7KO8coeGV5joHc'; |
|
var TAB_NAME = 'Data' |
|
|
|
/** |
|
* A convience routine for executing multiple operations that occur when a |
|
* change happens to the spreadsheet, which is defined in Triggers. |
|
* |
|
* Return {null} |
|
*/ |
|
function onChange(x) { |
|
Logger.log(x); |
|
var curr = SpreadsheetApp.openById(SHEET_ID).getSheetByName(TAB_NAME); |
|
var values = curr.getDataRange().getValues(); |
|
|
|
populateWeekStart(curr, values); |
|
convertLinkToID(curr, values); |
|
convertTimeElapsedToDuration(curr, values); |
|
|
|
return null; |
|
} |
|
|
|
/** |
|
* Automatically populate the WeekStart column based on values |
|
* in the CreatedAt column |
|
* |
|
* Params: |
|
* - sheet {Spreadsheet} An instance of the Spreadsheet class |
|
* - data {Array} A 2D array of values currently in the sheet. |
|
* |
|
* Return {null} |
|
* |
|
*/ |
|
function populateWeekStart(sheet, data) { |
|
var idx = 6; // zero-index of the WeekStart column |
|
var dtstr, weekstart; |
|
|
|
for (var row = 1; row < data.length; row++) { // Start at 1 because 0 is spreadsheet header |
|
if (data[row][idx].length === 0) { |
|
dtstr = PARSE_CREATEDAT(data[row][0]); |
|
weekstart = GET_WEEKSTART(dtstr); |
|
sheet.getRange(row + 1, idx + 1).setValue(weekstart); // getRange indices are one-indexed |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
/** |
|
* Extract activity ID from the link to the activity |
|
* |
|
* Params: |
|
* - sheet {Spreadsheet} An instance of the Spreadsheet class |
|
* - data {Array} A 2D array of values currently in the sheet. |
|
* |
|
* Return {null} |
|
* |
|
*/ |
|
function convertLinkToID(sheet, data) { |
|
var idx = 5; // zero-index of the ActivityID column |
|
var id; |
|
var regx = /activities\/(\d+)$/; |
|
|
|
for (var row = 1; row < data.length; row++) { |
|
var regxObj = regx.exec(data[row][idx]); |
|
if (regxObj !== null) { |
|
sheet.getRange(row + 1, idx + 1).setValue(regxObj[1]); |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
/** |
|
* Map the number of seconds in ElapsedTimeInSeconds column into |
|
* a new column reformatting the value in HH:MM:SS format |
|
* |
|
* Params: |
|
* - sheet {Spreadsheet} An instance of the Spreadsheet class |
|
* - data {Array} A 2D array of values currently in the sheet. |
|
* |
|
* Return {null} |
|
* |
|
*/ |
|
function convertTimeElapsedToDuration(sheet, data) { |
|
var idx = 4; // zero-index of the Duration column |
|
var duration, seconds; |
|
|
|
for (var row = 1; row < data.length; row++) { |
|
if (data[row][idx].length === 0) { |
|
seconds = data[row][3]; |
|
duration = GET_DURATION(seconds); |
|
sheet.getRange(row + 1, idx + 1).setValue(duration); |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
|
|
/** |
|
* Make a request to the Strava API's athlete activity list endpoint and append each |
|
* of those activities as rows in the spreadsheet. The following variables are meant |
|
* to be modified: |
|
* |
|
* -resutlsPerPage {Number}: The number of resutls per page (keep this high to help |
|
* with running up against the rate limit. |
|
* |
|
* -startDate {String}: A date string in MM/DD/YYYY format. |
|
* |
|
*/ |
|
function populateSheetFromStrava() { |
|
var sheet = SpreadsheetApp.openById(SHEET_ID).getSheetByName(TAB_NAME) |
|
var resultsPerPage = 100; |
|
var startDate = '12/1/2018'; |
|
var i = 0; |
|
var code; |
|
do { |
|
i++; |
|
code = getAthleteActivities({ |
|
after: dateToEpoch_(startDate), |
|
page: i, |
|
per_page: resultsPerPage, |
|
}, handleActivityList); |
|
} while (code === 200.0); |
|
|
|
return null; |
|
|
|
function handleActivityList(res) { |
|
var data = JSON.parse(res.getContentText()) |
|
.map(function(activity) { |
|
var CreatedAt = Utilities.formatDate(new Date(activity.start_date_local), 'GMT', "MMMMM dd, yyyy 'at' hh:mma"); |
|
var ActivityType = activity.type; |
|
var DistanceMeters = Number(activity.distance); |
|
var ElapsedTimeInSeconds = Number(activity.elapsed_time); |
|
var Duration = GET_DURATION(ElapsedTimeInSeconds); |
|
var ActivityID = activity.id; |
|
var WeekStart = GET_WEEKSTART(PARSE_CREATEDAT(CreatedAt)); |
|
|
|
return [CreatedAt, ActivityType, DistanceMeters, ElapsedTimeInSeconds, Duration, ActivityID, WeekStart]; |
|
}); |
|
|
|
// Add "new" data to the appropriate sheet |
|
data.forEach(function(row) { |
|
sheet.appendRow(row); |
|
}); |
|
|
|
} // handleActivityList |
|
|
|
} |