【问题标题】:How to take screenshot of specific subjectline (Gmail) email using Google Apps Script?如何使用 Google Apps 脚本截取特定主题 (Gmail) 电子邮件的屏幕截图?
【发布时间】:2022-11-21 12:44:30
【问题描述】:

我正在尝试使用 google apps 脚本将具有特定主题行的(Gmail)电子邮件的屏幕截图获取到 Google 表格中。我找到了一个source,可以获取网站的截图。这是示例代码:

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);

但由于我的新手技能,我无法将它们拼接起来以获得这封特定主题行电子邮件的屏幕截图。我想知道是否有办法解决这个问题。任何指导将不胜感激。谢谢你。

【问题讨论】:

  • 首先,对于我的示例脚本对您的情况没有用,我深表歉意。关于你的放映脚本,我是2021年12月9日更新的,请大家注意。但是,不幸的是,在当前阶段,无法使用“方法:pagespeedapi.runpagespeed”直接检索来自Gmail 的邮件的屏幕截图。因为 Gmail 邮件的 URL 不能公开共享。因此,在这种情况下,需要使用变通方法。但是,我无法想象您对 get the screenshot of the (Gmail) email 的预期结果。例如,您需要的屏幕截图只是 HTML 正文?
  • 感谢您的回复,是的,我只想要我们打开任何电子邮件时看到的界面截图。
  • 谢谢你的回复。关于I just want the screenshot of the interface which we see when we open any email.,我认为在现阶段,这是无法实现的,因为Gmail邮件的URL不能公开分享。我为此道歉。
  • 有没有解决办法,因为我们没有公开分享,我们只是使用我们想要截图的个人电子邮件地址授权脚本
  • 貌似在使用“pagespeedapi.runpagespeed”的时候要求url是public link。但是,Gmail 的 URL 不是公共链接。这样,当我的示例脚本与 Gmail 消息的 URL 一起使用时,将检索登录屏幕。所以,我问了我的解决方法。但是,从yes, I just want the screenshot of the interface which we see when we open any email.,我了解到我的解决方法对您的情况没有用。我为此道歉。

标签: parsing google-apps-script google-sheets gmail screenshot


【解决方案1】:

我相信你的目标如下。

  • 您想使用“pagespeedapi.runpagespeed”和 Google Apps 脚本将 Gmail 邮件导出为图像。

问题和解决方法:

在现阶段,为了使用“pagespeedapi.runpagespeed”,需要使用该站点的公共链接。但是,不幸的是,Gmail 中邮件的 URL 不是公共链接。这样,您的目标就不能直接使用“pagespeedapi.runpagespeed”来实现。

当我在电子邮件中以图像形式询问您所需的值时,您说 we can include email subject line, and email body.。由此,在这个答案中,作为一种解决方法,我想使用以下流程。

  1. 从 Gmail 邮件中检索主题和 HTML 正文。
  2. 使用Charts.newTableChart() 将检索到的主题和 HTML 正文转换为图像。

    当此流程反映在示例脚本中时,它会变成如下。

    示例脚本:

    // 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 作为样本大小。请根据您的实际情况进行修改。

    参考:

【讨论】:

  • 太棒了,你是一个传奇。它按预期工作。再次非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2018-03-08
  • 2020-12-30
  • 2012-07-25
  • 2023-03-30
相关资源
最近更新 更多