function sendMailMerge() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheetByName("Test"); const data = sheet.getDataRange().getValues(); const headers = data.shift(); const nameIndex = headers.indexOf("Họ tên"); const emailIndex = headers.indexOf("Email"); if (nameIndex === -1 || emailIndex === -1) { throw new Error("Could not find 'Họ tên' or 'Email' in the header row."); } const docId = '1tTD00wA1qOqACEQdFhl1N9Glh57igpemM9Vczt2mEMA'; // 1. Get the Doc content as HTML instead of plain text const docHtmlTemplate = convertDocToHtml(docId); data.forEach(row => { const recipientName = row[nameIndex]; const recipientEmail = row[emailIndex]; if (recipientEmail) { // 2. Replace placeholders within the HTML string let emailHtml = docHtmlTemplate; emailHtml = emailHtml.replace(/{{Họ tên}}/g, recipientName); emailHtml = emailHtml.replace(/{{Email}}/g, recipientEmail); MailApp.sendEmail({ name: "SOFT Việt Nam", to: recipientEmail, subject: "Giải pháp chăm sóc khách hàng đa kênh giúp tăng năng suất vượt trội", htmlBody: emailHtml }); } }); } /** * Helper function to convert Google Doc body to basic HTML */ function convertDocToHtml(docId) { const doc = DocumentApp.openById(docId); const body = doc.getBody(); let html = ""; const numChildren = body.getNumChildren(); for (let i = 0; i < numChildren; i++) { const child = body.getChild(i); const type = child.getType(); if (type === DocumentApp.ElementType.PARAGRAPH) { html += "
"; html += processTextElement(child.asParagraph()); html += "
"; } else if (type === DocumentApp.ElementType.LIST_ITEM) { html += "