[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"blog-gasgoogle-app-script-上與資料表溝通的方式":3},{"id":4,"slug":5,"title":6,"description":7,"content":8,"date":9,"image":10,"imageAlt":6,"author":11,"categories":14,"path":17,"minRead":18},3556,"gasgoogle-app-script-上與資料表溝通的方式","GAS(google app script) 上與資料表溝通的方式","GAS(google app script) 上與資料表溝通的方式 上次使用 GAS 部署網頁後，進階做法就是 […]","\u003Ch1>GAS(google app script) 上與資料表溝通的方式\u003C\u002Fh1>\n\u003Cp>上次使用 GAS 部署網頁後，進階做法就是能透過 GAS 結合 google sheet 建置一個簡易的後端存取資料系統，最重要的問題就是---要如何溝通？\u003C\u002Fp>\n\u003Cp>我摸索出兩種做法，一種是使用 doPost 或是 doGet ，將 GAS 直接作為溝通工具。另一種做法是呼叫 google script，就看哪種方法剛好符合需求直接用就可以。\u003C\u002Fp>\n\u003Ch2>doPost\u003C\u002Fh2>\n\u003Cp>其實就如同名稱，使用 doPost 就會使用 POST 方式，如果換成 doGet 就會使用 GET 方式。使用方式很簡單：\u003Cbr \u002F>\n先在 gs 檔中加上\u003C\u002Fp>\n\u003Ch3>xxx.gs\u003C\u002Fh3>\n\u003Cpre>\u003Ccode>function doPost(e) {\n  var param = e.parameter;\n  var name = param.name;\n  var replyMessege = &#039;Your nane：&#039; + name ;\n  return ContentService.createTextOutput(replyMessege);\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>然後進行部署，之後會得到一個類似的位置\u003C\u002Fp>\n\u003Cpre>\u003Ccode>https:\u002F\u002Fscript.google.com\u002Fmacros\u002Fs\u002F&lt;token&gt;\u002Fexec\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>對這個位置發送 POST 就可以囉～大概會是這種感覺\u003C\u002Fp>\n\u003Cpre>\u003Ccode>$.post(&#039;部署網址&#039;,{\n    name:&#039;billxu&#039;,\n},function(e){\n    console.log(e); \u002F\u002F\u002F Your name : billxu\n});\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>這種方法就能將 GAS 視為一個簡易後端 API \u003C\u002Fp>\n\u003Ch2>google.script\u003C\u002Fh2>\n\u003Cp>這個方式比較粗暴，簡單來說就是使用部署後的網頁直接呼叫 gs 檔的函式，簡單來說像這樣寫\u003C\u002Fp>\n\u003Cpre>\u003Ccode>google.script.run\n          .withSuccessHandler()\n          .customFunction(input);\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>然後在 gs 檔裡宣告對應的函式名稱\u003C\u002Fp>\n\u003Ch3>xxx.gs\u003C\u002Fh3>\n\u003Cpre>\u003Ccode>function customFunction (input) {\n\u002F\u002Fdo something\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>這樣就可以在部署的網頁呼叫 GAS 中的功能囉～也可以傳入資料，缺點就是不像 POST 可以回傳執行結果，這點要注意。\u003C\u002Fp>\n","2023-10-15T10:11:51","",{"name":12,"link":13},"Chih Fan","http:\u002F\u002Fpost.billxu.net",[15],{"name":16,"slug":16},"frontend","\u002Fblog\u002Fgasgoogle-app-script-上與資料表溝通的方式",1]