เมนู File > New > Script File
ตั้งชื่อ: myscript5
เขียนโค๊ดตามนี้
function MailMerge1() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
SalarySheet=SpreadsheetApp.setActiveSheet(ss.getSheetByName("Salary")),
TemplateSheet=SpreadsheetApp.setActiveSheet(ss.getSheetByName("Template")),
header = "A1:G1",
data="A2:G6",
template="A1:A4",
dataRows = SalarySheet.getRange(data).getValues(),
headerRow = SalarySheet.getRange(header).getValues(),
numColumns = SalarySheet.getRange(header).getNumColumns(),
templateRows= TemplateSheet.getRange(template).getValues();
for (var i=0 ; i < dataRows.length ; i++) {
var newText = templateRows[0][0] + "\n" +
"\t" + templateRows[1][0] + "\n" +
"\t" + templateRows[2][0] + "\n" +
templateRows[3][0] + "\n";
for (var j = 0 ; j < numColumns -1 ; j++) {
newText = newText.replace('{' + headerRow[0][j] + '}', dataRows[i][j]);
}
try {
dataRows[i][numColumns-1] = new Date();
} catch (e) {
dataRows[i][numColumns-1] = e.message;
}
Logger.log(newText);
}
SalarySheet.getRange(data).setValues(dataRows);
}
Comments are closed.