สร้าง function MailMerge2() ตามนี้
function MailMerge2() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
SalarySheet=SpreadsheetApp.setActiveSheet(ss.getSheetByName("Salary")),
TemplateID='1bjpQnJikYMGYNaJQhetpZpkHyjI7iqBqDufprzMSo4k',
header = "A1:G1",
data="A2:G6",
dataRows = SalarySheet.getRange(data).getValues(),
headerRow = SalarySheet.getRange(header).getValues(),
numColumns = SalarySheet.getRange(header).getNumColumns();
for (var i=0 ; i < dataRows.length ; i++) {
var id =DriveApp.getFileById(TemplateID).makeCopy("กองคลัง : แจ้งการโอนเงินให้คุณ " + dataRows[i][0] + " " + dataRows[i][1]).getId(),
doc=DocumentApp.openById(id),
docBody=doc.getBody();
for (var j = 0 ; j < numColumns -1 ; j++) {
docBody.replaceText('{' + headerRow[0][j] + '}' , dataRows[i][j]);
}
doc.saveAndClose();
var pdfFile = DriveApp.createFile(doc.getAs(MimeType.PDF));
try {
dataRows[i][numColumns-1] = new Date();
} catch (e) {
dataRows[i][numColumns-1] = e.message;
}
}
SalarySheet.getRange(data).setValues(dataRows);
}
Comments are closed.