Skip to content

Instantly share code, notes, and snippets.

@neno-tech
Created April 26, 2021 11:00
Show Gist options
  • Save neno-tech/47223d7f96be3d26a02eeb696c188256 to your computer and use it in GitHub Desktop.
Save neno-tech/47223d7f96be3d26a02eeb696c188256 to your computer and use it in GitHub Desktop.
google form+uplod image+pdf+emai+line notyfy
function FormSubmit(e) {
const info = e.namedValues
const pdfFile = createPDF(info)
const entryRow = e.range.getRow()
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('xxx')//1
ws.getRange(entryRow, 6).setValue(pdfFile.getUrl())
sendEmail(e.namedValues['xxx'], pdfFile)//2
sendLine(info)
}
//สร้าง pdf
function createPDF(info) {
const pdfFolder = DriveApp.getFolderById('xxx')//3
const tempFolder = DriveApp.getFolderById('xxx')//4
const templateDoc = DriveApp.getFileById('xxx')//5
const newTempFile = templateDoc.makeCopy(tempFolder)
const openDoc = DocumentApp.openById(newTempFile.getId())
var image_url = UrlFetchApp.fetch("http://drive.google.com/uc?id=" + info['xxx'][0].split('=')[1]);//6
const body = openDoc.getBody()
//รูปภาพ
var tables = body.getTables()
for (var i = 0; i < tables.length; i++) {
var cell = tables[i].getRow(0).getCell(0)
var inlineI = cell.clear().appendImage(image_url.getBlob())
var width = inlineI.getWidth();
var newW = width;
var height = inlineI.getHeight();
var newH = height;
var ratio = width / height;
if (width > 100) {
newW = 100;
newH = parseInt(newW / ratio);
}
inlineI.setWidth(newW).setHeight(newH)
}
body.replaceText("{xxx}", info['xxx'][0])//7
body.replaceText("{xxx}", info['xxx'][0])//8
openDoc.saveAndClose()
const blobPDF = newTempFile.getAs(MimeType.PDF)
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['xxx'][0])//9
pdfFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);
tempFolder.removeFile(newTempFile)
return pdfFile
}
//ส่งอีเมล
function sendEmail(email, pdfFile) {
GmailApp.sendEmail(email, "การรับสมัครเข้าเรียนต่อ", "คุณสมัครเข้าเรียนต่อ..เรียบร้อยแล้ว", {//10
attachments: [pdfFile],
name: 'โรงเรียนอภิวัฒน์สอนสร้างสื่อวิทยาคม'//11
})
}
//ส่งไลน์
function sendLine(info) {
var token = ['xxx']//12
var image_url = "http://drive.google.com/uc?id=" + info['xxx'][0].split('=')[1]
var name = info['xxx'][0]//13
var message = "มีนักเรียนสมัครเข้าเรียนต่อ \n ชื่อ-สกุล " + name//14
NotifyApp.sendNotify(token, message, image_url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment