พอดีผมอยากทำเว็บกรอกข้อมูลแล้ว เก็บข้อมูลไว้ที่ google sheet คล้ายฐานข้อมูล "ผมติดตรง เพิ่ม ตารางชื่อ green ใน googe sheet App Script " พอผมกรอกข้อมูลที่เว็บ html มันไม่ส่ง ข้อมูลของ green มาที่ google sheet ต้อง แก้โค้ดจุดไหนบ้างครับ ) โค้ชนี้ผมนำมาจากเว็ป https://www.crazycodersclub.com/appscript/crud-operation-on-google-spread-sheet-using-google-app-script-html-jquery/ มาทำเพิ่มอีกที ช่วยหน่อยนะครับ Code (JavaScript)
function doGet(e){
var op = e.parameter.action;
var ss=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1LMYfKxNonTxEK_ZzBmK0P4EV_tXC_MSylIxljGKE7Kw/edit#gid=1772958990");
var sheet = ss.getSheetByName("Sheet1");
if(op=="insert")
return insert_value(e,sheet);
//Make sure you are sending proper parameters
if(op=="read")
return read_value(e,ss);
if(op=="update")
return update_value(e,sheet);
if(op=="delete")
return delete_value(e,sheet);
}
//Recieve parameter and pass it to function to handle
function insert_value(request,sheet){
var id = request.parameter.id;
var country = request.parameter.name;
var green = request.parameter.green;
var flag=1;
var lr= sheet.getLastRow();
for(var i=1;i<=lr;i++){
var id1 = sheet.getRange(i, 3).getValue();
if(id1==id){
flag=0;
var result="Id already exist..";
} }
//add new row with recieved parameter from client
if(flag==1){
var d = new Date();
var currentTime = d.toLocaleString();
var rowData = sheet.appendRow([currentTime,id,country,green]);
var result="Insertion successful";
}
result = JSON.stringify({
"result": result
});
return ContentService
.createTextOutput(request.parameter.callback + "(" + result + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
function read_value(request,ss){
var output = ContentService.createTextOutput(),
data = {};
//Note : here sheet is sheet name , don't get confuse with other operation
var sheet="sheet1";
data.records = readData_(ss, sheet);
var callback = request.parameters.callback;
if (callback === undefined) {
output.setContent(JSON.stringify(data));
} else {
output.setContent(callback + "(" + JSON.stringify(data) + ")");
}
output.setMimeType(ContentService.MimeType.JAVASCRIPT);
return output;
}
function readData_(ss, sheetname, properties) {
if (typeof properties == "undefined") {
properties = getHeaderRow_(ss, sheetname);
properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); });
}
var rows = getDataRows_(ss, sheetname),
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_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(3, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
}
function getHeaderRow_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(1, 1, 1, 1, sh.getLastColumn()).getValues()[0];
}
//update function
function update_value(request,sheet){
var output = ContentService.createTextOutput();
var id = request.parameter.id;
var flag=0;
var country = request.parameter.name;
var lr= sheet.getLastRow();
for(var i=1;i<=lr;i++){
var rid = sheet.getRange(i, 3).getValue();
if(rid==id){
sheet.getRange(i,4).setValue(country);
var result="value updated successfully";
flag=1;
}
}
if(flag==0)
var result="id not found";
result = JSON.stringify({
"result": result
});
return ContentService
.createTextOutput(request.parameter.callback + "(" + result + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
function delete_value(request,sheet){
var output = ContentService.createTextOutput();
var id = request.parameter.id;
var country = request.parameter.name;
var flag=0;
var lr= sheet.getLastRow();
for(var i=1;i<=lr;i++){
var rid = sheet.getRange(i, 3).getValue();
if(rid==id){
sheet.deleteRow(i);
var result="value deleted successfully";
flag=1;
}
}
if(flag==0)
var result="id not found";
result = JSON.stringify({
"result": result
});
return ContentService
.createTextOutput(request.parameter.callback + "(" + result + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
Code (PHP)
อันนี้ส่วน html
Code (PHP)
<html>
<head>
<style>
table, th, td
{
margin:10px 0;
border:solid 1px #333;
padding:2px 4px;
font:15px Verdana;
}
th {
font-weight:bold;
}
#loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid blue;
border-bottom: 16px solid blue;
width: 60px;
height: 60px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
visibility:hidden;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.cl {
color: #F00;
}
#b1 {
color: #00F;
}
#b1 #b1 {
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.BG {
color: #FF0;
}
.sz {
font-size: 18px;
}
.t {
color: #0F0;
font-family: Arial, Helvetica, sans-serif;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
var script_url = "https://script.google.com/macros/s/AKfycbxmHn7Foqb3l_jwFYvUeoVIGHTn8BsGoJIm5uHMq_x9gT8233E6/exec";
// Make an AJAX call to Google Script
function insert_value() {
$("#re").css("visibility","hidden");
document.getElementById("loader").style.visibility = "visible";
$('#mySpinner').addClass('spinner');
var id1= $("#id").val();
var name= $("#name").val();
var green= $("#green").val();
var url = script_url+"?callback=ctrlq&name="+name+"&id="+id1+"&green="+green+"&action=insert";
var request = jQuery.ajax({
crossDomain: true,
url: url ,
method: "GET",
dataType: "jsonp"
});
}
function update_value(){
$("#re").css("visibility","hidden");
document.getElementById("loader").style.visibility = "visible";
var id1= $("#id").val();
var name= $("#name").val();
var url = script_url+"?callback=ctrlq&name="+name+"&id="+id1+"&action=update";
var request = jQuery.ajax({
crossDomain: true,
url: url ,
method: "GET",
dataType: "jsonp"
});
}
function delete_value(){
$("#re").css("visibility","hidden");
document.getElementById("loader").style.visibility = "visible";
$('#mySpinner').addClass('spinner');
var id1= $("#id").val();
var name= $("#name").val();
var url = script_url+"?callback=ctrlq&name="+name+"&id="+id1+"&action=delete";
var request = jQuery.ajax({
crossDomain: true,
url: url ,
method: "GET",
dataType: "jsonp"
});
}
// print the returned data
function ctrlq(e) {
$("#re").html(e.result);
$("#re").css("visibility","visible");
read_value();
}
function read_value() {
$("#re").css("visibility","hidden");
document.getElementById("loader").style.visibility = "visible";
var url = script_url+"?action=read";
$.getJSON(url, function (json) {
// Set the variables from the results array
// CREATE DYNAMIC TABLE.
var table = document.createElement("table");
var header = table.createTHead();
var row = header.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = "<b>ID</b>";
cell2.innerHTML = "<b>Name</b>";
cell3.innerHTML = "<b>Green</b>";
// ADD JSON DATA TO THE TABLE AS ROWS.
for (var i = 0; i < json.records.length; i++) {
tr = table.insertRow(-1);
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = json.records[i].ID;
tabCell = tr.insertCell(-1);
tabCell.innerHTML = json.records[i].NAME;
tabCell = tr.insertCell(-1);
tabCell.innerHTML = json.records[i].Green;
}
// FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "";
divContainer.appendChild(table);
document.getElementById("loader").style.visibility = "hidden";
$("#re").css("visibility","visible");
});
}
</script>
</head>
<body>
<div align="center">
<h1 class="BG"> </h1>
<form class="cl" >
ID
<input type = "text" name ="id" id="id">
Name
<input type = "text" name ="name" id="name">
Green
<input type = "text" name ="green" id="green">
</form>
<div id="loader"></div>
<p id="re"></p>
<span id="b1">
<input type = "button" id = "b1" onClick="insert_value()" value = "Add Data">
</input>
<input type="button" id = "b1" onClick="read_value()" value="Database Table" />
<input type="button" id = "b1" onClick="update_value()" value="Update" />
<input type="button" id = "b1" onClick="delete_value()" value="Delete Table" />
</span><a href="https://docs.google.com/spreadsheets/d/1LMYfKxNonTxEK_ZzBmK0P4EV_tXC_MSylIxljGKE7Kw/edit#gid=1772958990" target="_blank" class="sz"> <span class="t"> Click to open Google sheet</span></a>
<div id="showData"></div>
</div>
</body>
<div align="center">
<p> QR-Code DataBase 2018 | All Rights Reserved</p>
</div>
<html>