You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Apps Script生成的邮件中设置加粗文本与字号调整?

问题解决:调整邮件签名的样式(加粗职位+放大名字字号)

需求说明

我通过Google表单提交的数据会填充到Google表格,表单提交时,表格内的Google Apps Script会向表单指定收件人发送邮件。希望邮件签名中的职位「乐队与合唱团总监」和「荣誉合唱团协调员」显示为加粗样式,同时让签名里的名字字号更大。现有代码如下:

function afterFormSubmit(e) {
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  const entryRow = e.range.getRow();
  const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  ws.getRange(entryRow, 5).setValue(pdfFile.getUrl());
  ws.getRange(entryRow, 6).setValue(pdfFile.getName());

  sendEmail(e.namedValues['Email'][0],pdfFile);
}

function sendEmail(email,pdfFile){
  GmailApp.sendEmail(email, "Honors Choir", "Esteemed Colleague, \n\nAttached to this email is a confirmation of registration & receipt of payment for the Honors Choir. \n\nAdditionally, it appears that music for the concert is on backorder. I will share a folder with scans of the music with you soon. However, please make sure to support our fellow musicians by ordering the originals. \n\nSincerely, \nMy Name \n<b>Director of Bands and Choirs</b> \nA Michigan High School | School Address | City, State ZIP CODE\nWebsite.com | Facebook: Account \n\nHonors Choir Coordinator \nwww.Website.org | Facebook: Account",    {
    attachments: [pdfFile],
    name: 'Honors Choir Coordinator'
  });
}

解决方案

原代码中使用了HTML标签但未启用HTML邮件模式,导致样式不生效。需要修改sendEmail函数,启用HTML格式邮件,并通过HTML标签设置签名样式:

  1. 添加htmlBody参数,将邮件内容转换为HTML结构(用<br>替代换行符)
  2. 用<span>标签给名字设置更大字号
  3. 确保职位的<b>标签在HTML中生效

修改后的完整代码:

function afterFormSubmit(e) {
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  const entryRow = e.range.getRow();
  const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  ws.getRange(entryRow, 5).setValue(pdfFile.getUrl());
  ws.getRange(entryRow, 6).setValue(pdfFile.getName());

  sendEmail(e.namedValues['Email'][0], pdfFile);
}

function sendEmail(email, pdfFile) {
  // 纯文本内容(作为HTML邮件的降级备选)
  const plainTextBody = "Esteemed Colleague, \n\nAttached to this email is a confirmation of registration & receipt of payment for the Honors Choir. \n\nAdditionally, it appears that music for the concert is on backorder. I will share a folder with scans of the music with you soon. However, please make sure to support our fellow musicians by ordering the originals. \n\nSincerely, \nMy Name \nDirector of Bands and Choirs \nA Michigan High School | School Address | City, State ZIP CODE\nWebsite.com | Facebook: Account \n\nHonors Choir Coordinator \nwww.Website.org | Facebook: Account";
  
  // HTML格式内容(带样式)
  const htmlBody = `
    Esteemed Colleague, <br><br>
    Attached to this email is a confirmation of registration & receipt of payment for the Honors Choir. <br><br>
    Additionally, it appears that music for the concert is on backorder. I will share a folder with scans of the music with you soon. However, please make sure to support our fellow musicians by ordering the originals. <br><br>
    Sincerely, <br>
    <span style="font-size: 1.3em; font-weight: 500;">My Name</span> <br>
    <b>Director of Bands and Choirs</b> <br>
    A Michigan High School | School Address | City, State ZIP CODE <br>
    Website.com | Facebook: Account <br><br>
    <b>Honors Choir Coordinator</b> <br>
    www.Website.org | Facebook: Account
  `;

  GmailApp.sendEmail(email, "Honors Choir", plainTextBody, {
    attachments: [pdfFile],
    name: 'Honors Choir Coordinator',
    htmlBody: htmlBody // 启用HTML邮件模式
  });
}

关键修改说明

  • 拆分出纯文本和HTML两种内容,保证不支持HTML的邮箱也能正常显示邮件内容
  • 用<span style="font-size: 1.3em;">将名字字号放大(可根据需求调整1.3em的数值)
  • 给两个职位都加上<b>标签,确保在HTML邮件中显示为加粗样式
  • 通过htmlBody参数告诉Gmail发送HTML格式的邮件

内容的提问来源于stack exchange,提问作者Jaylon Sims

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 12:26:13