-
-
Save songwutk/80edffbb1d8e23342ba213a56bfc131a to your computer and use it in GitHub Desktop.
สร้าง API จาก GOOGLE SHEET และสร้าง API สำหรับเพิ่มข้อมูลลงใน GOOGLE SHEET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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