飲料訂購APP|串連 GoogleSheet 菜單
Published in
May 2, 2021
串連線上 Google Sheet 更新飲料菜單
本篇為串連 GoogleSheet 菜單
🌟 本篇技術項目
・Google Sheet 資料轉 Json資料解析
・enum CodingKeys 轉化屬性名稱
ABOUT APP
總共有四個頁面:
・登入畫面
・菜單畫面:串接 Google Sheet 資料 (此篇)
・訂購畫面:DBSheet “POST” 新增資料
・訂單畫面:DBSheet “GET” 讀取資料
使用View Controller + tableView / CollectionView 製作頁面,畫面皆有設定 autoLayout。
【 MENU 菜單畫面:串接 GoogleSheet 資料 】
將 GoogleSheet 表格資料轉換為 JSON
1. 創建GoogleSheet 菜單表單,將表單「發布到網路」
2. 取得Google 表單的 JSON 資料
複製 Google 表單 網址裡的googleSheetID
https://docs.google.com/spreadsheets/d/googleSheetID/edit#gid=0
將 googleSheetID 填入 spreadsheetID
,至 safari 網址貼上 SpreadSheets API 即取得 JSON 資料。
//SpreadSheets APIhttps://spreadsheets.google.com/feeds/list/spreadsheet ID/od6/public/values?alt=json
複製 JSON 資料,貼上至 JSON Editor / POSTMAN 檢視資料
自訂資料型別 Menu
・只取需要的資料,所需資料都在 feed > entry 裡頭,資料名稱前都多了gsx$
字樣,而$t
內才是所需資料。
・為提升程式好讀性因此使用 enum CodingKeys 重新命名。
URLSession 抓取資料
URLSession 執行於背景執行緒,顯示資料則使用DispatchQueue.main
至主執行緒執行。