Execute script without ending when there is an error in apps script

0

I have a code in apps script where I delete an event in calendar with the following instruction

var calendar = CalendarApp.getCalendarById("[email protected]")
.getEventSeriesById(IdParaBorrar).deleteEventSeries()

It works well when the event exists, but when the event (IdParaDelete) does not exist, the code stops.

I can not find a way to delete the event only if it exists and continue without marking an error.

Do you know how I can achieve it?

    
asked by Fernando Tovar 09.02.2017 в 05:13
source

2 answers

1

His thing is to do it in two parts. The first to recover the CalendarEventSeries :

var eventSeries = CalendarApp.getCalendarById("[email protected]").getEventSeriesById(IdParaBorrar);

do the check and then the deleteEventSeries() .

What I do not remember very well is if it returns a null or you have to verify some property as getId() .

I hope it serves you.

    
answered by 09.02.2017 / 08:23
source
1

Short answer

An alternative is to use try ... catch

Explanation

Google Apps Script is based on JavaScript so it is possible to use many of the techniques for the Flow control and error handling .

From the link in the short answer section (code format is mine to highlight the commands)

  

The statement of try consists of a block try , it contains one or more instructions, and at least one clause catch or finally , or both.

Additional bibliography

answered by 09.02.2017 в 14:15