【问题标题】:Email Pdf attachments to Google sheet通过电子邮件将 PDF 附件发送到 Google 表格
【发布时间】:2021-01-14 12:36:43
【问题描述】:

我正在寻找可以让我从邮件 PDF 附件中获取 Google 表格中的数据的东西。

我们都经常在电子邮件中收到 PDF 附件,如果我们在谷歌表格中获得全部数据,那就太好了。

如果有类似情况请告诉我

【问题讨论】:

  • 欢迎来到 Stack Overflow!如果您只是在寻找一个完成的工具来手动运行以提取内容,那么 superuser.com 可能更适合(它也适用于 Stack Overflow/StackExchange 帐户)。这个网站更多的是为自己编写程序的编程相关问题。如果我误读了您的问题,我深表歉意。

标签: email google-apps-script google-sheets


【解决方案1】:

解释:

您的问题非常广泛,无法给出具体答案,因为该答案将取决于 pdf,还取决于您想要从中获取的数据,以及您跳过提及的所有其他细节。

  • 在这里,我将提供一个通用代码 sn-p,您可以使用它从 gmail 附件中获取 pdf,然后将其转换为文本(字符串)。对于此文本,您可以使用一些正则表达式(必须非常具体地针对您的用例)来获取所需的信息,然后将其放入工作表中。

代码sn-p:

主要代码就是这个。您应该只修改此代码:

function myFunction() {
  const queryString = "label:unread from example@gmail.com" // use your email query
  const threads = GmailApp.search(queryString);
  const threadsMessages = GmailApp.getMessagesForThreads(threads);
  const ss = SpreadsheetApp.getActive();

  for (thr = 0, thrs = threads.length; thr < thrs; ++thr) {
   let messages = threads[thr].getMessages(); 
     for (msg = 0, msgs = messages.length; msg < msgs; ++msg) { 
       let attachments = messages[msg].getAttachments();
        for (att = 0, atts = attachments.length; att < atts; ++att) { 
          let attachment_Name = attachments[att].getName();
      
      let filetext = pdfToText( attachments[att], {keepTextfile: false} );
      Logger.log(filetext)
      // do something with filetext
      // build some regular expression that fetches the desired data from filetext
      // put this data to the sheet
   }}}
}

pdfToTextMogsdad 实现的函数,你可以找到here。只需复制粘贴该链接中提供的代码 sn-p 以及我在此答案中发布的myFunction。您还可以使用一些选项,这些选项在我提供的链接中得到了很好的解释。需要注意的重要一点是,要使用此库,您需要从资源中启用 Drive API

这将帮助您入门,如果您遇到任何无法找到解决方案的问题,您应该在此处创建一个包含问题的具体细节的新帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2020-12-15
    相关资源
    最近更新 更多