To manage more than 100 hits with one step

202 Views Asked by At

I'm learning Google-App-Script. An I have written a very simply script to manage my emails:

var threads = GmailApp.search('label:Project1 is:unread');
GmailApp.markThreadsRead(threads);

This script works nearly perfect. But when I have more then 100 unread emails in the Label "Porject1" I get the Error-Message that max. 100 Threads are allowed to change.

How can i limit my search command to 99 hits? Or is there another way to manage all hits in one step?

2

There are 2 best solutions below

1
utphx On BEST ANSWER

To answer this part of your question:

How can i limit my searchcommand to 99 hit´s?

you can use:

var threads = GmailApp.search('label:Project1 is:unread',0,100);

Also note the max thread results I believe is 500.

1
Pierre-Marie Richard On

You can use the splice method:

function mailReader(){
  var bigThreads = GmailApp.search('label:Project1 is:unread');

  // While bigthreads bigger than 100 threads
  while(bigThreads.length>99) {

    // Split the bigThreads array in two
    var littlethreads = bigThreads.splice(0,99); 
    // Mark those threads as unread
    GmailApp.markThreadsRead(littlethreads);
  }

  // Mark the rest of the threads on bigThreads
  GmailApp.markThreadsRead(bigThreads);
}