Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda