2

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
J. Doe
  • 151
  • 12

2 Answers2

2

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);
}
Pierre-Marie Richard
  • 1,914
  • 2
  • 19
  • 25
0

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.

utphx
  • 1,287
  • 1
  • 8
  • 19
  • Hello, thanks. I checke the searchcommands only [this site](https://support.google.com/mail/answer/7190?hl=en). [Here](https://developers.google.com/apps-script/reference/gmail/gmail-app#searchquery-start-max) i see the command. Thanks – J. Doe Aug 16 '17 at 10:26