我正在嘗試使用 google apps 腳本將具有特定主題行的(Gmail)電子郵件的螢屏截圖獲取到 Google 表格中。我找到了一個可以獲取網站截圖的來源。這是示例代碼:
var siteUrl = "### URL you want to retrieve a screenshot. ###";
var url ="https://www.googleapis.com/pagespeedonline/v4/runPagespeed?screenshot=true&fields=screenshot&url="
encodeURIComponent(siteUrl);
var res = UrlFetchApp.fetch(url).getContentText();
var obj = JSON.parse(res);
var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(obj.screenshot.data),
"image/png",
"sample.png"
);
DriveApp.createFile(blob);
我們可以通過針對特定主題行使用以下行來獲取執行緒:
var threads = GmailApp.search('subject:"Daily Report"')
var msgs = GmailApp.getMessagesForThreads(threads);
但由于我的新手技能,我無法將它們拼接起來以獲得這封特定主題行電子郵件的螢屏截圖。我想知道是否有辦法解決這個問題。任何指導將不勝感激。謝謝你。
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想要使用“pagespeedapi.runpagespeed”和 Google Apps 腳本將 Gmail 郵件匯出為影像。
問題和解決方法:
在現階段,為了使用“pagespeedapi.runpagespeed”,需要使用該站點的公共鏈接。但是,不幸的是,Gmail 中郵件的 URL 不是公共鏈接。這樣,您的目標就不能直接使用“pagespeedapi.runpagespeed”來實作。
當我在電子郵件中以影像形式詢問您所需的值時,您說we can include email subject line, and email body.
。由此,在這個答案中,作為一種解決方法,我想使用以下流程。
- 從 Gmail 郵件中檢索主題和 HTML 正文。
- 使用 將檢索到的主題和 HTML 正文轉換為影像
Charts.newTableChart()
。
當此流程反映在示例腳本中時,它會變成如下。
示例腳本:
// This is from your showing script.
var threads = GmailApp.search('subject:"Daily Report"');
var msgs = GmailApp.getMessagesForThreads(threads);
// I added the blow script.
var subject = msgs[0][0].getSubject();
var htmlBody = msgs[0][0].getBody();
var imageBlob = Charts.newTableChart().setDataTable(
Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, '')
.addRow([`<p style="font-size: 150%">Subject: ${subject}</p>`])
.addRow([htmlBody])
.build()
)
.setOption('allowHtml', true)
.setDimensions(1024, 1024)
.build().getBlob();
// Here, the retrieved image is created as an image file in the root folder. By this, you can confirm the output image. The filename is "sample.png".
DriveApp.createFile(imageBlob.setName("sample.png"));
筆記:
在此示例腳本中,第一條訊息用于
msgs
您的腳本。運行此腳本時,從中檢索第一條訊息
msgs
,并從訊息中檢索主題和 HTML 正文。然后,將它們轉換為 PNG 影像作為 blob。當您將 blob 輸出為影像檔案時,您可以看到主題和 HTML 正文按順序顯示。在此示例中,主題的字體大小為 150%。請根據您的實際情況進行修改。
在此樣本中,影像大小為 1024 x 1024 作為樣本大小。請根據您的實際情況進行修改。
參考:
- 新表格圖表()
- 新資料表()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537472.html