使用 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.']
我查看了這篇 Reddit 帖子,它似乎處理了類似的問題。但是,我嘗試了提出問題的人建議的解決方案:
body = message.getPlainBody().replace(/\r\n\r\n/gm,'aaaLINEBREAKERaaa').replace(/\r\n/gm,' ').replace(/aaaLINEBREAKERaaa/gm, '\r\r').replace(/ /gm,' ')
它并沒有完全滿足我的需要。有沒有其他人遇到過這個問題,如果有,您有建議的解決方法嗎?謝謝!
uj5u.com熱心網友回復:
我遇到過同樣的問題。在這種情況下,我使用了一種解決方法。
查郵件的時候發現郵件正文中包含了HTML正文,HTML正文有原始段落,就用了這種情況。因此,在此變通方法中,從 HTML 正文檢索原始文本并將 HTML 轉換為文本。這樣就得到了原始段落。示例腳本如下。
示例腳本:
此腳本使用 Drive API 將 HTML 轉換為文本。因此,請在 Advanced Google services 中啟用 Drive API。
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)
參考:
- 獲取身體()
- 檔案:插入
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/402866.html
標籤:
上一篇:如果使用谷歌應用程式腳本單元格為空白,如何設定帶有條件的公式?
下一篇:拉取多個存盤庫后如何保留分支
