Home > Net >  How to change title of an event?
How to change title of an event?

Time:04-07

I am trying to update the title of the events in the last 7 days in a specific calendar, when an user submits a form.

I have this code now:

function myFunction() {

var form = FormApp.openById('1g4gA7glYWRKOVqOi3z3qxyEGbJgaSg_2Jkry_uOp0Cc');
var responses = form.getResponses();
var len = responses.length;
var last = len - 1;
var items = responses[last].getItemResponses();
var email = responses[last].getRespondentEmail();
var equipment = items[1].getResponse();
var cal = CalendarApp.getCalendarsByName(equipment);


var d = new Date();
var ms = d.getTime();
var sevenDays = 7*24*60*60*1000;
var minus7daysMs = ms - sevenDays;
var minus7days = new Date(minus7daysMs);
Logger.log('Number of events: '   minus7days);
var events = cal[0].getEvents(minus7days, d);
var title = events.getName;
Logger.log('Event title: '   title);
events.setTitle('Returned');
}

However, I get that error when running the code: TypeError: events.setTitle is not a function myFunction @ update.gs:22

I tried many different ways of doing it, but it always ends with an error. Could you please tell me what I am doing wrong?

Thank you very much for your help!

CodePudding user response:

From your showing script, getEvents(startTime, endTime) returns CalendarEvent[]. And setTitle(title) is a method of Class CalendarEvent. In your script, at events.setTitle('Returned'), setTitle is used to an array. I think that the reason of your issue is due to this. When you want to change all events in events with setTitle('Returned'), how about the following modification?

From:

events.setTitle('Returned');

To:

events.forEach(e => e.setTitle('Returned'));
  • By this modification, the title of all events in events are changed.

References:

  • Related