Home > Software engineering >  Is there a way to check if a next event includes a term in the title?
Is there a way to check if a next event includes a term in the title?

Time:04-27

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 ...

CodePudding user response:

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'});
    Logger.log(today);
    if(events.length == 0)
    {
      Logger.log("False")
    }
    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:

CodePudding user response:

How about this:

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