Last active
February 20, 2020 15:18
-
-
Save JeremyMorgan/bbab05404cddcfe89c97f36843e36d65 to your computer and use it in GitHub Desktop.
Google Sheets Code to pull Dev.TO stats
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 getFirstEmptyRow() { | |
var spr = SpreadsheetApp.getActiveSpreadsheet(); | |
var column = spr.getRange('A:A'); | |
var values = column.getValues(); // get all data in one call | |
var ct = 0; | |
while (values[ct][0] != "") { | |
ct++; | |
} | |
return (ct); | |
} | |
function formatDate(timestamp) { | |
var year = timestamp.substring(0, 4); | |
var month = timestamp.substring(5, 7); | |
var day = timestamp.substring(8, 10); | |
var newdate = month + "/" + day + "/" + year; | |
return newdate; | |
} | |
function getStats() { | |
var options = { | |
"access-control-allow-headers": "Content-Type", | |
"api-key": "[YOUR API KEY]" | |
} | |
var header = { | |
'headers': options | |
} | |
try { | |
// make the API call | |
var response = UrlFetchApp.fetch("https://dev.to/api/articles/me/published?per_page=500", header); | |
// parse output as JSON | |
var output = JSON.parse(response.getContentText()); | |
// grab the spreadsheet tab | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('[SPREADSHEET TAB NAME'); | |
output.forEach(function(row, index) { | |
sheet.getRange(index + 2, 1).setValue(row.title); | |
sheet.getRange(index + 2, 2).setValue(formatDate(row.published_timestamp)); | |
sheet.getRange(index + 2, 3).setValue(row.page_views_count); | |
sheet.getRange(index + 2, 4).setValue(row.positive_reactions_count); | |
sheet.getRange(index + 2, 5).setValue(row.comments_count); | |
}); | |
} catch (err) { | |
throw new Error(err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment