Skip to content

Instantly share code, notes, and snippets.

@Elchi3
Created May 9, 2014 10:24
Show Gist options
  • Save Elchi3/d6504bd8005fb00af409 to your computer and use it in GitHub Desktop.
Save Elchi3/d6504bd8005fb00af409 to your computer and use it in GitHub Desktop.
Import localzation status data in a Google Spreadsheet
function fetchData() {
// helpers
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);
}
// setup
var languages = ["de", "fr", "ja", "es", "pl", "zh-CN", "pt-BR", "bn-BD", "ko", "ru", "it"];
var topics = ["DevTools", "Apps", "Firefox_OS", "Marketplace", "Firefox", "HTML", "CSS", "JavaScript", "MDN"];
for (i = 0; i < languages.length; i++) {
var language = languages[i];
for (j = 0; j < topics.length; j++) {
var topic = topics[j];
var metrics = [];
// ==================== Fetch from MDN ===========================================================
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = 'https://developer.mozilla.org/' + language + '/docs/MDN/Doc_status/'+ topic +'?raw&macros&section=json';
var response = UrlFetchApp.fetch(url);
var data;
try {
data = JSON.parse(response.getContentText());
} catch (e) {
Logger.log(language);
Logger.log(topic);
Logger.log(e.message);
}
// ======================= Build or choose sheet =================================================
var s = ss.getSheetByName(language);
if (!s) {
s = ss.insertSheet(language);
s.appendRow(["Date"]);
}
// add new topic if not there yet
if (_getIndex(s.getRange("1:1").getValues(), topic) == -1) {
lastCol = s.getLastColumn();
s.getRange(1, lastCol+1).setValue(topic);
s.getRange(2, lastCol+1).setValue('English Pages');
s.getRange(2, lastCol+2).setValue(language + ' translated');
s.getRange(2, lastCol+3).setValue(language + ' up to date');
s.getRange(1, s.getLastColumn()-2, 1, 3).mergeAcross();
}
// clear formats to prevent duplicate entries due to date formatting :/
s.getRange("A2:A").setNumberFormat('@STRING@');
// ====================== Build date ============================================
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var newdate = year + "/" + month + "/" + day;
// ======================== Append new row when that date is not yet there ==========================
if (_getIndex(s.getRange("A2:A").getValues(), newdate) == -1) {
s.appendRow([newdate]);
}
var topicColumn = _getIndex(s.getRange("1:1").getValues(), topic);
var lastRow = s.getLastRow();
s.getRange(lastRow, topicColumn + 1).setValue(data.pages.counter);
s.getRange(lastRow, topicColumn + 2).setValue(data.translations.counter);
s.getRange(lastRow, topicColumn + 3).setValue(data.updateNeeded.counter);
}} // end for loops
};
// ======================== 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