Download Spreadsheets
Download Code
ปรับแก้โค้ดให้เก็บรูปลงกูเกิลไดรฟ์และเพิ่มลิงก์ดูรูป
- คัดลอกฟังก์ชั่นด้านล่างนี้ไปแทนฟังก์ชั่นเดิม ใน code.gs
/** save data to sheet */
function saveData(obj) { // studentId, fullName, className, activity, message, imageUrl
const lock = LockService.getScriptLock();
if (lock.tryLock(5000)) { // ล็อกการเข้าถึงสูงสุด 5 วินาที
try {
// ดึงเฉพาะส่วน Base64 (ตัด 'data:image/jpeg;base64,' ออก)
const base64Data = obj.imageUrl.replace(/^data:image\/\w+;base64,/, "");
// แปลง Base64 เป็น Blob
const blob = Utilities.newBlob(Utilities.base64Decode(base64Data), "image/jpeg", "uploaded_image.jpg");
// ระบุโฟลเดอร์ปลายทางใน Google Drive (ใช้ Folder ID)
const folderId = "xxxxxxxxxxxxx"; // 👉 เปลี่ยนเป็น Folder ID ของคุณ
const folder = DriveApp.getFolderById(folderId);
// อัปโหลดไฟล์
const file = folder.createFile(blob);
const fileUrl = file.getUrl();
const imgUrl = "https://lh5.googleusercontent.com/d/"+file.getId()
const date = new Date();
const options = { day: 'numeric', month: 'long', year: 'numeric' };
const datenow = date.toLocaleDateString('th-TH', options);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Records");
sheet.appendRow([
datenow,
obj.studentId,
obj.fullName,
obj.className,
obj.activity,
obj.message,
fileUrl,
imgUrl
]);
getstudentData(); // อัปเดตข้อมูลสรุปการเข้าร่วมกิจกรรม
return { success: true, message: "บันทึกข้อมูลเรียบร้อยแล้ว!" };
} finally {
lock.releaseLock(); // ปลดล็อกเมื่อทำงานเสร็จ
}
} else {
return { success: false, message: "มีผู้เข้าใช้ระบบจำนวนมาก กรุณาลองอีกครั้ง!" };
}
}