Skip to content

Instantly share code, notes, and snippets.

@charlie0228
Created December 5, 2016 11:48
Show Gist options
  • Save charlie0228/3cbc21099a4dbbc5f5fdb07561114b76 to your computer and use it in GitHub Desktop.
Save charlie0228/3cbc21099a4dbbc5f5fdb07561114b76 to your computer and use it in GitHub Desktop.
function googlesheet_json(json_link , _filter, callbackFunction) {
// 本函數為GOOGLE JSON修改,可以無視GOOGLESHEET使用JSON抓取資料需要知道第一列(ROW)內容的要求
// 利用 https://spreadsheets.google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/full 找出我們要抓起連結的工作表的真實ID (可能不是od6)
// JSON link: https://spreadsheets.google.com/feeds/list/YOUR_SPREADSHEET_ID/YOUR_WORKSSHEET_ID/public/full?alt=json
if (!json_link)
return alert("無JSON網址");
if (_filter)
json_link = json_link + "&q=" + _filter;
jQuery.support.cors = true;
$.ajax({
url: json_link,
cache: false,
dataType: "jsonp",
success: function(data) {
var getData = [];
var row_num = 0;
var count = 0;
for (var i=0; i < data.feed.entry.length; i++) {
// 計算 data.feed.entry[i]中有多少Object (因為Object 沒辦法直接使用length,Array 才行)
for (var temp in data.feed.entry[i]) {
if (data.feed.entry[i].hasOwnProperty(temp))
count++;
}
/*
Here's an update as of 2016 and widespread deployment of ES5 and beyond.
For IE9+ and all other modern ES5+ capable browsers,
you can use Object.keys() so the above code just becomes:
var size = Object.keys(myObj).length;
count = Object.keys(data.feed.entry[i]).length;
*/
row_num = count - 6; // 因為Google JSON格式,要取得的值都在最後,前六項為固定,直接省略
getData[i] = []; // 宣告二維空陣列
$.each( data.feed.entry[i], function( key, value ) {
count--;
if (count < row_num) {
getData[i][row_num-count-1] = value.$t;
}
});
}
callbackFunction(getData); // 把資料用callbackFunction的方式傳給下一個函數處理
},
error: function (request, status, error) { alert(status + ", " + error); }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment