RegExp bug in GDocs backend fixed and code refactoring
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
sandbox/*
|
sandbox/*
|
||||||
.*.swp
|
.*.swp
|
||||||
|
.*.swo
|
||||||
_site/*
|
_site/*
|
||||||
|
|||||||
@@ -31,8 +31,9 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
my.fetch = function(dataset) {
|
my.fetch = function(dataset) {
|
||||||
var dfd = $.Deferred();
|
var dfd = $.Deferred();
|
||||||
var url = my.getSpreadsheetAPIUrl(dataset.url);
|
var url = my.getSpreadsheetAPIUrl(dataset.url);
|
||||||
|
|
||||||
$.getJSON(url, function(d) {
|
$.getJSON(url, function(d) {
|
||||||
result = my.parseData(d);
|
var result = my.parseData(d);
|
||||||
var fields = _.map(result.fields, function(fieldId) {
|
var fields = _.map(result.fields, function(fieldId) {
|
||||||
return {id: fieldId};
|
return {id: fieldId};
|
||||||
});
|
});
|
||||||
@@ -42,6 +43,7 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
useMemoryStore: true
|
useMemoryStore: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return dfd.promise();
|
return dfd.promise();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -56,67 +58,66 @@ this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
|
|||||||
//
|
//
|
||||||
// Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows.
|
// Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows.
|
||||||
my.parseData = function(gdocsSpreadsheet) {
|
my.parseData = function(gdocsSpreadsheet) {
|
||||||
var options = {};
|
var options = arguments[1] || {};
|
||||||
if (arguments.length > 1) {
|
var colTypes = options.colTypes || {};
|
||||||
options = arguments[1];
|
|
||||||
}
|
|
||||||
var results = {
|
var results = {
|
||||||
fields: [],
|
fields : [],
|
||||||
records: []
|
records: []
|
||||||
};
|
};
|
||||||
// default is no special info on type of columns
|
var entries = gdocsSpreadsheet.feed.entry || [];
|
||||||
var colTypes = {};
|
var key;
|
||||||
if (options.colTypes) {
|
var colName;
|
||||||
colTypes = options.colTypes;
|
// percentage values (e.g. 23.3%)
|
||||||
}
|
var rep = /^([\d\.\-]+)\%$/;
|
||||||
if (gdocsSpreadsheet.feed.entry.length > 0) {
|
|
||||||
for (var k in gdocsSpreadsheet.feed.entry[0]) {
|
for(key in entries[0]) {
|
||||||
if (k.substr(0, 3) == 'gsx') {
|
// it's barely possible it has inherited keys starting with 'gsx$'
|
||||||
var col = k.substr(4);
|
if(/^gsx/.test(key)) {
|
||||||
results.fields.push(col);
|
colName = key.substr(4);
|
||||||
}
|
results.fields.push(colName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts non numberical values that should be numerical (22.3%[string] -> 0.223[float])
|
// converts non numberical values that should be numerical (22.3%[string] -> 0.223[float])
|
||||||
var rep = /^([\d\.\-]+)\%$/;
|
results.records = _.map(entries, function(entry) {
|
||||||
results.records = _.map(gdocsSpreadsheet.feed.entry, function(entry) {
|
|
||||||
var row = {};
|
var row = {};
|
||||||
|
|
||||||
_.each(results.fields, function(col) {
|
_.each(results.fields, function(col) {
|
||||||
var _keyname = 'gsx$' + col;
|
var _keyname = 'gsx$' + col;
|
||||||
var value = entry[_keyname]['$t'];
|
var value = entry[_keyname].$t;
|
||||||
|
var num;
|
||||||
|
|
||||||
|
// TODO decide the entry format of percentage data to be parsed
|
||||||
|
// TODO cover this part of code with test
|
||||||
|
// TODO use the regexp only once
|
||||||
// if labelled as % and value contains %, convert
|
// if labelled as % and value contains %, convert
|
||||||
if (colTypes[col] == 'percent') {
|
if(colTypes[col] === 'percent' && rep.test(value)) {
|
||||||
if (rep.test(value)) {
|
num = rep.exec(value)[1];
|
||||||
var value2 = rep.exec(value);
|
value = parseFloat(num) / 100;
|
||||||
var value3 = parseFloat(value2);
|
|
||||||
value = value3 / 100;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
row[col] = value;
|
row[col] = value;
|
||||||
});
|
});
|
||||||
|
|
||||||
return row;
|
return row;
|
||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Convenience function to get GDocs JSON API Url from standard URL
|
// Convenience function to get GDocs JSON API Url from standard URL
|
||||||
my.getSpreadsheetAPIUrl = function(url) {
|
my.getSpreadsheetAPIUrl = function(url) {
|
||||||
if (url.indexOf('feeds/list') != -1) {
|
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0
|
||||||
return url;
|
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/;
|
||||||
} else {
|
var matches = url.match(regex);
|
||||||
// https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=0
|
var key;
|
||||||
var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*/;
|
// TODO check possible worksheet options
|
||||||
var matches = url.match(regex);
|
var worksheet = 1;
|
||||||
if (matches) {
|
|
||||||
var key = matches[1];
|
if(!!matches) {
|
||||||
var worksheet = 1;
|
key = matches[1];
|
||||||
var out = 'https://spreadsheets.google.com/feeds/list/' + key + '/' + worksheet + '/public/values?alt=json';
|
url = 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json';
|
||||||
return out;
|
|
||||||
} else {
|
|
||||||
alert('Failed to extract gdocs key from ' + url);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return url;
|
||||||
};
|
};
|
||||||
}(jQuery, this.recline.Backend.GDocs));
|
}(jQuery, this.recline.Backend.GDocs));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user