首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google保存Google的附件

Google保存Google的附件
EN

Stack Overflow用户
提问于 2021-12-15 19:22:20
回答 1查看 113关注 0票数 0

基本上,我要做的是把收到的电子邮件中的所有附件放到google的一个文件夹中(大部分是.PDF)。但它说,我不能超过500个附加的文件与搜索功能,我必须使用一个叫做pageToken的东西,我不知道如何应用到我的代码。因此,我需要一些建议或指导,或者一些例子来做这件事。

代码语言:javascript
复制
function saveGmailtoGoogleDrive() {

  const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder

  const searchQuery = 'label:unread has:attachment'; //Filter

  const threads = GmailApp.search(searchQuery, 0, 500);

  

  threads.forEach(thread => {
    const messages = thread.getMessages();
    messages.forEach(message => {

      const attachments = message.getAttachments({
          includeInlineImages: false,
          includeAttachments: true
          
      });

      attachments.forEach(attachment => {

        // Insert the attachment to google drive folder

        Drive.Files.insert(
          {
            title: attachment.getName(),
            mimeType: attachment.getContentType(),
            parents: [{ id: folderId }]
          },
          attachment.copyBlob()
        );
      });
    });
  });
};

代码语言:javascript
复制
function saveGmailtoGoogleDrive() {

  const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder

  const searchQuery = 'label:unread has:attachment'; //Filter

  const threads = GmailApp.search(searchQuery, 0, 500);

  

  threads.forEach(thread => {
    const messages = thread.getMessages();
    messages.forEach(message => {

      const attachments = message.getAttachments({
          includeInlineImages: false,
          includeAttachments: true
          
      });

      attachments.forEach(attachment => {

        // Insert the attachment to google drive folder

        Drive.Files.insert(
          {
            title: attachment.getName(),
            mimeType: attachment.getContentType(),
            parents: [{ id: folderId }]
          },
          attachment.copyBlob()
        );
      });
    });
  });
};

EN

回答 1

Stack Overflow用户

发布于 2021-12-16 01:04:38

search(query, start, max)方法的参数是query, start, maxmax的当前最大值是500。当此值结束时,会发生像Argument max cannot exceed 500.这样的错误。start是搜索的起始位置。所以我想这可以用来实现你的目标。当这反映在您的脚本中时,如下所示。

修改脚本:

发自:

代码语言:javascript
复制
const threads = GmailApp.search(searchQuery, 0, 500);

至:

代码语言:javascript
复制
let [start, end] = [0, 500];
let threads = [];
do {
  const t = GmailApp.search(searchQuery, start, end);
  start += end;
  threads = [...threads, ...t];
} while (threads.length == start);
  • 通过这一修改,您可以在threads中检索超过500封电子邮件。

参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70369249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档