Skip to content

Instantly share code, notes, and snippets.

@gatesakagi
Last active November 14, 2022 17:30
Show Gist options
  • Save gatesakagi/440d627f84fb9c90755d1caab36bc1dc to your computer and use it in GitHub Desktop.
Save gatesakagi/440d627f84fb9c90755d1caab36bc1dc to your computer and use it in GitHub Desktop.
Google App Script (讀取QueryString並寫入Sheet)
function doGet(e) {
//JSON.stringify(name)
if(typeof e !== 'undefined') {
var params = e.parameter;
var name = params.name;
var drinkIndex = params.drinkindex;
var drink = params.drink;
var drinkPrice = params.drinkprice;
var sugar = params.sugar;
var ice = params.ice;
var note = params.note;
var orderDatetime = params.orderdatetime;
}
//將Sheet指定為"資料庫"試算表 SpreadSheet=試算表
var SpreadSheet = SpreadsheetApp.openById("<您要寫入Google Sheets的Key值");
//取得頁籤"工作表1" Sheet = 頁籤
var Sheet = SpreadSheet.getSheetByName("drinkOrder")
//取得有資料的最後一行的"行數"(目的要在最後一行插入新資料)
var LastRow = Sheet.getLastRow();
//--開始寫入資料--
//在最後一行的下一行寫入資料
if(typeof name !== "undefined" && typeof drinkIndex !== "undefined" && typeof drink !== "undefined" && typeof drinkPrice !== "undefined" && typeof sugar !== "undefined" && typeof ice !== "undefined" && typeof orderDatetime !== "undefined" && name !="" && drinkIndex !="" && drink !="" && drinkPrice !="" && sugar !="" && ice !="" && orderDatetime !="") {
//首先寫入name
Sheet.getRange(LastRow+1, 1).setValue(name); //意即最後一行的加一行處,左邊數來第一格,寫入數值為name
//寫入drinkIndex
Sheet.getRange(LastRow+1, 2).setValue(drinkIndex); //意即最後一行的加一行處,左邊數來第二格,寫入數值為drinkIndex
//寫入drink
Sheet.getRange(LastRow+1, 3).setValue(drink); //意即最後一行的加一行處,左邊數來第三格,寫入數值為drink
//寫入drinkPrice
Sheet.getRange(LastRow+1, 4).setValue(drinkPrice); //意即最後一行的加一行處,左邊數來第三格,寫入數值為drinkPrice
//寫入sugar
Sheet.getRange(LastRow+1, 5).setValue(sugar); //意即最後一行的加一行處,左邊數來第三格,寫入數值為sugar
//寫入ice
Sheet.getRange(LastRow+1, 6).setValue(ice); //意即最後一行的加一行處,左邊數來第三格,寫入數值為ice
//寫入note
Sheet.getRange(LastRow+1, 7).setValue(note); //意即最後一行的加一行處,左邊數來第三格,寫入數值為note
//寫入orderDatetime
Sheet.getRange(LastRow+1, 8).setValue(orderDatetime); //意即最後一行的加一行處,左邊數來第三格,寫入數值為orderDatetime*/
//寫入結束後傳回true
return ContentService.createTextOutput('true');
} else {
return ContentService.createTextOutput('false');
}
}
function testGET() {
var queryString = "?name=&type=get";
var url = ScriptApp.getService().getUrl() + queryString;
var options =
{
"method" : "GET",
"followRedirects" : true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment