Last active
August 29, 2015 14:10
-
-
Save spalladino/307d8f69698eb1c55545 to your computer and use it in GitHub Desktop.
Google Spreadsheets InSTEDD Hub
This file contains 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
/** | |
* Adds a custom menu to the active spreadsheet, containing a single menu item | |
* for invoking the readRows() function specified above. | |
* The onOpen() function, when defined, is automatically invoked whenever the | |
* spreadsheet is opened. | |
* For more information on using the Spreadsheet API, see | |
* https://developers.google.com/apps-script/service_spreadsheet | |
*/ | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "Load My Verboice Contacts", | |
functionName : "writeMyVerboiceContacts" | |
}]; | |
spreadsheet.addMenu("Script Center Menu", entries); | |
}; | |
/** | |
* Loads a JSON file from an InSTEDD server via a GET to the specified URL using basic auth | |
*/ | |
function loadInsteddJSON(url) { | |
var jsondata = UrlFetchApp.fetch(url, { headers: { 'Authorization': insteddBasicAuth() }}); | |
return JSON.parse(jsondata.getContentText()); | |
} | |
/** | |
* Creates a site in resourcemap | |
* @collectionId: Id of the collection, find out by visiting http://resourcemap.instedd.org/api/collections.json | |
* @name: Name of the site to create | |
* @properties: Map with fieldId:value pairs, find out field Ids by visiting http://resourcemap.instedd.org/api/collections/COLLECTIONID/layers.json | |
* @lat: Latitude where this site is located | |
* @lng: Longitude where this site is located | |
*/ | |
function createResmapSite(collectionId, name, properties, lat, lng) { | |
var url = 'http://resourcemap.instedd.org/api/collections/' + collectionId + '/sites'; | |
var jsondata = UrlFetchApp.fetch(url, { | |
method: 'post', | |
headers: { 'Authorization': insteddBasicAuth(), 'Content-Disposition': 'form-data' }, | |
payload: 'site=' + JSON.stringify({name: name, lat: lat, lng: lng, "properties": properties || {}}) | |
}); | |
return JSON.parse(jsondata.getContentText()); | |
} | |
/** | |
* Creates a basic auth header based on properties instedd_user and instedd_pass | |
*/ | |
function insteddBasicAuth() { | |
var user = UserProperties.getProperty("instedd_user"); | |
var pass = UserProperties.getProperty("instedd_pass"); | |
return 'Basic ' + Utilities.base64Encode(user + ':' + pass, Utilities.Charset.UTF_8); | |
} | |
// Sample: write Verboice contacts to a sheet named 'verboice' | |
function writeMyVerboiceContacts(projectId) { | |
var contacts = loadInsteddJSON("https://verboice.instedd.org/api/projects/"+projectId+"/contacts.json"); | |
var sheet = SpreadsheetApp.getActive().getSheetByName('verboice'); | |
sheet.clearContents(); | |
for (var i=0; i < contacts.length; i++) { | |
var contact = contacts[i]; | |
sheet.appendRow([contact.addresses[0], contact.vars.rating]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment