Last active
September 6, 2022 06:39
-
-
Save apivat60/b324143cf26dca3decf65b932a834e14 to your computer and use it in GitHub Desktop.
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
function doGet(e){ | |
var op = e.parameter.action; | |
var ss=SpreadsheetApp.openByUrl("ใส่ url ชีต"); | |
var sheet = ss.getSheetByName("ใส่ชื่อแผ่นงาน"); | |
if(op=="insert") | |
return insert_value(e,sheet); | |
if(op=="read") | |
return read_value(e,ss); | |
if(op=="update") | |
return update_value(e,sheet); | |
if(op=="delete") | |
return delete_value(e,sheet); | |
} | |
function insert_value(request,sheet){ | |
var id = request.parameter.id; | |
var name = request.parameter.name; | |
var flag=1; | |
var lr= sheet.getLastRow(); | |
for(var i=1;i<=lr;i++){ | |
var id1 = sheet.getRange(i, 2).getValue(); | |
if(id1==id){ | |
flag=0; | |
var result="มีรหัสนักเรียนนี้อยู่ในระบบแล้ว!!"; | |
} } | |
if(flag==1){ | |
var d = new Date(); | |
var currentTime = d.toLocaleString(); | |
var rowData = sheet.appendRow([currentTime,id,name]); | |
var result="เพิ่มข้อมูลเรียบร้อยแล้ว!!"; | |
} | |
result = JSON.stringify({ | |
"result": result | |
}); | |
return ContentService | |
.createTextOutput(request.parameter.callback + "(" + result + ")") | |
.setMimeType(ContentService.MimeType.JAVASCRIPT); | |
} | |
function read_value(request,ss){ | |
var sheet="ใส่ชื่อแผ่นงาน"; | |
var output = ContentService.createTextOutput(), | |
data = {}; | |
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(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues(); | |
} | |
function getHeaderRow_(ss, sheetname) { | |
var sh = ss.getSheetByName(sheetname); | |
return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0]; | |
} | |
function update_value(request,sheet){ | |
var output = ContentService.createTextOutput(); | |
var id = request.parameter.id; | |
var name = request.parameter.name; | |
var flag=0; | |
var lr= sheet.getLastRow(); | |
for(var i=1;i<=lr;i++){ | |
var rid = sheet.getRange(i, 2).getValue(); | |
if(rid==id){ | |
sheet.getRange(i,3).setValue(name); | |
var result="อัปเดทข้อมูลเรียบร้อยแล้ว!!"; | |
flag=1; | |
} | |
} | |
if(flag==0) | |
var result="ไม่มีรหัสนักเรียนนี้ในระบบ!!"; | |
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 name = request.parameter.name; | |
var flag=0; | |
var lr= sheet.getLastRow(); | |
for(var i=1;i<=lr;i++){ | |
var rid = sheet.getRange(i, 2).getValue(); | |
if(rid==id){ | |
sheet.deleteRow(i); | |
var result="ลบข้อมูลเรียบร้อยแล้ว!!"; | |
flag=1; | |
} | |
} | |
if(flag==0) | |
var result="ไม่พบรหัสนักเรียนนี้ในระบบ!!"; | |
result = JSON.stringify({ | |
"result": result | |
}); | |
return ContentService | |
.createTextOutput(request.parameter.callback + "(" + result + ")") | |
.setMimeType(ContentService.MimeType.JAVASCRIPT); | |
} |
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
<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 #0B96F7; | |
border-radius: 50%; | |
border-top: 16px solid #FFFF00; | |
border-bottom: 16px solid #FFFF00; | |
width: 40px; | |
height: 40px; | |
-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); } | |
} | |
</style> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> | |
<script> | |
var script_url = "ใส่ URL ของ Web App"; | |
//เพิ่มข้อมูล | |
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 url = script_url+"?callback=ctrlq&name="+name+"&id="+id1+"&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) { | |
// สร้างตารางแสดงข้อมูล | |
var table = document.createElement("table"); | |
var header = table.createTHead(); | |
var row = header.insertRow(0); | |
var cell1 = row.insertCell(0); | |
var cell2 = row.insertCell(1); | |
row.style.background = "rgb(243,219,113)"; | |
cell1.innerHTML = "<b>รหัสนักเรียน</b>"; | |
cell2.innerHTML = "<b>ชื่อ สกุล</b>"; | |
//นำข้อมูลลงในตาราง | |
for (var i = 0; i < json.records.length; i++) { | |
tr = table.insertRow(-1); | |
var tabCell = tr.insertCell(-1); | |
tabCell.innerHTML = json.records[i].รหัสนักเรียน; | |
tabCell = tr.insertCell(-1); | |
tabCell.innerHTML = json.records[i].ชื่อ_สกุล; | |
} | |
var divContainer = document.getElementById("showData"); | |
divContainer.innerHTML = ""; | |
divContainer.appendChild(table); | |
document.getElementById("loader").style.visibility = "hidden"; | |
$("#re").css("visibility","visible"); | |
$("#id").val("") | |
$("#name").val("") | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<div align="center"> | |
<img src="ใส่ url รูปภาพ" width="50%"> | |
<h3>พัฒนาโดย ครูอภิวัฒน์ สอนสร้างสื่อ</h3> | |
<!-- สร้างฟอร์ม --> | |
<form > | |
รหัสนักเรียน | |
<input type = "text" name ="id" id="id"> | |
ชื่อ สกุล | |
<input type = "text" name ="name" id="name"> | |
</form> | |
<!-- เรียกตัว loder มาแสดง--> | |
<div id="loader"></div> | |
<p id="re"></p> | |
<input type = "button" style="font-size:12pt;color:white;background-color:#F76326;border:1px solid #000000;padding:2px" | |
id = "b1" onClick="insert_value()" value = "เพิ่มข้อมูล" ></input> | |
<input type="button" style="font-size:12pt;color:white;background-color:#6DA551;border:1px solid #000000;padding:2px" | |
onclick="read_value()" value="แสดงข้อมูล" /> | |
<input type="button" style="font-size:12pt;color:white;background-color:#0298F7;border:1px solid #000000;padding:2px" | |
onclick="update_value()" value="อัปเดทข้อมูล" /> | |
<input type="button" style="font-size:12pt;color:white;background-color:#975AE0;border:1px solid #000000;padding:2px" | |
onclick="delete_value()" value="ลบข้อมูล" /> | |
<br><br> | |
<a href="ใส่ urlของชีต?usp=sharing" target="_blank">ดูข้อมูลในชีต</a> | |
<div id="showData"></div> | |
</div> | |
</body> | |
<html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment