Google Spreadsheets for Meteor
Google Spreadsheets for Meteor
meteor add ongoworks:google-spreadsheets
Provides a way to pull a published, public google spreadsheet into a cache collection.
Client:
Meteor.call "spreadsheet/fetch","<spreadsheet key>"
spreadsheetData = GASpreadsheet.findOne({spreadsheet:'<spreadsheet name or number>'})
if spreadsheetData
for index,row of spreadsheetData.cells
if ( row[1] ) then value = row[1].value
...
Or you could call on server:
if ( Meteor.is_server ) {
Meteor.startup(function () {
Meteor.setInterval(function() {
Meteor.call('spreadsheet/fetch', key, worksheet, range, rowOneHeader)
},50000);
});
}
GoogleSpreadsheets
GoogleClientLogin
See:
Used as a cache for results GASpreadsheet
Provides a way to push the data from any collection to a Google spreadsheet, which can be either public or private. Then you can also make changes in the spreadsheet and pull them back, overwriting the data in the collection with the data from the spreadsheet.
The collection must have a simple-schema attached using the collection2
package.
The collection must have a simple structure with only top-level fields that are strings, numbers, etc. There is no sub-object or array support yet.
google-spreadsheets
package to your Meteor app.There are only three things you need to do to prep the spreadsheet:
The package creates two server methods. Currently these do not do all of the work of integrating with your collection. Instead, you can make your own server methods to do that. Here are some example methods you could create in your app:
pullAllSteps: function () {
var spreadsheetName = 'Steps'; // must match exactly the name you gave your Google spreadsheet
var serviceEmail = '795073958503-qukpg8tt7vbsjqtufgc379ag24200fr3@developer.gserviceaccount.com'; // this is fake; replace with your own
var result = Meteor.call("spreadsheet/fetch2", spreadsheetName, "1", {email: serviceEmail});
// Remove all existing
Steps.remove({});
// Gather property names
var propNames = {};
_.each(result.rows, function (rowCells, rowNum) {
var doc = {};
_.each(rowCells, function (val, colNum) {
if (+rowNum === 1) {
propNames[colNum] = val;
} else {
var propName = propNames[colNum];
if (propName) {
doc[propName] = val;
}
}
});
if (+rowNum > 1) {
Steps.insert(doc);
}
});
},
writeAllSteps: function () {
var spreadsheetName = 'Steps'; // must match exactly the name you gave your Google spreadsheet
var serviceEmail = '795073958503-qukpg8tt7vbsjqtufgc379ag24200fr3@developer.gserviceaccount.com'; // this is fake; replace with your own
var obj = {};
obj[1] = {}
var colPropNames = {};
var col = 1;
_.each(Steps.simpleSchema().schema(), function (def, key) {
obj[1][col] = key;
colPropNames[key] = col;
col++;
});
var row = 2;
Steps.find().forEach(function (step) {
obj[row] = {};
_.each(step, function (val, prop) {
var pCol = colPropNames[prop];
if (!pCol)
return;
obj[row][pCol] = val.toString();
});
row++;
});
Meteor.call("spreadsheet/update", spreadsheetName, "1", obj, {email: serviceEmail});
}
TODO - the above methods could be pulled into the package methods, just passing in the collection object.
So if you are building an app that needs to pull or push content from any unknown spreadsheets that are owned by any random users, you simply need to ask the user what the name of the spreadsheet is, and then tell the user to share that spreadsheet with your service account email address. (XXX not sure about potential name contention?)