สร้าง webapp โดยใข้ google appscript ผ่าน Restful api โดยใช้ php ในฟอร์ม อัพเดทแก้ไข ขึ้น Error รบกวนผู้รู้หน่อยครับ
Web Application ด้วย Google App Script | Sheet RESTFul API ในฟอร์ม อัพเดทข้อมูลขึ้น แบบนี้หน่ะครับ รบกวนผู้รู้หน่อย
Parse error: syntax error, unexpected end of file in /storage/ssd4/621/15852621/public_html/test/form_insert2.php on line 31
ข้อมูล มี 2 ไฟล์ คือ code.gs , ไฟล์ php ด้านล่าง
ีurl : https://mtb21.000webhostapp.com/test/show.php
code.gs ใน appscript มีดังนี้
var db = SpreadsheetApp.openById("154eMzZu-Du4HyVFnxCs3_QNyiqkcJHEVj2xps_IWYFQ");
var SCRIPT_PROP = PropertiesService.getScriptProperties();
function doGet(e){
return handleResponse(e);
}
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var lock = LockService.getPublicLock();
lock.waitLock(30000);
try {
var action = e.parameter.action;
var sheet_name = e.parameter.sheet_name;
if (action == 'selects'& sheet_name == '350') {
var sheetUsers = db.getSheetByName('350');
return selects(e,sheetUsers);
}
else if (action == 'insert' & sheet_name == '350') {
var sheetUsers = db.getSheetByName('350');
return insert(e,sheetUsers);
}
else if (action == 'edit' & sheet_name == '350') {
var sheetUsers = db.getSheetByName('350');
return edit(e,sheetUsers);
}
else if (action == 'delete'& sheet_name == '350') {
var sheetUsers = db.getSheetByName('350');
return del(e,sheetUsers);
}
}
catch(e){
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}
function selects(e, sheetObject)
{
var data = {};
data = _readData(sheetObject);
return response().json(data);
}
function insert(e,sheetUsers) {
var requestJSON = e.postData.contents;
var id = JSON.parse(requestJSON).id;
var name = JSON.parse(requestJSON).name;
var age = JSON.parse(requestJSON).age;
var Row = sheetUsers.getLastRow();
var idTemp = sheetUsers.getRange(Row+1, 1).getValue();
var rowData = sheetUsers.appendRow([
Row+1,
id,
name,
age
]);
var result = "บันทึกข้อมูลเรียบร้อยแล้ว";
return response().json(result)
}
function edit(e,sheetUsers) {
var requestJSON = e.postData.contents;
var num = JSON.parse(requestJSON).num;
var id = JSON.parse(requestJSON).id;
var name = JSON.parse(requestJSON).name;
var age = JSON.parse(requestJSON).age;
var Row = sheetUsers.getLastRow();
for (var i = 1; i <= Row; i++) {
var idTemp = sheetUsers.getRange(i, 1).getValue();
if(num==idTemp){
var rowData =[[num,id,name,age]];
sheetUsers.getRange(idTemp,1,1,4).setValues(rowData);
}
}
var result = "แก้ไขข้อมูลเรียบร้อยแล้ว";
return response().json(result)
}
function del(e,sheetUsers) {
var requestJSON = e.postData.contents;
var num = JSON.parse(requestJSON).num;
var Row = sheetUsers.getLastRow();
for (var i = 1; i <= Row; i++) {
var idTemp = sheetUsers.getRange(i, 1).getValue();
if (idTemp == num) {
sheetUsers.deleteRow(i);
}
}
var result = "ลบข้อมูลเรียบร้อยแล้ว";
return response().json(result)
}
function _readData(sheetObject, properties) {
if (typeof properties == "undefined") {
properties = _getHeaderRow(sheetObject);
properties = properties.map(function (p) {
return p.replace(/\s+/g, '_');
});
}
var rows = _getDataRows(sheetObject),
data = [];
for (var r = 0, l = rows.length; r < l; r++) {
var row = rows[r],
record = {};
for (var p in properties) {
record[properties[p]] = row[p];
}
data.push(record);
}
return data;
}
function _getDataRows(sheetObject) {
var sh = sheetObject;
return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
}
function _getHeaderRow(sheetObject) {
var sh = sheetObject;
return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
}
function response() {
return {
json: function(data) {
return ContentService
.createTextOutput(JSON.stringify(data))
.setMimeType(ContentService.MimeType.JSON);
}
}
}
ข้อมูล ไฟล์ php มีดังนี้
<!DOCTYPE html>
<html>
<body>
<h2>API GOOGLE APPSCRIPT</h2>
<?php
$num = $_GET['num'];
$url = 'https://script.google.com/macros/s/AKfycbwZn4huPHxX5HqT8pgmVNpFaKec7LcLKNz20gQfNdqcnJRL0BY/exec?action=selects&sheet_name=350' ; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed
?>
<form action="edit_save.php?num=<?php echo $num ?>" name="form1" method="post">
<?php foreach ($characters as $character) {
if($character->num == $num){
?>
<label for="fname">First name:</label><br>
<input type="text" name="id" value="<?php echo $character->id?>"><br>
<label for="lname">Last name:</label><br>
<input type="text" name="name" value="<?php echo $character->name?>"><br><br>
<label for="age">age:</label><br>
<input type="text" name="age" value="<?php echo $character->age?>"> <br><br>
<? } }?>
<input type="submit" value="Submit">
</form>
</body>
</html>
ขอบพระคุณล่วงหนี้าTag : PHP
Date :
2021-01-07 22:26:49
By :
andersonpokhao
View :
2587
Reply :
7
อันนี้ไฟล์ form_insert2.php ครับ
Code (PHP)
<!DOCTYPE html>
<html>
<body>
<h2>API GOOGLE APPSCRIPT</h2>
<?php
$num = $_GET['num'];
$url = 'https://script.google.com/macros/s/AKfycbwZn4huPHxX5HqT8pgmVNpFaKec7LcLKNz20gQfNdqcnJRL0BY/exec?action=selects&sheet_name=350' ; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed
?>
<form action="edit_save.php?num=<?php echo $num ?>" name="form1" method="post">
<?php foreach ($characters as $character) {
if($character->num == $num){
?>
<label for="fname">First name:</label><br>
<input type="text" name="id" value="<?php echo $character->id?>"><br>
<label for="lname">Last name:</label><br>
<input type="text" name="name" value="<?php echo $character->name?>"><br><br>
<label for="age">age:</label><br>
<input type="text" name="age" value="<?php echo $character->age?>"> <br><br>
<? } }?>
<input type="submit" value="Submit">
</form>
</body>
</html>
Date :
2021-01-08 11:01:31
By :
andersonpokhao
แท็กเปิด php <? เขาเลิกใช้กันมาเกือบสิบปีแล้วนะครับ
Date :
2021-01-08 11:10:49
By :
mr.v
แก้ไขตามคำแนะนำแล้วยังไม่ได้เลยครับ
Date :
2021-01-09 09:02:14
By :
andersonpokhao
ได้ แล้วครับ ขอบคุณ mr.V ครับ
Date :
2021-01-09 10:59:55
By :
andersonpokhao
Load balance : Server 00