Skip to content

Instantly share code, notes, and snippets.

@Elchi3
Created March 23, 2014 12:38
Show Gist options
  • Save Elchi3/9722538 to your computer and use it in GitHub Desktop.
Save Elchi3/9722538 to your computer and use it in GitHub Desktop.
Import documentation status data in a Google Spreadsheet
function fetchData() {
var docStatusPages = ["Accessibility", "Apps", "CSS", "DevTools", "Firefox_OS", "Games", "HTML",
"JavaScript", "Marketplace", "MathML", "SVG",
"API/CSSOM", "API/Device_API", "API/DOM", "API/File_API", "API/HTML_API",
"API/IndexedDB", "API/SVG_API", "API/WebAudio", "API/WebRTC", "API/WebSockets",
"API/WebWorkers"
];
for (i = 0; i < docStatusPages.length; i++) {
var page = docStatusPages[i];
// ==================== Fetch from MDN ===========================================================
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = 'https://developer.mozilla.org/en-US/docs/MDN/Doc_status/' + page + '?raw&macros&section=json';
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
// ======================= Build or choose sheet =================================================
var s = ss.getSheetByName(page);
if (!s) {
var headline = ["Date"];
for (var key in data) {
if (data.hasOwnProperty(key)) {
headline.push(data[key].title);
}
}
s = ss.insertSheet(page);
s.appendRow(headline);
}
// clear formats to prevent duplicate entries due to date formatting :/
s.getRange("A2:A").setNumberFormat('@STRING@');
// ====================== Build data array from JSON ============================================
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = year + "/" + month + "/" + day;
var metrics = [newdate];
for (var key in data) {
if (data.hasOwnProperty(key)) {
metrics.push(data[key].counter);
}
}
// ======================== Append new row when that date is not yet there ==========================
var range = s.getRange("A2:A").getValues();
function _flatten(range) {
var results = [];
var row, column;
for(row = 0; row < range.length; row++) {
for(column = 0; column < range[row].length; column++) {
results.push(range[row][column]);
}
}
return results;
}
function _getIndex(range, value) {
return _flatten(range).indexOf(value);
}
if (_getIndex(range, newdate) == -1) {
s.appendRow(metrics);
}
} // end for loop
};
// ======================== Add a menu button to manually fetch data ==========================
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu("MDNImport", [{
name : "MDN import",
functionName : "fetchData"
}]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment