Skip to content

Instantly share code, notes, and snippets.

@songwutk
Forked from neno-tech/code1.gs
Created August 14, 2021 15:05
Show Gist options
  • Save songwutk/80edffbb1d8e23342ba213a56bfc131a to your computer and use it in GitHub Desktop.
Save songwutk/80edffbb1d8e23342ba213a56bfc131a to your computer and use it in GitHub Desktop.
สร้าง API จาก GOOGLE SHEET และสร้าง API สำหรับเพิ่มข้อมูลลงใน GOOGLE SHEET
var ss = SpreadsheetApp.openById('xxx')
var sheet = ss.getSheetByName('xxx')
function doGet(e) {
var action = e.parameter.action
if (action == 'getUsers') {
return getUsers(e)
}
}
function getUsers(e) {
var rows = sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues()
var data =[]
for(var i=0; i<rows.length;i++){
var row = rows[i]
var record = {}
record['Name']=row[0]
record['nickName']=row[1]
record['Phone']=row[2]
record['Email']=row[3]
data.push(record)
}
var result = JSON.stringify(data)
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON)
}
var ss = SpreadsheetApp.openById('xxx')
var sheet = ss.getSheetByName('xxx')
function doPost(e) {
var action = e.parameter.action
if (action == 'addUser') {
return addUser(e)
}
}
function addUser(e) {
var user = JSON.parse(e.postData.contents)
sheet.appendRow([user.name, user.nickName, user.phone, user.email])
return ContentService.createTextOutput("success").setMimeType(ContentService.MimeType.TEXT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment