Skip to content

Instantly share code, notes, and snippets.

@neno-tech
Created August 14, 2021 06:03
Show Gist options
  • Save neno-tech/6c51e3481059e0aebd8d26b900a95ce9 to your computer and use it in GitHub Desktop.
Save neno-tech/6c51e3481059e0aebd8d26b900a95ce9 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)
}
@kenijikoong
Copy link

อาจารย์มีตัวอย่าง function edit และ delete ไหมครับ

@Phongphat-ohm
Copy link

มีตัวอย่างการ select ข้อมูลแบบมีเงื่อนไขไหมครับ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment