0

basically what I'm trying to do is to get all the attachments within the received emails to a folder in google Drive (there are many, mostly .PDF). But it says I can't go beyond 500 attached files with search function and that I have to use something called pageToken which I have no idea how to apply to my code. So I need some advice or guide or maybe some examples to do this.

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

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()
        );
      });
    });
  });
};
Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

0

The arguments of the method of search(query, start, max) are query, start, max. The current maximum value of max is 500. When this value is over, an error like Argument max cannot exceed 500. occurs. And, start is the start position of the search. So I thought that this can be used for achieving your goal. When this is reflected in your script, it becomes as follows.

Modified script:

From:

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

To:

let [start, end] = [0, 500];
let threads = [];
do {
  const t = GmailApp.search(searchQuery, start, end);
  start += end;
  threads = [...threads, ...t];
} while (threads.length == start);
  • By this modification, you can retrieve the emails in threads more than 500.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hey, thanks for taking your time to reply to my question. I tried the way you showed me but I can't still make it work, it keeps saying this: "Error Exception: Argument max cannot exceed 500." I replaced const threads = GmailApp.search(searchQuery, 0, 500); to the modification you suggested but I keep getting the same error. Do you know by chance how could I apply the pageToken to my code? or how could I make it work I'd really appreciate your help please – Gabriel Belt Dec 17 '21 at 15:49
  • @Gabriel Belt Thank you for replying. I apologize for the inconvenience. Unfortunately, I cannot replicate your situation of `Error Exception: Argument max cannot exceed 500.`. When I tested my proposed script, no error occurs. So can you provide your current script for replicating your issue? By this, I would like to confirm your current issue. About `how could I apply the pageToken to my code?`, `GmailApp.search` doesn't use the pageToken. I apologize for this. Can you provide the official document of the pageToken you expect? – Tanaike Dec 17 '21 at 23:38