换行符 - Gmail Apps 脚本
Line Breaks - Gmail Apps Script
我已经编写了一些代码来从电子表格中自动发送我的电子邮件,并希望在我的团队中扩展它,但我想向它添加一个 footer/signature,并且不必在其中包含收件人和主题代码本身,而是从电子表格中提取它。我在电子表格中将其布局为;
我希望电子邮件页脚看起来像那样,但是当我 运行 脚本时它会返回 "Range"
function EODReportEmail() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Tasks");; // Use data from the active sheet
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow(); // Number of rows to process
var lastColumn = sheet.getLastColumn(); // Last column
var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
var data = dataRange.getValues(); // Fetch values for each row in the range
var recipient = "shea.a.murphy@icloud.com" // Email address report will be sent to
var subject = "Shea Murphy - EOD Email" // Subject heading of email e.g. Shea Murphy - EOD Email
var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
Logger.log(footer);
// Work through each row in the spreadsheet
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// Assign each column a variable
var Point1 = row[0]; // Col A: 1st point to be included in email
var Point2 = row[1]; // Col B: 2nd point to be included in email
var Point3 = row[2]; // Col C: 3rd point to be included in email
var Point4 = row[3]; // Col D: 4th point to be included in email
var Point5 = row[4]; // Col E: 5th point to be included in email
var Num1 = "<b>1. </b>" // Number variables for email points
var Num2 = "<b>2. </b>"
var Num3 = "<b>3. </b>"
var Num4 = "<b>4. </b>"
var Num5 = "<b>5. </b>"
var Task1 = Num1 + Point1 // Note 1 to be inlcuded in the email
var Task2 = Num2 + Point2 // Note 2 to be inlcuded in the email
var Task3 = Num3 + Point3 // Note 3 to be inlcuded in the email
var Task4 = Num4 + Point4 // Note 4 to be inlcuded in the email
var Task5 = Num5 + Point5 // Note 5 to be inlcuded in the email
if (Point1 == undefined) {Task1 = " "};
if (Point2 == undefined) {Task2 = " "};
if (Point3 == undefined) {Task3 = " "};
if (Point4 == undefined) {Task4 = " "};
if (Point5 == undefined) {Task5 = " "};
// Build the email message
var emailBody = '<b style="font-family:georgia;font-size:18px;font-style:italic; color: #D04A02";>EOD Report</b>';
emailBody += '<p>Please see what I have worked on today below:<p>';
emailBody += '<dl><dd>'+ Task1 +'</dd>';
emailBody += '<dl><dd>'+ Task2 +'</dd>';
emailBody += '<dl><dd>'+ Task3 +'</dd>';
emailBody += '<dl><dd>'+ Task4 +'</dd>';
emailBody += '<dl><dd>'+ Task5 +'</dd>';
emailBody += '<p>Let me know if you have any questions.<p>';
emailBody += footer
// Create the email draft
GmailApp.sendEmail(
recipient, // Recipients
subject, // Subject
' ', // Body
{
htmlBody: emailBody, // Options: Body (HTML)
}
)
}
}
基本上,有没有什么方法可以让收件人、主题的代码跨越两个页面,并在末尾包含所有正确格式的签名,而不是在代码本身中逐行写出来?
EOD Email Information
EOD Email Tasks
Email Itself
我相信你的目标如下。
- 您想使用 Google Apps 脚本将单元格 "C2" 中的富文本转换为 HTML 数据。
为此,这个答案怎么样?
修改点:
- 关于
when I run the script it brings back "Range"
,这个问题的原因是由于var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
的脚本。
- 在这种情况下,将检索范围对象。通过这种方式,
Range
被放在电子邮件的页脚。
- 为了使用从单元格中检索到的富文本作为电子邮件 HTML 正文的页脚,需要将富文本转换为 HTML.
当我看到你问题的图片时,我可以确认你放入单元格 "C2" 的富文本使用 "Bold"、"Italic"、"ForegroundColor" 和超链接。使用这个,我想提出以下修改脚本。
当您的脚本修改时,请修改如下。
修改后的脚本:
从:
var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
到:
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
var footer = range.getRichTextValue().getRuns().reduce((s, r) => {
let text = r.getText().replace(/\n/g, "<br>").replace(/ /g, " ");
if (r.getLinkUrl()) text = `<a href="${r.getLinkUrl()}">${text}<\/a>`;
const style = r.getTextStyle();
const obj = {
fontFamily: style.getFontFamily(),
fontSize: style.getFontSize(),
foregroundColor: style.getForegroundColor(),
bold: style.isBold(),
italic: style.isItalic(),
strikethrough: style.isStrikethrough(),
underline: style.isUnderline(),
};
const fontFamily = obj.fontFamily ? `font-family: '${obj.fontFamily}';` : "";
const fontSize = obj.fontSize ? `font-size: ${obj.fontSize * 1.333}px;` : "";
const foregroundColor = obj.foregroundColor ? `color: ${obj.foregroundColor};` : "";
const bold = obj.bold ? 'font-weight: bold;' : "";
const italic = obj.italic ? 'font-style: italic;' : "";
const strikethrough = obj.strikethrough ? 'text-decoration: line-through;' : "";
const underline = obj.underline ? 'text-decoration: underline;' : "";
const keys = [fontFamily, fontSize, foregroundColor, bold, italic, strikethrough, underline];
if (keys.some(e => e != "")) {
s += `${keys.reduce((str, e) => str += e, '<span style="')}">${text}</span>`;
} else {
s += text;
}
return s;
}, "");
结果:
当您的图像值用于此修改后的脚本时,将检索以下 HTML。 (电子邮件地址已更改为示例值。)
<span style="font-family: 'Arial';font-size: 13.33px;color: #000000;">Kind regards <br> --<br></span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;font-weight: bold;">Shea Murphy</span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;"><br>Mobile: 074xxxxxx93<br>Email: </span><span style="font-family: 'Arial';font-size: 13.33px;color: #1155cc;text-decoration: underline;"><a href="mailto:sample@email.com">sample@email.com</a></span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;"><br><br></span><span style="font-family: 'Arial';font-size: 13.33px;color: #990000;font-weight: bold;font-style: italic;">For further information - please contact</span>
参考文献:
我已经编写了一些代码来从电子表格中自动发送我的电子邮件,并希望在我的团队中扩展它,但我想向它添加一个 footer/signature,并且不必在其中包含收件人和主题代码本身,而是从电子表格中提取它。我在电子表格中将其布局为;
我希望电子邮件页脚看起来像那样,但是当我 运行 脚本时它会返回 "Range"
function EODReportEmail() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Tasks");; // Use data from the active sheet
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow(); // Number of rows to process
var lastColumn = sheet.getLastColumn(); // Last column
var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
var data = dataRange.getValues(); // Fetch values for each row in the range
var recipient = "shea.a.murphy@icloud.com" // Email address report will be sent to
var subject = "Shea Murphy - EOD Email" // Subject heading of email e.g. Shea Murphy - EOD Email
var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
Logger.log(footer);
// Work through each row in the spreadsheet
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// Assign each column a variable
var Point1 = row[0]; // Col A: 1st point to be included in email
var Point2 = row[1]; // Col B: 2nd point to be included in email
var Point3 = row[2]; // Col C: 3rd point to be included in email
var Point4 = row[3]; // Col D: 4th point to be included in email
var Point5 = row[4]; // Col E: 5th point to be included in email
var Num1 = "<b>1. </b>" // Number variables for email points
var Num2 = "<b>2. </b>"
var Num3 = "<b>3. </b>"
var Num4 = "<b>4. </b>"
var Num5 = "<b>5. </b>"
var Task1 = Num1 + Point1 // Note 1 to be inlcuded in the email
var Task2 = Num2 + Point2 // Note 2 to be inlcuded in the email
var Task3 = Num3 + Point3 // Note 3 to be inlcuded in the email
var Task4 = Num4 + Point4 // Note 4 to be inlcuded in the email
var Task5 = Num5 + Point5 // Note 5 to be inlcuded in the email
if (Point1 == undefined) {Task1 = " "};
if (Point2 == undefined) {Task2 = " "};
if (Point3 == undefined) {Task3 = " "};
if (Point4 == undefined) {Task4 = " "};
if (Point5 == undefined) {Task5 = " "};
// Build the email message
var emailBody = '<b style="font-family:georgia;font-size:18px;font-style:italic; color: #D04A02";>EOD Report</b>';
emailBody += '<p>Please see what I have worked on today below:<p>';
emailBody += '<dl><dd>'+ Task1 +'</dd>';
emailBody += '<dl><dd>'+ Task2 +'</dd>';
emailBody += '<dl><dd>'+ Task3 +'</dd>';
emailBody += '<dl><dd>'+ Task4 +'</dd>';
emailBody += '<dl><dd>'+ Task5 +'</dd>';
emailBody += '<p>Let me know if you have any questions.<p>';
emailBody += footer
// Create the email draft
GmailApp.sendEmail(
recipient, // Recipients
subject, // Subject
' ', // Body
{
htmlBody: emailBody, // Options: Body (HTML)
}
)
}
}
基本上,有没有什么方法可以让收件人、主题的代码跨越两个页面,并在末尾包含所有正确格式的签名,而不是在代码本身中逐行写出来?
EOD Email Information
EOD Email Tasks
Email Itself
我相信你的目标如下。
- 您想使用 Google Apps 脚本将单元格 "C2" 中的富文本转换为 HTML 数据。
为此,这个答案怎么样?
修改点:
- 关于
when I run the script it brings back "Range"
,这个问题的原因是由于var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
的脚本。- 在这种情况下,将检索范围对象。通过这种方式,
Range
被放在电子邮件的页脚。
- 在这种情况下,将检索范围对象。通过这种方式,
- 为了使用从单元格中检索到的富文本作为电子邮件 HTML 正文的页脚,需要将富文本转换为 HTML.
当我看到你问题的图片时,我可以确认你放入单元格 "C2" 的富文本使用 "Bold"、"Italic"、"ForegroundColor" 和超链接。使用这个,我想提出以下修改脚本。
当您的脚本修改时,请修改如下。
修改后的脚本:
从:var footer = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
到:
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EOD Email Information").getRange("C2");
var footer = range.getRichTextValue().getRuns().reduce((s, r) => {
let text = r.getText().replace(/\n/g, "<br>").replace(/ /g, " ");
if (r.getLinkUrl()) text = `<a href="${r.getLinkUrl()}">${text}<\/a>`;
const style = r.getTextStyle();
const obj = {
fontFamily: style.getFontFamily(),
fontSize: style.getFontSize(),
foregroundColor: style.getForegroundColor(),
bold: style.isBold(),
italic: style.isItalic(),
strikethrough: style.isStrikethrough(),
underline: style.isUnderline(),
};
const fontFamily = obj.fontFamily ? `font-family: '${obj.fontFamily}';` : "";
const fontSize = obj.fontSize ? `font-size: ${obj.fontSize * 1.333}px;` : "";
const foregroundColor = obj.foregroundColor ? `color: ${obj.foregroundColor};` : "";
const bold = obj.bold ? 'font-weight: bold;' : "";
const italic = obj.italic ? 'font-style: italic;' : "";
const strikethrough = obj.strikethrough ? 'text-decoration: line-through;' : "";
const underline = obj.underline ? 'text-decoration: underline;' : "";
const keys = [fontFamily, fontSize, foregroundColor, bold, italic, strikethrough, underline];
if (keys.some(e => e != "")) {
s += `${keys.reduce((str, e) => str += e, '<span style="')}">${text}</span>`;
} else {
s += text;
}
return s;
}, "");
结果:
当您的图像值用于此修改后的脚本时,将检索以下 HTML。 (电子邮件地址已更改为示例值。)
<span style="font-family: 'Arial';font-size: 13.33px;color: #000000;">Kind regards <br> --<br></span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;font-weight: bold;">Shea Murphy</span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;"><br>Mobile: 074xxxxxx93<br>Email: </span><span style="font-family: 'Arial';font-size: 13.33px;color: #1155cc;text-decoration: underline;"><a href="mailto:sample@email.com">sample@email.com</a></span><span style="font-family: 'Arial';font-size: 13.33px;color: #000000;"><br><br></span><span style="font-family: 'Arial';font-size: 13.33px;color: #990000;font-weight: bold;font-style: italic;">For further information - please contact</span>