如何使用 Google Apps 脚本将 PDF 正文格式化为看起来像电子邮件?
How to format PDF body to look like the email using Google Apps Script?
下面的代码确实获得了表单响应并通过电子邮件将其与格式如下的正文一起发送:
Question/Title1
响应 1
Question/Title2
回应2
不过,创建的 pdf 在一行中显示了所有内容,如下所示:
新回复表格:Question/Title1 Response1 Question/Title2 Response2...
如何创建它以使其看起来像上面格式化的电子邮件正文?
function onFormSubmit(e) {
// Get the response that was submitted.
var formResponse = e.response;
// Get the items (i.e., responses to various questions)
// that were submitted.
var itemResponses = formResponse.getItemResponses();
// Create a variable emailBody to store the body
// of the email notification to be sent.
var emailBody = "New form response:\n\n";
// Put together the email body by appending all the
// questions & responses to the variable emailBody.
itemResponses.forEach(function(itemResponse) {
var title = itemResponse.getItem().getTitle();
var response = itemResponse.getResponse();
emailBody += title + "\n" + response + "\n\n";
});
// Send the email notification using the
// sendEmail() function.
sendEmail(emailBody);
var folderID = "SOME_FOLDERID";
var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);
var blob = intermediate.getAs(MimeType.PDF);
Logger.log(blob.getContentType());
var pdfFile = DriveApp.createFile(blob);
DriveApp.getFolderById(folderID).addFile(pdfFile)
DriveApp.getFileById(intermediate.getId()).setTrashed(true);
}
// A function that sends the email
// notification.
function sendEmail(emailBody) {
MailApp.sendEmail("EMAIL_ADDRESS", "New form response", emailBody);
}
您需要在 emailBody
中为新行使用 <br>
标记,因为您在创建文件时使用的是 HTML 类型。您可以使用 String.replaceAll() 将 \n
替换为 <br>
。
//Replace all "\n" with "<br>"
emailBody = emailBody.replaceAll("\n","<br>");
var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);
输出:
注:
- 您实际上可以在 html 正文中使用
\n
作为换行符,但您需要在 css 中设置它。参见
下面的代码确实获得了表单响应并通过电子邮件将其与格式如下的正文一起发送:
Question/Title1
响应 1
Question/Title2
回应2
不过,创建的 pdf 在一行中显示了所有内容,如下所示: 新回复表格:Question/Title1 Response1 Question/Title2 Response2...
如何创建它以使其看起来像上面格式化的电子邮件正文?
function onFormSubmit(e) {
// Get the response that was submitted.
var formResponse = e.response;
// Get the items (i.e., responses to various questions)
// that were submitted.
var itemResponses = formResponse.getItemResponses();
// Create a variable emailBody to store the body
// of the email notification to be sent.
var emailBody = "New form response:\n\n";
// Put together the email body by appending all the
// questions & responses to the variable emailBody.
itemResponses.forEach(function(itemResponse) {
var title = itemResponse.getItem().getTitle();
var response = itemResponse.getResponse();
emailBody += title + "\n" + response + "\n\n";
});
// Send the email notification using the
// sendEmail() function.
sendEmail(emailBody);
var folderID = "SOME_FOLDERID";
var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);
var blob = intermediate.getAs(MimeType.PDF);
Logger.log(blob.getContentType());
var pdfFile = DriveApp.createFile(blob);
DriveApp.getFolderById(folderID).addFile(pdfFile)
DriveApp.getFileById(intermediate.getId()).setTrashed(true);
}
// A function that sends the email
// notification.
function sendEmail(emailBody) {
MailApp.sendEmail("EMAIL_ADDRESS", "New form response", emailBody);
}
您需要在 emailBody
中为新行使用 <br>
标记,因为您在创建文件时使用的是 HTML 类型。您可以使用 String.replaceAll() 将 \n
替换为 <br>
。
//Replace all "\n" with "<br>"
emailBody = emailBody.replaceAll("\n","<br>");
var intermediate = DriveApp.createFile('New Form Response','<b>'+ emailBody +'</b>', MimeType.HTML);
输出:
注:
- 您实际上可以在 html 正文中使用
\n
作为换行符,但您需要在 css 中设置它。参见