[英]Remove html formatting when getting Body of a gmail message in javascript
I would like to remove the html formatting in my google apps script.我想删除我的 google 应用程序脚本中的 html 格式。 I am currently searching the email and printing the results to a google spreadsheet.我目前正在搜索电子邮件并将结果打印到谷歌电子表格。 I would like to know if there is a way to replace text.I am aware of regex but I dont think it works with the getBody function.我想知道是否有办法替换文本。我知道正则表达式,但我认为它不适用于 getBody 函数。
I would really appreciate some feedback or some help on this matter.我真的很感激在这个问题上的一些反馈或帮助。
Code:代码:
function Search() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
// Clear existing search results
sheet.getRange(2, 1, sheet.getMaxRows() - 1, 4).clearContent();
// Which Gmail Label should be searched?
var label = sheet.getRange("F3").getValue();
// Get the Regular Expression Search Pattern
var pattern = sheet.getRange("F4").getValue();
// Retrieve all threads of the specified label
var threads = GmailApp.search("in:" + label);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var msg = messages[m].getBody();
// Does the message content match the search pattern?
if (msg.search(pattern) !== -1) {
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getBody());
Replace this:替换这个:
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getBody());
With this:有了这个:
// Print the message subject
sheet.getRange(row,3).setValue(getTextFromHtml(messages[m].getBody()));
The getTextFromHtml()
function has been adapted from this answer , with the addition of handling for some basic formatting (numbered & bullet lists, paragraph breaks). getTextFromHtml()
函数已经改编自这个答案,增加了一些基本格式的处理(编号和项目符号列表,段落中断)。
function getTextFromHtml(html) {
return getTextFromNode(Xml.parse(html, true).getElement());
}
var _itemNum; // Used to lead unordered & ordered list items.
function getTextFromNode(x) {
switch(x.toString()) {
case 'XmlText': return x.toXmlString();
case 'XmlElement':
var name = x.getName().getLocalName();
Logger.log(name);
var pre = '';
var post = '';
switch (name) {
case 'br':
case 'p':
pre = '';
post = '\n';
break;
case 'ul':
pre = '';
post = '\n';
itemNum = 0;
break;
case 'ol':
pre = '';
post = '\n';
_itemNum = 1;
break;
case 'li':
pre = '\n' + (_itemNum == 0 ? ' - ' : (' '+ _itemNum++ +'. '));
post = '';
break;
default:
pre = '';
post = '';
break;
}
return pre + x.getNodes().map(getTextFromNode).join('') + post;
default: return '';
}
}
From this answer: Google Apps Scripts - Extract data from gmail into a spreadsheet来自这个答案: Google Apps Scripts - Extract data from gmail into an电子表格
You can forgo the getTextFromHTML function altogether by simply using getPlainBody();只需使用 getPlainBody(); 就可以完全放弃 getTextFromHTML 函数; instead of getBody();.而不是 getBody();。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.