**假設現在有個Google試算表,想要把裡面的資料匯出成Json格式方便程式存取,該怎麼做呢?** ![未命名](https://hackmd.io/_uploads/r1Z4rEKIR.png) --- **步驟1:點擊上方「擴充功能」→「Apps Script」** ![未命名](https://hackmd.io/_uploads/BkKiS4KIA.png) **會出現這個畫面** ![未命名](https://hackmd.io/_uploads/H1lkU4tIA.png) **步驟2:把程式碼.gs裡面的程式改為以下程式碼** ``` var content = ""; function ExportJson_Data() { //取得所有試算表頁籤 var sheetList = SpreadsheetApp.getActiveSpreadsheet().getSheets(); for(var i =0; i < sheetList.length; i++) { var sheet = sheetList[i]; var sheetName = sheet.getSheetName(); switch(sheetName){ case "匯出範例": //要匯出的頁籤名稱 templateSheet = sheet; ExportJson_SetData(templateSheet); break; } } } function ExportJson_SetData(sheet){ //從第二列第一欄(A2)開始取資料 var dataRange = sheet.getRange(2,1, sheet.getMaxRows(), sheet.getMaxColumns()); var arrData = dataRange.getValues(); content += "[\n"; for(var i=0; i<arrData.length-1; i++){ if(arrData[i][0] != ""){ content += " {\n"; content += " \"Id\":\"" + arrData[i][0] + "\",\n"; content += " \"LevelBegin\":\"" + arrData[i][1] + "\",\n"; content += " \"LevelEnd\":\"" + arrData[i][2] + "\",\n"; content += " \"Bet1\":\"" + arrData[i][3] + "\"\n"; if(arrData[i+1][0] == ""){ content += " }\n"; break; }else{ content += " },\n"; } } } content += "]"; openOutputHTML() } function openOutputHTML(){ res = 'exportHtml'; var template = HtmlService.createTemplateFromFile(res); template.jsonGetter = ""; template.jsonformetCb = ""; template.nameSpace = ""; var html = template.evaluate(); SpreadsheetApp.getUi().showModalDialog(html, res); } ``` **步驟3:增加一個HTML檔案,命名為「exportHtml」** ![未命名](https://hackmd.io/_uploads/Syit9SKUA.png) ![未命名](https://hackmd.io/_uploads/Bk4q5SY8R.png) **步驟4:把exportHtml.html裡面的程式改為以下程式碼** ``` <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> 點擊下方方框內文字便自動複製到剪貼簿 <textarea id="Content" style="overflow:hidden" rows="15" cols="60" onClick="selectAllAndCopy('Content')"><?=content?></textarea> </body> </html> <script> function selectAllAndCopy(id){ document.getElementById(id).focus(); document.getElementById(id).select(); try { document.execCommand('Copy'); showToast('copy ' + id); } catch(e){ //to nothing } } function showToast(msg){ var toastBar = document.getElementById("toastBar") toastBar.innerHTML = msg; toastBar.className = "show"; setTimeout(function(){ toastBar.className = toastBar.className.replace("show", ""); }, 1000); } </script> ``` **步驟5:儲存** ![未命名](https://hackmd.io/_uploads/B1sGoBYUC.png) **步驟6:回到試算表畫面,點擊上方「插入」→「繪圖」** ![未命名](https://hackmd.io/_uploads/SJ-isSKIC.png) **步驟7:增加一個圓角矩形的圖案** ![未命名](https://hackmd.io/_uploads/r1C1nrtIR.png) **步驟8:點圖案右鍵,選「編輯文字」,輸入「匯出」(或其他你想顯示的文字),儲存並關閉** ![未命名](https://hackmd.io/_uploads/Bkbd2BKU0.png) **步驟9:畫面上會增加那個按鈕,把它拉到你想要它顯示的位置** ![未命名](https://hackmd.io/_uploads/r1sahHFUR.png) **步驟10:點按鈕右鍵,右邊會出現三個點,點擊後選擇「指派指令碼」** ![未命名](https://hackmd.io/_uploads/SkuzprYLR.png) **步驟11:輸入「ExportJson_Data」按確定** ![未命名](https://hackmd.io/_uploads/SkqtarKUA.png) **步驟12:點擊匯出按鈕,等待程式執行完畢跳出執行結果** **※第一次執行或偵錯時,會跳出需要授權畫面,點審查權限一路點確定就好** ![未命名](https://hackmd.io/_uploads/B1xxAPEtUA.png) ![未命名](https://hackmd.io/_uploads/r14TJIYLR.png) **步驟13:開啟[JSON Editor Online](https://jsoneditoronline.org/),將結果複製過去,查看格式是否正確** ![未命名](https://hackmd.io/_uploads/HJZMgLYU0.png) **步驟14:新增一行資料或者修改目前的內容,再執行一次查看結果是否正確** ![未命名](https://hackmd.io/_uploads/H1S4ZIFLC.png) **※該匯出方式是完全由字串的方式來組出Json格式,  若想組成其他格式,直接調整這一段程式碼,試圖組成你要的格式即可** ``` for(var i=0; i<arrData.length-1; i++){ if(arrData[i][0] != ""){ content += " {\n"; content += " \"Id\":\"" + arrData[i][0] + "\",\n"; content += " \"LevelBegin\":\"" + arrData[i][1] + "\",\n"; content += " \"LevelEnd\":\"" + arrData[i][2] + "\",\n"; content += " \"Bet1\":\"" + arrData[i][3] + "\"\n"; if(arrData[i+1][0] == ""){ content += " }\n"; break; }else{ content += " },\n"; } } } ```