Last active
September 21, 2015 17:42
-
-
Save codenamezjames/47018e10a8931c790c42 to your computer and use it in GitHub Desktop.
This snippet is designed to get all sheets of a google sheets workbook and return them in json format using promises.
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
angular.module('googleSheetsDB',[]).factory('sheetsDB', function($q, $http){ | |
var config= { | |
doc:'1Ie6BFT2mBGOxtqsqq7KhfPqDVlNazsV-OW1nTXdFCDo', | |
workbook:`https://spreadsheets.google.com/feeds/worksheets/{doc}/public/full?alt=json`, | |
sheets:`https://spreadsheets.google.com/feeds/list/{doc}/{sheet}/public/values?alt=json` | |
}; | |
function _getSheets(){ | |
var def = $q.defer(); | |
$http.get(config.workbook.replace('{doc}', config.doc)) | |
.success(function(data){ | |
def.resolve(data); | |
}); | |
return def.promise; | |
} | |
function _sheetNameExstractor(){ | |
var def = $q.defer(), | |
sheetNames = {}; | |
_getSheets() | |
.then(function(data){ | |
angular.forEach(data.feed.entry, function(v, i){ | |
sheetNames[v.title.$t] = v.id.$t.substr(v.id.$t.lastIndexOf('/')+1); //pares the sheet name with the id | |
}); | |
def.resolve(sheetNames); | |
}); | |
return def.promise; | |
} | |
function _getDataSets(){ | |
var def = $q.defer(), | |
datas = []; | |
_sheetNameExstractor().then(function(sheetNames){ | |
angular.forEach(sheetNames, function(sheet,i){ | |
datas.push($http.get(config.sheets.replace('{doc}', config.doc).replace('{sheet}', sheet))); | |
}); | |
def.resolve(datas); | |
}); | |
return def.promise; | |
} | |
return function returnFinalData(cb){ | |
var def = $q.defer(); | |
_getDataSets().then(function(arr){ | |
$q.all(arr).then(function(data){ | |
if(angular.isFunction(cb)) | |
{return cb(data);} | |
else | |
{def.resolve(data);} | |
}); | |
}); | |
return def.promise; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment