Skip to content

Instantly share code, notes, and snippets.

@neno-tech
Last active February 2, 2022 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neno-tech/b0b30521ec31f46d981a4fe514684325 to your computer and use it in GitHub Desktop.
Save neno-tech/b0b30521ec31f46d981a4fe514684325 to your computer and use it in GitHub Desktop.
ฟอร์มบันทึกลงชีตด้วยวิธี doPost
function doGet(e) {
var htmlOutput = HtmlService.createTemplateFromFile('index');
var courses = getCourses()
htmlOutput.message = ''
htmlOutput.courses = courses
return htmlOutput.evaluate()
}
function doPost(e) {
var name = e.parameters.name.toString();
var courses = e.parameters.courses.toString();
AddRecord(name, courses);
var htmlOutput = HtmlService.createTemplateFromFile('index');
var courses = getCourses();
htmlOutput.message = 'ลงทะเบียนเรียบร้อยแล้ว';
htmlOutput.courses = courses;
return htmlOutput.evaluate()
}
function AddRecord(name, courses) {
var ss = SpreadsheetApp.getActive().getSheets()[0]
ss.appendRow([name, courses, new Date()])
}
function getCourses() {
var ss = SpreadsheetApp.getActive().getSheets()[1]
var array = []
for (var i = 2; i< ss.getLastRow()+1; i++) {
array.push(ss.getRange(i,1).getValue())
}
return array;
}
function getUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>*{font-family:Prompt}</style>
</head>
<body>
<h1>ฟอร์มลงทะเบียน</h1>
<?var url = getUrl()?>
<form method="post" action="<?= url ?>">
<label style="font-size:20px">ชื่อ สกุล</label><br>
<input type="text" name="name" style="font-size: 20px"><br><br>
<label style="font-size:20px" > เลือกคอร์ส </label><br>
<select name="courses" style="font-size: 20px" >
<option value="" ></option>
<? for(var i=0; i< courses.length; i++) { ?>
<option value="<?= courses[i] ?>" ><?= courses[i] ?></option>
<? } ?>
</select>
<br><br>
<input type="submit" name="submitButton" value="ลงทะเบียน" style="font-size: 20px">
<span style-="font-size: 20px" ><?= message ?></span>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment