简体   繁体   中英

How can I access enum value with a string in Google Apps Script?

So, I am currently working on a project where I collect the data from a google form and then create event(s) in the google calendar based on the response. I am using Google Apps Script for this purpose. My form lets the user input one or more weekdays in which they want the event to occur.

But weekdays are stored in an enum. Here's the link to the documentation.

This is how one can create a recurring event every Wednesday at a given time between specified dates.

CalendarApp.getDefaultCalendar().createAllDayEventSeries(event_name,start_date,
CalendarApp.newRecurrence().addWeeklyRule()
           .onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY)
           .until(end_date));

Now, I am having a list of weekdays with me like ['Monday', 'Wednesday'] . This is stored in an object called data . Here is my approach for creating events using such a list:

 for(var i=0;i<data["Weekdays"].length;++i)
{
    var day = data["Weekdays"][i].toUpperCase();
    CalendarApp.getDefaultCalendar().createAllDayEventSeries(data["SubjectName"],new Date(start_date),
    CalendarApp.newRecurrence().addWeeklyRule()         
               .onlyOnWeekday(CalendarApp.Weekday[day])                     
               .until(end_date));
}

I am new to web development yet and have never worked with enums before. I apologize in advance for any mistakes or uncertainty in my question.

In addition with Cooper's comment.

By using for loop, you are calling the API multiple times. Instead, use onlyOnWeekdays(days)

Example:

var recurrence = CalendarApp.newRecurrence()
    .addWeeklyRule().onlyOnWeekdays(
        [CalendarApp.Weekday.TUESDAY, CalendarApp.Weekday.THURSDAY]);

In your code:

for(var i=0;i<data["Weekdays"].length;++i)
{
    var day = data["Weekdays"][i].toUpperCase();
    CalendarApp.getDefaultCalendar().createAllDayEventSeries(data["SubjectName"],new Date(start_date),
    CalendarApp.newRecurrence().addWeeklyRule()         
               .onlyOnWeekdays([CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.WEDNESDAY])                    
               .until(end_date));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM