'Is there a way to check if a next event includes a term in the title?

I have made a script that updates events title after an user submits a form, based on this condition:

for(var j=0; j<events.length;j++){
        var ev = events[j];
        if(!ev.getTitle().includes("Returned"){ ...

I want the script to check if there is one or many events that include the term "Returned", but only among the next events.

For example, the condition should be false if an event title does not include "Returned" AND none of the next events titles include "Returned".

Is that possible? I tried with creating a variable evnext = events[j+1] but of course I get an error once it gets to the last event.

Thank you for your help, I feel it is very doable but my understanding of loops is sadly too low ...

Edit: details on my goal

Actually my script sends an email to all people who borrowed an equipment but did not confirm they returned it.

The problem now is that I would like to send an email to these people EXCEPT IF someone else returned it afterwards.

It would indeed mean that even the first borrower did not confirm he returned it, the fact that someone who took it next and marked it as returns proves that the first person actually returned the equipment.

Otherwise it would not have been possible for the next guy to return it.



Solution 1:[1]

How about this:

for(var j=0; j<events.length;j++){
        var ev = events[j];
        if(j < events.length - 1) {
          if(ev.getTitle().include("Returned") && events[j+1].include("Returned")) {}
        } else {
          if(!ev.getTitle().includes("Returned") {}
        }

Solution 2:[2]

You can try the following script:

function listEvents()
{
  let calendar = CalendarApp.getCalendarById("Calendar ID"); // Change the calendar ID
  var today = new Date();
  for(let i=1; i<=30; i++) // 30 days limit
  {
    var events = calendar.getEventsForDay(today, {search: 'Returned'});
    if(events.length != 0)
    {
      MailApp.sendEmail("[email protected]","Subject","Body");
    }
    today.setDate(today.getDate() + 1);
  }
}

In this example the script takes the current date and starts checking day by day of a specific calendar for the upcoming 30 days if an event has the word "Returned" on its title and returns False if not.

Let me know if this helps. I can also modify the script if needed. You can modify the 30 days limit, and the initial date depending on your needs. Do not forget to change the calendar ID to the one you are currently using to save the events.

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2