// o---------------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net/license.html | // | | // | RGraph is dual-licensed under the Open Source GPL license. That means that it's | // | free to use and there are no restrictions on what you can use RGraph for! | // | If the GPL license does not suit you however, then there's an inexpensive | // | commercial license option available. See the URL above for more details. | // o---------------------------------------------------------------------------------o // // Initialise the various objects // RGraph = window.RGraph || {isrgraph:true,isRGraph: true,rgraph:true}; // Module pattern (function (win, doc, undefined) { RGraph.Sheets = function (oauth, key, worksheet, callback = null) { // Allow three args to be given as well as four if (arguments.length === 1) { // Nothing to do here } else if (arguments.length === 3) { callback = worksheet; // Need to set this because the order of the args is wrong worksheet = 'Sheet1'; } else if (arguments.length === 4) { // Nothing to do here } var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var url = ('https://sheets.googleapis.com/v4/spreadsheets/[KEY]/values/[WORKSHEET]?alt=json&key=[OAUTH_KEY]') .replace(/\[KEY\]/, key) .replace(/\[WORKSHEET\]/, encodeURIComponent(worksheet)) .replace(/\[OAUTH_KEY\]/, oauth) // // Loads the spreadsheet // this.load = function(url, userCallback) { var obj = this; RGraph.Sheets.AJAX.getJSON(url, function (json) { var grid = json.values; var rows = grid.length; var cells = 0; // // Determine the longest row // for (var i=0; i 234 for (var i=0; i=0; i--) { if (arr[i] || content) { out.push(arr[i]); content = true; } } arr = out.reverse(); return out; }; // // Makes an AJAX call. It calls the given callback (a function) when ready // // @param args object An object consisting of: // o url // o callback // OR // // @param string url The URL to retrieve // @param function callback A function that is called when the response is ready, // there's an example below called "myCallback". // RGraph.Sheets.AJAX = function (url, callback) { // Mozilla, Safari, ... if (window.XMLHttpRequest) { var httpRequest = new XMLHttpRequest(); // MSIE } else if (window.ActiveXObject) { var httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } httpRequest.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { this.__user_callback__ = callback; this.__user_callback__(this.responseText); } } httpRequest.open('GET', url, true); // Set a Cache-Control header if (httpRequest && httpRequest.setRequestHeader) { httpRequest.setRequestHeader('Cache-Control', 'no-cache'); } httpRequest.send(); }; // // Uses the above function but calls the call back passing JSON (ie a JavaScript object ) as its argument // // @param args object An object consisting of: // o url // o callback // OR // // @param url string The URL to fetch // @param callback function Your callback function (which is passed the JSON object as an argument) // RGraph.Sheets.AJAX.getJSON = function (url, callback) { RGraph.Sheets.AJAX(url, function () { var json = eval('(' + this.responseText + ')'); callback(json); }); }; // End module pattern })(window, document);