简体   繁体   English

在 javascript 中获取 Gmail 邮件正文时删除 html 格式

[英]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.

相关问题 在 Gmail 消息中以 HTML 表的形式通过电子邮件发送 Google 表格范围(带或不带格式) - Emailing Google Sheet range (with or without formatting) as a HTML table in a Gmail message 缺少草稿邮件-JavaScript Gmail API-如何构建请求正文? - Missing draft message - javascript Gmail API - how to structure body of the request? 将javascript输出消息返回到html主体中 - return javascript output message into body of html javascript,如何在将 DOMparser 与 text/html 一起使用时删除元素 - javascript, how to remove the <html><head><body> elements when using DOMparser with text/html 使用JS替换Gmail消息体中的文字 - Use JS to replace text in Gmail message body Gmail:从扩展程序中获取邮件正文 - Gmail: Get a message body from within an extension html body 中的 Javascript 遇到时会执行吗? - Does Javascript in the html body execute when encountered? 尝试从 JavaScript 数组中删除 JSON 对象时收到错误消息 - Getting an error message when I try to remove a JSON object from a JavaScript Array 格式化使用JavaScript发送的电子邮件的正文 - Formatting the body of an email sent with JavaScript 当我们回复电子邮件时,如何从gmail正文中删除跟踪像素? - How to remove tracking pixel from gmail body when we reply to a email?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM