Google App 脚本在将解压缩的 CSV 附加到 Google 表格之前解压缩 Gmail 附件

发布于 2025-01-09 21:54:38 字数 1019 浏览 0 评论 0原文

我对 Google App Script 完全陌生。我正在尝试构建一些东西,因为我找不到完全符合我想要的功能的 Google 插件。但我不知道我在做什么!有人愿意帮忙吗?

目标:我需要一个脚本来识别来自特定发件人的具有特定主题行的每日电子邮件何时到达我的 Gmail 收件箱。电子邮件始终带有 .zip 附件。我需要脚本来 1) 解压附件 - 这会给我一个 csv 文件,然后我需要 2) 将解压的 csv 文件附加到 Google 表格中。

这是我从别人的脚本开始的地方:

function importDailyCallVolumes() {
  // Get the newest Gmail thread based on sender and subject
  var gmailThread = GmailApp.search("from:[email protected] subject:\"Daily Call Vols\"", 0, 1)[0];
  
  // Get the attachments of the latest mail in the thread.
  var attachments = gmailThread.getMessages()[gmailThread.getMessageCount() - 1].getAttachments();
  
  // Here I need to add some code to unzip the attachment - how?

  // Get and and parse the CSV from the unzipped attachment
  var csv = Utilities.parseCsv(attachments[0].getDataAsString());
  return csv;
}

非常感谢任何愿意帮助应用程序脚本新手的人!

I am completely new to Google App Script. I'm trying to build something because I can't find a Google add-on that does exactly what I want. But I have no idea what I'm doing! Is anyone willing to help?

Goal: I need a script that will identify when a daily email with a particular subject line and from a particular sender arrives in my gmail inbox. The email always has a .zip attachement. I need the script to 1) unzip the attached - which gives me a csv file, and then I need 2) the unzipped csv file to be appended to a Google Sheet.

Here is where I am starting with someone else's script:

function importDailyCallVolumes() {
  // Get the newest Gmail thread based on sender and subject
  var gmailThread = GmailApp.search("from:[email protected] subject:\"Daily Call Vols\"", 0, 1)[0];
  
  // Get the attachments of the latest mail in the thread.
  var attachments = gmailThread.getMessages()[gmailThread.getMessageCount() - 1].getAttachments();
  
  // Here I need to add some code to unzip the attachment - how?

  // Get and and parse the CSV from the unzipped attachment
  var csv = Utilities.parseCsv(attachments[0].getDataAsString());
  return csv;
}

Thank you so much to anyone willing to help an App Script newbie!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2025-01-16 21:54:38

将 CSV 压缩文件附加到活动工作表

function appendzipcsv() {
  const ibts = GmailApp.getInboxThreads();
  for (let i = 0; i < ibts.length; i++) {
    let ms = ibts[i].getMessages();
    for (let j = 0; j < ms.length; j++) {
      let a = ms[j].getAttachments();
      if (a && a.length > 0) {
        Logger.log(JSON.stringify(a));
        if (a[0].getContentType() == MimeType.ZIP) {
          let s = Utilities.unzip(a[0].copyBlob())[0].getDataAsString();
          Logger.log(s);
          let vs = Utilities.parseCsv(s);
          let ss = SpreadsheetApp.getActive();
          let sh = ss.getActiveSheet();
          sh.getRange(sh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
        }
      }
    }
  }
}

Utilities.parseCsv( )

Append CSV zipped file to Active sheet

function appendzipcsv() {
  const ibts = GmailApp.getInboxThreads();
  for (let i = 0; i < ibts.length; i++) {
    let ms = ibts[i].getMessages();
    for (let j = 0; j < ms.length; j++) {
      let a = ms[j].getAttachments();
      if (a && a.length > 0) {
        Logger.log(JSON.stringify(a));
        if (a[0].getContentType() == MimeType.ZIP) {
          let s = Utilities.unzip(a[0].copyBlob())[0].getDataAsString();
          Logger.log(s);
          let vs = Utilities.parseCsv(s);
          let ss = SpreadsheetApp.getActive();
          let sh = ss.getActiveSheet();
          sh.getRange(sh.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
        }
      }
    }
  }
}

Utilities.parseCsv()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文