Skip to content

Commit 8837ad6

Browse files
committed
Uses v4 Sheets API to replace deprecated v3 API
1 parent af08c9b commit 8837ad6

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

views/index.jade

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,15 @@ block script
125125
var foldr_histories = JSON.parse(localStorage.getItem("hackfoldr")) || [];
126126
var foldr_scale = JSON.parse(localStorage.getItem("hackfoldr-scale")) || "";
127127

128+
// API key from @mrorz in g0v slack
129+
var GOOGLE_API_KEY = 'AIzaSyCQB8Ufy4k-PpYSq0wUoMSj_WtTyuLgIRY';
130+
128131
// check where the csv will come from, ethercalc or gsheet?
129132
if(ethercalc_name.length < 40){
130133
csv_api_source = 'https://ethercalc-cache.g0v.tw/_/'+ethercalc_name+'/csv';
131134
csv_api_source_type = "ethercalc";
132135
}else{
133-
csv_api_source = 'docs.google.com/feeds/download/spreadsheets/Export?key='+ethercalc_name+'&exportFormat=csv&gid=0';
136+
csv_api_source = 'https://sheets.googleapis.com/v4/spreadsheets/'+ethercalc_name+'/values/A1:D?key='+GOOGLE_API_KEY;
134137
csv_api_source_type = "google";
135138
// because posting to gsheet is hard to implement, we don't offer submit feature when using gsheet. since it's easy to moderate editing in gsheet, you can just let users edit or comment on it.
136139
$("#topbar .add.to.list, #topbar .submit.segment").remove();
@@ -182,7 +185,7 @@ block script
182185
// jump from ethercalc to google spreadsheet when A1 is filled with a gsheet id
183186
if(csv_api_source_type == "ethercalc" && !rows[0][0].match(/^#/) && rows[0][0].length >= 40){
184187
// reset all related variables and compile again
185-
csv_api_source = 'docs.google.com/feeds/download/spreadsheets/Export?key='+rows[0][0]+'&exportFormat=csv&gid=0';
188+
csv_api_source = 'https://sheets.googleapis.com/v4/spreadsheets/'+rows[0][0]+'/values/A1:D?key='+GOOGLE_API_KEY;
186189
csv_api_source_type = "google";
187190
csv_api_source_id = rows[0][0].trim();
188191
// remember? we don't support submit forms with gsheet. so need to remove the + button as well as the form segment.
@@ -610,26 +613,15 @@ block script
610613
// compile ethercalc csv
611614
$.get(csv_api_source).pipe(CSV.parse).done(compile_json);
612615
}else{
613-
// compile gsheet-tabletop json
614-
Tabletop.init({
615-
key: csv_api_source_id,
616-
callback: function(data, tabletop){
617-
var columnNames = tabletop.models[tabletop.model_names[0]].column_names
618-
data = data.map(function (o) {
619-
var result = ['', '', '', '', '']
620-
621-
columnNames.forEach(function(columnName, idx){
622-
if(o[columnName]) {
623-
result[idx] = o[columnName];
624-
}
625-
});
626-
627-
return result;
616+
// compile google sheet
617+
$.getJSON(csv_api_source).done(function (resp) {
618+
return compile_json(resp.values.map(function (row) {
619+
// Pad empty columns with '' so that each row contains exactly 4 items
620+
return ['', '', '', ''].map( function (_, i) {
621+
return typeof row[i] === 'undefined' ? '' : row[i];
628622
});
629-
compile_json(data);
630-
},
631-
simpleSheet: true
632-
});
623+
}));
624+
})
633625
}
634626
};
635627

0 commit comments

Comments
 (0)