Skip to content

Instantly share code, notes, and snippets.

@evansmwendwa
Created June 5, 2017 18:40
Show Gist options
  • Save evansmwendwa/e216dc4a48c4e848621d1ca52b8daf0e to your computer and use it in GitHub Desktop.
Save evansmwendwa/e216dc4a48c4e848621d1ca52b8daf0e to your computer and use it in GitHub Desktop.
Script to sync rest endpoint to Google Spreadsheets
function parseISOString(s) {
var b = s.split(/\D+/);
return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}
function pullJSON() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getActiveSheet();
var url="https://ihub.co.ke/api/opencellular/applications?token=xxxxxx"; // Paste your JSON URL here
var response = UrlFetchApp.fetch(url); // get feed
var dataAll = JSON.parse(response.getContentText());
var dataSet = dataAll;
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
var interests = '';
var interest;
for(var k = 0; k < data.interests.length; k++) {
interest = data.interests[k];
if(interest.active == true) {
interests = interests + "\n - " + interest.title;
}
}
rows.push([
data.id,
data.firstname,
data.lastname,
data.email,
data.phone,
data.institution,
data.website,
data.job_title,
data.tshirt,
interests,
parseISOString(data.created_at)
]);
}
dataRange = sheet.getRange(2, 1, rows.length, 11); // 3 Denotes total number of entites
dataRange.setValues(rows);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('OpenCellular')
.addItem('Sync Data', 'pullJSON')
.addToUi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment