Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Google App Script pageToken to save attachments to Google Drive

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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:

  • search(query, start, max)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!