Básicamente, lo que estoy tratando de hacer es obtener todos los archivos adjuntos dentro de los correos electrónicos recibidos en una carpeta en Google Drive (hay muchos, en su mayoría .PDF). Pero dice que no puedo ir más allá de 500 archivos adjuntos con la función de búsqueda y que tengo que usar algo llamado pageToken que no tengo idea de cómo aplicar a mi código. Así que necesito algún consejo o guía o tal vez algunos ejemplos para hacer esto.
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() ); }); }); }); };Los argumentos del método de search(query, start, max) son query, start, max . El valor máximo actual de max es 500 . Cuando se supera este valor, se produce un error como Argument max cannot exceed 500. . Y, start es la posición de inicio de la búsqueda. Entonces pensé que esto puede usarse para lograr su objetivo. Cuando esto se refleja en su guión, se convierte en lo siguiente.
const threads = GmailApp.search(searchQuery, 0, 500); 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 de más de 500.