简体   繁体   English

当数据仅来自电子表格的一个单元格时,如何使用应用程序脚本在 Html 电子邮件中进行换行?

[英]How can i do line break in Html Email using app script while the data is only from one cell of Spreadsheet?

I tried to import the data from a spread sheet and send it with GmailApp.我尝试从电子表格导入数据并使用 GmailApp 发送。 In the "B4" of my spreadsheet, there is a text with line breaks.在我的电子表格的“B4”中,有一个带有换行符的文本。 If i tried to transfer it to Html-Email.如果我尝试将其传输到 Html-Email。 It didn't read the " <br /> " which i converted from "\n".它没有读取我从“\n”转换而来的“ <br /> ”。 It always showed like " Hello <br /> i'm here! " in the email and didn't do the line breaks in email.它总是在电子邮件中显示为“你好<br />我在这里!”并且没有在电子邮件中进行换行。 Should i do something more for it?我应该为它做更多的事情吗? Thanks!谢谢!

The code:编码:

function splitTest() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
 var str = ss.getRange("B4").getValue();
  Logger.log(str);
  var array1 = [{}];
  array1 = str.split("\n");
  return array1
};

function sendMail() {
  var ename = 3; //give every variable the right column number. 1st column is 0 and so on.
  var cname = 2;
  var email = 5;
  var emailTemp = HtmlService.createTemplateFromFile("email");
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Recipients");
  var wsSettings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var name = wsSettings.getRange("B2").getValue();
  var subject = wsSettings.getRange("B1").getValue();
  var array1 = splitTest();
  var cMessage = array1.join("<br />");
  var data = ws.getRange("A2:G" + ws.getLastRow()).getValues();
  data = data.filter(function(r){ return r[6] == true}); //return r[a], a is for where the checkbox is. for us to choose who we send the mails.The column starts from 0!
  data.forEach(function(row){
   emailTemp.cmess = cMessage;
   var htmlMessage = emailTemp.evaluate().getContent();
    GmailApp.sendEmail(
      row[email],
      subject,
      "Bitte öffnen Sie das Html-Format,um die Email zu lesen, Danke!",
      {name: name,
      htmlBody: htmlMessage, 
      });
  }); 
};

The html code html代码

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    
  </head>
  <body>
   
  <div align="left">
<table style="border: none;"><colgroup><col style="width: 487px;" /><col style="width: 115px;" /></colgroup>
<tbody>
<tr style="height: 99pt;">
<td style="vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; text-align: justify; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><span style="border: none;"><img src="https://drive.google.com/uc?id=1rRVBhFpEbp8bjGI9jqWlbn_6K8H4djAv" width="438" height="158" /></span></span></p>
</td>
<td style="border-right: 1pt solid #ffffff; border-bottom: 1pt solid #ffffff; border-top: 1pt solid #ffffff; vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; text-align: right; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><span style="border: none;"><img src="https://drive.google.com/uc?id=1vwNZtLRAnoRX5WuEWeojEKMf6lOOYbre" width="67" height="137" /></span></span></p>
</td>
</tr>
<tr style="height: 470.66pt;">
<td style="border-left: 1pt solid #ffffff; border-right: 1pt solid #ffffff; vertical-align: top;" colspan="2">
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><?= cbegin ?></span></p>
<br />
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt; white-space:pre-line;"><span style="font-size: 11pt; font-family: Arial;">
  <?= cmess ?>
  </span>

</p>
<br />
<br />
<br />
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><?= cend ?></span></p>
</td>
</tr>
<tr style="height: 60.1465pt;">
<td style="vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><strong><span style="font-size: 9pt; font-family: Arial;">Formosa Chinesische Sprachschule e.V.</span></strong></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 9pt; font-family: Arial;">Schulort : Geschwister-Scholl-Gymnasium D&uuml;sseldorf</span></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 9pt; font-family: Arial;">Redinghovenstra&szlig;e 41, 40225 D&uuml;sseldorf</span></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><strong><span style="font-size: 9pt; font-family: Arial;">Email: </span></strong><a style="text-decoration: none;" href="mailto:formosa.school.germany@gmail.com"><strong><span style="font-size: 9pt; font-family: Arial; color: #1155cc; text-decoration: underline; text-decoration-skip-ink: none;">formosa.school.germany@gmail.com</span></strong></a></p>
</td>
<td style="border-right: 1pt solid #ffffff; border-bottom: 1pt solid #ffffff; border-top: 1pt solid #ffffff; vertical-align: top; background-color: #f4cccc;">&nbsp;</td>
</tr>
</tbody>
</table>
</div>

  </body>
</html>

I found the answer!我找到了答案! It works if I do .replace() to change the code back to "<" and ">", Thank you Cooper anyway!如果我执行.replace()将代码更改回“<”和“>”,它会起作用,无论如何,谢谢 Cooper! Yes!是的!

var str = emailTemp.evaluate().getContent();
  str1 = str.replace(/&lt;/g,"<");   
  str2 = str1.replace(/&gt;/g,">"); 
....
htmlbody: str2...

Try this:尝试这个:

function splitTest() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var str = ss.getRange("B4").getValue();
  return str.split("\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 HTML 发送 Google App 脚本 email - How do I send Google App Script email using HTML 如何将单元格中的日期从电子表格中提取到我的 HTML - How can I pull a date in cell from spreadsheet to my HTML 关于如何根据从其电子表格的 html 形式收到的数据创建 pdf 并通过电子邮件发送 pdf 文件的 Google 应用脚本 - Google app script on how to create a pdf from the data received from an html form of its spreadsheet and send the pdf file via email 如何在网络应用程序中加入换行符? (HTML,CSS) - How do I incorperate a line break in a web app? (HTML,CSS) 如何使用电子表格中的表格填充 HTML 表格? - How do I populate an HTML table using a table from a spreadsheet? 换行 html 电子邮件 - Line break html Email 如何从另一个HTML文件中的一个HTML Apps脚本文件获取JSON数据? - How do I get JSON data from one HTML Apps Script file in another HTML file? 我如何使用content.match(Google应用程序脚本)读取空白行的html源代码 - how can i read html source code with line blank using content.match (google app script) 如何在不使用任何 html 标签的情况下向 html 文本添加换行符 - How can I add line break to html text without using any html tag 如何从电子表格中检索图表Google脚本的数据。 (使用HTML服务) - How to retrieve data from spreadsheet for chart google script. (Using HTML Service)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM