Last active
July 2, 2023 04:24
-
-
Save krooluang/b2acb5653888de20a6888c493ed7605a to your computer and use it in GitHub Desktop.
Form กำหนดเงื่อนไขผ่าน/ไม่ผ่าน แนบไฟล์
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() { | |
return HtmlService.createTemplateFromFile('index') | |
.evaluate() | |
.setTitle('FormSubmit') | |
.addMetaTag('viewport', 'width=device-width, initial-scale=1') | |
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) | |
} | |
var folder = DriveApp.getFolderById('xxx') //ChangeIdFolder | |
function saveData(obj){ | |
if(obj.myFile.length > 0){ | |
var file = folder.createFile(obj.myFile).getUrl() | |
} | |
let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ชีต1') | |
ss.appendRow([ | |
new Date(), | |
obj.firstname, | |
obj.position, | |
obj.cert, | |
file | |
]) | |
} |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<base target="_top"> | |
<script src="https://code.jquery.com/jquery-3.6.4.js"></script> | |
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" /> | |
<style> | |
@import url('https://fonts.googleapis.com/css2?family=Kanit&display=swap'); | |
body { | |
font-family: 'Kanit', sans-serif; | |
font-size: .875rem; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="col-md-6 mx-auto"> | |
<form id="myForm" onsubmit="submitForm(this)"> | |
<div class="col-md-12 text-center"> | |
<h5 class="mb-2 p-2 bg-primary text-white"><i class="fa-solid fa-align-justify"></i> ฟอร์มลงทะเบียน</h5> | |
</div> | |
<div class="row"> | |
<div class="col-md-6 mb-2"> | |
<label><i class="fa-solid fa-caret-right"></i> ชื่อ สกุล</label> | |
<input type="text" class="form-control" name="firstname" placeholder="กรุณาพิมพ์ชื่อ สกุล" required > | |
</div> | |
<div class="col-md-6 mb-2"> | |
<label><i class="fa-solid fa-caret-right"></i> ตำแหน่ง</label> | |
<select class="form-control" name="position" required> | |
<option disabled selected value="">เลือกตำแหน่ง</option> | |
<option>ผู้อำนวยการ</option> | |
<option>ครู</option> | |
<option>พนักงานราชการ</option> | |
<option>ครูอัตราจ้าง</option> | |
</select> | |
</div> | |
<div class="col-md-6 mb-2"> | |
<label><i class="fa-solid fa-caret-right"></i> ท่านเคยผ่านการอบรมหรือไม่</label> | |
<select class="form-control" id="cert" name="cert" required onchange="attcert(value)"> | |
<option disabled selected value="">เลือก</option> | |
<option>ผ่าน</option> | |
<option>ไม่ผ่าน</option> | |
</select> | |
</div> | |
<div class="col-md-6 mb-2" id="pcert" style="display:none"> | |
<label><i class="fa-solid fa-caret-right"></i> ไฟล์เกียรติบัตร</label> | |
<input type="file" class="form-control" id="myFile" name="myFile"> | |
</div> | |
</div> | |
<div class="col-md-12 mt-2"> | |
<button type="submit" class="btn btn-primary w-100" id="btn1" ><i class="fa-solid fa-floppy-disk"></i> บันทึกข้อมูล</button> | |
</div> | |
</form> | |
</div> | |
</div> | |
<script> | |
function attcert(value){ | |
if(value == "ผ่าน"){ | |
$('#pcert').show(); | |
}else{ | |
$('#pcert').hide(); | |
} | |
} | |
function submitForm(obj){ | |
event.preventDefault() | |
let cert = $('#cert').val() | |
let file = $('#myFile').val() | |
if(cert == "ผ่าน"){ | |
if(file == ""){ | |
Swal.fire({ | |
position: 'top', | |
icon: 'error', | |
title: 'กรุณาแนบไฟล์เกียรติบัตร', | |
showConfirmButton: false, | |
timer: 1500 | |
}) | |
}else if(file!=""){ | |
saveData(obj) | |
} | |
} | |
else{ | |
saveData(obj) | |
} | |
} | |
function saveData(obj){ | |
$('#btn1').html('<i>Loading...</i>') | |
$('#btn1').attr("disabled", true); | |
google.script.run.withSuccessHandler(()=>{ | |
$('#myForm')[0].reset() | |
$('#btn1').html('บันทึกข้อมูล') | |
$('#btn1').attr("disabled", false); | |
Swal.fire({ | |
position: 'top', | |
icon: 'success', | |
title: 'บันทึกข้อมูลเรียบร้อย', | |
showConfirmButton: false, | |
timer: 1500 | |
}) | |
}).saveData(obj) | |
} | |
</script> | |
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment