【问题标题】:Google Apps Script Gmail getPlainBody Line BreaksGoogle Apps 脚本 Gmail getPlainBody 换行符
【发布时间】:2022-01-03 05:19:27
【问题描述】:

使用 Google Apps Script Gmail 库时,当我使用函数 GmailMessage.getPlainBody() 时,API 似乎将过去的一个段落拆分为多个,可能会使用字符限制。例如,我的电子邮件中有一段内容如下:

From the link you sent me, I gleaned that Gmail refers to their secure email as confidential.

但是当我在邮件上调用这个函数时,它变成了:

From the link you sent me, I gleaned that Gmail refers to their
secure email as confidential.

而且,当我在新的行分隔符上拆分电子邮件文本并进行一些清理以使用我的输出创建一个数组时,我最终得到:

['From the link you sent me, I gleaned that Gmail refers to their', 'secure email as confidential.']

我查看了this Reddit post,似乎处理了类似的问题。但是,我尝试了提出问题的人建议的解决方案:

body =  message.getPlainBody().replace(/\r\n\r\n/gm,'aaaLINEBREAKERaaa').replace(/\r\n/gm,' ').replace(/aaaLINEBREAKERaaa/gm, '\r\r').replace(/  /gm,' ')

它并没有完全满足我的需求。有没有其他人遇到过这个问题,如果有,您有建议的解决方法吗?谢谢!

【问题讨论】:

  • 虽然我不确定你的实际情况,但我提出了一个我曾经使用过的解决方法。你能确认一下吗?如果这对您的情况没有帮助,我深表歉意。
  • 看起来很棒!如果其他人有想法,仍然会感激另一种选择,但这暂时可行!

标签: google-apps-script gmail gmail-api


【解决方案1】:

我有同样的问题。在这种情况下,我使用了一种解决方法。

查看邮件时,发现邮件正文中包含了 HTML 正文,并且 HTML 正文中有原始段落,我使用了这种情况。因此,在此解决方法中,从 HTML 正文中检索原始文本,并将 HTML 转换为文本。这样就得到了原始段落。示例脚本如下。

示例脚本:

此脚本使用 Drive API 将 HTML 转换为文本。所以pelase enable Drive API at Advanced Google services

var message =  // Here, please use your "message".

var html = message.getBody();
var id = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, Utilities.newBlob(html, MimeType.HTML)).id;
var text = DocumentApp.openById(id).getBody().getText(); // or DocumentApp.openById(id).getBody().getText().trim();
DriveApp.getFileById(id).setTrashed(true);
console.log(text)

参考资料:

【讨论】:

  • 这是一个有趣的提议——谢谢!稍后我将有机会对其进行测试:)
  • 我想这会满足我的需要——谢谢!!
  • @Lle.4 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
相关资源
最近更新 更多