Created
December 3, 2021 08:46
-
-
Save neno-tech/ff014ad67ee60a1deb7076ffc2eb58e4 to your computer and use it in GitHub Desktop.
ส่งเมลจากชีต+แนบไฟล์
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 onOpen(){ | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('ส่งอีเมล') | |
.addItem('ส่งอีเมลพร้อมแนบไฟล์', 'sendPDFForm') | |
.addItem('ส่งอีเมลให้ทุกคน', 'sendFormToAll') | |
.addToUi(); | |
} | |
function sendPDFForm(){ | |
var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow(); | |
sendEmailWithAttachment(row); | |
} | |
function sendEmailWithAttachment(row){ | |
var filename= 'xxx.pdf'; //แก้ชื่อไฟล์ที่แนบ | |
var file = DriveApp.getFilesByName(filename); | |
if (!file.hasNext()) | |
{ | |
console.error("Could not open file "+filename); | |
return; | |
} | |
var client = getClientInfo(row); | |
var template = HtmlService | |
.createTemplateFromFile('template');//ชื่อไฟล์ html | |
template.client = client; | |
var message = template.evaluate().getContent(); | |
MailApp.sendEmail({ | |
to: client.email, | |
subject: "ตัวอย่างการส่งอีเมลจากชีตพร้อมแนบไฟล์", | |
htmlBody: message, | |
attachments: [file.next().getAs(MimeType.PDF)] | |
}); | |
} | |
function getClientInfo(row){ | |
var sheet = SpreadsheetApp.getActive().getSheetByName('xxx');//เปลี่ยนชื่อชีต | |
var values = sheet.getRange(row,1,row,3).getValues(); | |
var rec = values[0]; | |
var client = | |
{ | |
first_name: rec[0], | |
last_name: rec[1], | |
email: rec[2] | |
}; | |
client.name = client.first_name+' '+client.last_name; | |
return client; | |
} | |
function sendFormToAll(){ | |
var sheet = SpreadsheetApp.getActive().getSheetByName('xxx');//เปลี่ยนชื่อชีต | |
var last_row = sheet.getDataRange().getLastRow(); | |
for(var row=2; row <= last_row; row++) | |
{ | |
sendEmailWithAttachment(row); | |
sheet.getRange(row,4).setValue("ส่งเรียบร้อยแล้ว"); | |
} | |
} |
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> | |
<head> | |
<base target="_top"> | |
</head> | |
<body> | |
<h2>ผลการอีเมลพร้อมกับแนบไฟล์</h2> | |
<p>สวัสดีคุณ <?= client.name ?> </p> | |
<p>นี่คืออีเมลพร้อมกับแนบไฟล์ส่งมาให้คุณ.</p> | |
<p>ขอแสดงความยินดีมา ณ โอกาศนี้.</p> | |
<p>ขอบคุณ<br/></p> | |
ครูอภิวัฒน์ "สอนสร้างสื่อ" | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment