![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_00.jpg)
而Google表單的內容,其實是存放在Google試算表中,因此可以將Google表單想像成一個輸入界面,Google試算表則是資料庫的概念,藉由表單來將資料寫入Google試算表中,竟然Googlei試算表是資料庫的概念,那能否直接透過Google試算表,就能直接存取,不要再經由Google表單,其實是可以,只要在Google試算表中,加入一點Google Script,就能作到新增、查詢與更新,接下來梅干先來分享,如何透過Google Script新增資料到Google試算表中吧!
Step1
首先,開啟Googlei試算表,再將所需的欄位建立好。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_01.jpg)
Step2
再選「工具 / 指令碼編輯器」選項。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_02.jpg)
Step3
在進入Google Script後,先設定一下專案名稱。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_03.jpg)
Step4
這只是檔案名稱,因此可輸中文沒關係。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_04.jpg)
Step5
接著將下方的程式碼,整個貼上後,再輸入相對應的欄位,與表單ID和頁籤名稱。
- code
- source
- function doPost(e) {
- //設定相應的欄位參數
- var params = e.parameter; //這一個不要動
- var time = params.order_time;
- var no = params.order_no;
- var pay = params.order_pay;
- var name = params.order_name;
- var phone = params.order_phone;
- var email = params.order_email;
- var address = params.order_address;
- var invoice = params.order_invoice;
- var ps = params.order_ps;
- //設定sheet資訊
- var SpreadSheet = SpreadsheetApp.openById("試算表ID");
- var Sheet = SpreadSheet.getSheetByName("頁籤名稱");
- var LastRow = Sheet.getLastRow();
- //資料寫入對應欄位中
- Sheet.getRange(LastRow+1, 1).setValue(time);
- Sheet.getRange(LastRow+1, 2).setValue(no);
- Sheet.getRange(LastRow+1, 3).setValue(pay);
- Sheet.getRange(LastRow+1, 4).setValue(name);
- Sheet.getRange(LastRow+1, 5).setValue(phone);
- Sheet.getRange(LastRow+1, 6).setValue(email);
- Sheet.getRange(LastRow+1, 7).setValue(address);
- Sheet.getRange(LastRow+1, 8).setValue(invoice);
- Sheet.getRange(LastRow+1, 9).setValue(ps);
- //當資料寫入完成後,回傳資訊
- return ContentService.createTextOutput("成功");
- }
function doPost(e) {
//設定相應的欄位參數
var params = e.parameter; //這一個不要動
var time = params.order_time;
var no = params.order_no;
var pay = params.order_pay;
var name = params.order_name;
var phone = params.order_phone;
var email = params.order_email;
var address = params.order_address;
var invoice = params.order_invoice;
var ps = params.order_ps;
//設定sheet資訊
var SpreadSheet = SpreadsheetApp.openById("試算表ID");
var Sheet = SpreadSheet.getSheetByName("頁籤名稱");
var LastRow = Sheet.getLastRow();
//資料寫入對應欄位中
Sheet.getRange(LastRow+1, 1).setValue(time);
Sheet.getRange(LastRow+1, 2).setValue(no);
Sheet.getRange(LastRow+1, 3).setValue(pay);
Sheet.getRange(LastRow+1, 4).setValue(name);
Sheet.getRange(LastRow+1, 5).setValue(phone);
Sheet.getRange(LastRow+1, 6).setValue(email);
Sheet.getRange(LastRow+1, 7).setValue(address);
Sheet.getRange(LastRow+1, 8).setValue(invoice);
Sheet.getRange(LastRow+1, 9).setValue(ps);
//當資料寫入完成後,回傳資訊
return ContentService.createTextOutput("成功");
}
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_05.jpg)
語法說明:
1.doPost代表 API method 爲 post形式
2.利用 e.parameter,來分別設定每個試算表中的欄位
3.Sheet.getRange(LastRow+1, 1).setValue(order_time);這代表第一欄第一列為時間戳記,依此類推。
2.利用 e.parameter,來分別設定每個試算表中的欄位
3.Sheet.getRange(LastRow+1, 1).setValue(order_time);這代表第一欄第一列為時間戳記,依此類推。
Step6
都弄好後,再點選「發布 / 部署為網路應用程式」選項。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_06.jpg)
Step7
接著將權限,設為任何人與匿名使用者。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_07.jpg)
Step8
再點選「核對權限」鈕。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_08.jpg)
Step9
選擇Google帳號。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_09.jpg)
Step10
由於目前該程式,沒有經過Google驗證,由於是自用沒啥影響,再點左下的「進階」。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_010.jpg)
Step11
再點「前往xxx(不安全)」。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_011.jpg)
Step12
再點允許。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_012.jpg)
Step13
這樣就已將剛的程式碼發布完成了,同時下方的URL就是API呼叫的網址。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_013.jpg)
Step14
接著進到W3school TIY的平台中,來測試一下,而梅干是使用ajax方式,將資料以透過剛的API網址,將值送進去。
Ajax call API 新增資料
- code
- source
- <script src="//code.jquery.com/jquery-3.4.1.js"
- integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
- <script>
- $.ajax({
- type: "post",
- url: "剛剛取得的 API URL",
- data: {
- "order_time": $("input[name=time]").val(),
- "order_no": $("input[name=no]").val(),
- "order_pay": $("input[name=pay]").val(),
- "order_name": $("input[name=name]").val(),
- "order_phone":$("input[name=phone]").val(),
- "order_email":$("input[name=email]").val(),
- "order_address":$("input[name=address]").val(),
- "order_invoice":$("select[name=invoice]").val(),
- "order_ps":$("input[name=ps]").val()
- },
- success: function(response) {
- if(response == "成功"){
- alert("成功::::"+no);
- }
- }
- });
- }
- </script>
<script
src="//code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script>
$.ajax({
type: "post",
url: "剛剛取得的 API URL",
data: {
"order_time": $("input[name=time]").val(),
"order_no": $("input[name=no]").val(),
"order_pay": $("input[name=pay]").val(),
"order_name": $("input[name=name]").val(),
"order_phone":$("input[name=phone]").val(),
"order_email":$("input[name=email]").val(),
"order_address":$("input[name=address]").val(),
"order_invoice":$("select[name=invoice]").val(),
"order_ps":$("input[name=ps]").val()
},
success: function(response) {
if(response == "成功"){
alert("成功::::"+no);
}
}
});
}
</script>
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_014.jpg)
Step15
都好了之後,再按上方的「執行代碼」,當資料寫入成功時,則會出現成功的訊息。
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_015.jpg)
Step16
接著再到Google試算表來檢查一下,哈~資料果真成功的寫入啦!竟然資料可寫入,當然也可讀取囉!下回再與大家分享,如何讀取試算表中的資料吧!
![梅問題-[教學] 利用 Google Script 將 Google Sheet試算表,變成簡易資料庫使用(寫入篇)](http://photo.minwt.com/img/Content/webdesign/google-sheet-api-insert-data/google-sheet-gs-insert-data_016.jpg)