add script with mail attachment bentuk PDF

This commit is contained in:
Fiky B 2022-09-24 23:25:35 +07:00
parent 2c606d236d
commit 9fd4df6399

96
automail-WithAttachment Normal file
View File

@ -0,0 +1,96 @@
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('SEND MAIL')
.addItem('Abstract Diterima, Kirim LoA','sendConfirmLoA')
// .addItem('Abstract Harus Direvisi','sendRevisiAbstract')
.addToUi();
}
//grab data from excel
function getData(namasheet) {
var data = SpreadsheetApp.getActive().getSheetByName(namasheet).getDataRange().getValues();
return data;
}
//bikin file PDF Untuk Attachment
function Create_PDF(getdata) {
const PDF_folder = DriveApp.getFolderById("1WQHSN7WASFV2IyjPWbeNAa8fHoFAjZft");
const TEMP_FOLDER = DriveApp.getFolderById("1gHbd9JgDCk-5sOG4azMZ-dIEx_ZAtzz_");
//file harus berbentuk Google Docs bukan DOCX
const PDF_Template = DriveApp.getFileById("1MITgesvn4_B-Oen0C7nZAhBDFZ7WvQhMzqypv0vzeqs");
const newTempFile = PDF_Template.makeCopy(TEMP_FOLDER);
const OpenDoc = DocumentApp.openById(newTempFile.getId());
const body = OpenDoc.getBody();
//console.log(body);
body.replaceText("{firstname}", getdata[1][0])
body.replaceText("{lastname}", getdata[1][1]);
body.replaceText("{email}", getdata[1][2]);
body.replaceText("{judulAbstract}", getdata[1][3]);
body.replaceText("{jenisloa}", getdata[1][4]);
body.replaceText("{kodeAbstract}", getdata[1][5]);
OpenDoc.saveAndClose();
const BLOBPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = PDF_folder.createFile(BLOBPDF).setName(getdata[1][0] + " " + getdata[1][5]);
//console.log("The file has been created ");
return pdfFile;
}
//render templateMail.html untuk body pesan
function renderTemplate(getdata){
var htmlTemplate = HtmlService.createTemplateFromFile("templateMail.html");
htmlTemplate.stocks = getdata;
var htmlBody = htmlTemplate.evaluate().getContent();
return htmlBody;
}
//bikin log ke spreadsheet
function makeLog(){
const data = SpreadsheetApp.getActive().getSheetByName("SheetSendLoA");
var email = data.getRange('SheetSendLoA!C2').getValue();
var time = Date("now");
//console.log(email)
data.appendRow([time,"Berhasil UwU",email]);
}
//kirim Mailer
function sendConfirmLoA(){
var getdata = getData("SheetSendLoA");
var pdffile = Create_PDF(getdata);
// var firstname = getdata[1][0];
// var lastname = getdata[1][1];
// var gelar = getdata[1][3];
var email =getdata[1][2];
// var judulAbstract = getdata[1][3];
// var jenisloa = getdata[1][4];
// var kodeAbstract = getdata[1][5];
var htmlBody = renderTemplate(getdata);
makeLog();
//console.log(firstname,'|',lastname,'|',email,'|',topic,'|',jenisLoA,'|',kodeAbstract);
var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter((account) => {
if(account.isDefault){
return true}
})[0].signature;
MailApp.sendEmail({
name: "ICMP",
to: email,
subject: "Letter of Acceptance for The 1st ICMP",
htmlBody: htmlBody,
attachments: [pdffile],
});
}
// function sendRevisiAbstract(){
// }