【问题标题】:How do I format my text in Google Script, for it to show when I send an email?如何在 Google Script 中格式化我的文本,以便在我发送电子邮件时显示?
【发布时间】:2015-07-27 20:47:55
【问题描述】:

我一直在尝试编辑一个脚本,我必须将某些文本加粗。此脚本从 Google 表单电子表格中提取信息并返回如下所示的电子邮件:


教师参与: 在最初的观察中,老师会适当地参与(直接指导、建模、构建知识、指导/独立实践等)

技术整合: 没有技术使用或集成的证据


我希望标题“教师参与:”和“技术集成”加粗并加下划线。如何在我的代码中实现 HTML?

我的代码是:

function sendEmail() {

var sheet = SpreadsheetApp.getActiveSheet();

var row = sheet.getActiveRange().getRowIndex();

var userEmail = sheet.getRange(row,   
  getColIndexByName("Username")).getValue();

var body = "Below are the results of a recent Walkthrough: ";

 body += "\n\nTeacher Engagement: \n" + sheet.getRange(row,     
  getColIndexByName("Teacher Engagement")).getValue();

 body += "\n\nTechnology Integration: \n" + sheet.getRange(row,     
  getColIndexByName("Technology Integration")).getValue();

MailApp.sendEmail(userEmail, subject, body, {name:"Classroom Walkthrough"});
}

【问题讨论】:

  • 查看官方文档,其中发送了一个示例 html 电子邮件。尝试将当前字符串转换为 html,因为它当前不是那种格式。例如,将“\n”替换为 html 段落或只是
    以保持简单。先试一试,然后根据您的结果更新我们

标签: javascript html google-apps-script formatting


【解决方案1】:

使用 MailApp 格式化电子邮件的唯一可行解决方案是使用 HTMLbody 选项并将您的文本编写为 HTML。

示例:

function sendEmail() {

var sheet = SpreadsheetApp.getActiveSheet();

var row = sheet.getActiveRange().getRowIndex();

var userEmail = sheet.getRange(row,   
getColIndexByName("Username")).getValue();

var body = "<HTML><BODY>"
+"Below are the results of a recent Walkthrough: "
+"<P>Teacher Engagement: <BR>" 
+sheet.getRange(row,getColIndexByName("Teacher Engagement")).getValue()
+"</P>"
+"<P>Technology Integration: <BR>" 
+sheet.getRange(row,getColIndexByName("Technology Integration")).getValue()
+"</P></BODY></HTML>";

MailApp.sendEmail({
to:userEmail,
subject:subject,
htmlBody:body,
name:"Classroom Walkthrough"
});
}

结果:

Below are the results of a recent Walkthrough: 

Teacher Engagement: 
stuff

Technology Integration: 
other stuff

【讨论】:

  • 感谢奥兰治本德!您的代码有效,我只需将 添加到我想加粗的区域。这封电子邮件最终看起来很棒,而且更容易阅读。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多