Good morning,
I’ve implemented the provided code, and it works as expected when I make changes to an event in my primary calendar. However, when I try to modify an event in another calendar that I’ve created, the code doesn’t run. This issue occurs because the ‘owner’ variable is not correctly defined or is empty in my code.
// Cette fonction met à jour les titres des événements dans un calendrier.
function updateEventTitle(e) {
// Obtenez l'ID du calendrier depuis l'objet de déclenchement.
var calendarId = e.calendarId;
// Obtenez le calendrier par son ID.
var calendar = CalendarApp.getCalendarById(calendarId);
Logger.log('calendarId' + calendarId);
// Obtenez les propriétés de l'utilisateur pour stocker le jeton de synchronisation.
var properties = PropertiesService.getUserProperties();
var options = {};
// Récupérez le jeton de synchronisation.
var syncToken = properties.getProperty('syncToken');
if (syncToken) {
// Ajoutez le jeton de synchronisation aux options.
options.syncToken = syncToken;
} else {
// Sinon, synchronisez les événements à partir de la date actuelle.
options.timeMin = new Date().toISOString();
}
var eventResponse;
var pageToken = null;
try {
do {
// Liste des événements avec les options de synchronisation.
var eventResponse = Calendar.Events.list(calendarId, options);
pageToken = eventResponse.nextPageToken;
if (eventResponse.nextSyncToken != null) {
// Mettez à jour le jeton de synchronisation.
options.syncToken = eventResponse.nextSyncToken;
properties.setProperty('syncToken', eventResponse.nextSyncToken);
}
var events = eventResponse.items;
for (var j = 0; j < events.length; j++) {
// Obtenez la description du calendrier.
var descriptionCalendrier = calendar.getDescription();
// Obtenez un événement par son ID.
var event = calendar.getEventById(events[j].id);
// Obtenez le titre de l'événement.
var title = event.getTitle();
// Obtenez la description de l'événement.
var description = event.getDescription();
// Récupérez la liste des participants de l'événement.
var participants = event.getGuestList(true);
// Récupérez le nombre de participants maximum (limite).
var limitEventMatch = description.match(/limites*:s*(d+)/i);
var limitCalendrierMatch = descriptionCalendrier.match(/limites*:s*(d+)/i);
if(limitEventMatch) {
limitparticipant = limitEventMatch[1];
//Logger.log('limitEventMatch' + limitEventMatch[1]);
} else {
if(limitCalendrierMatch) {
limitparticipant = limitCalendrierMatch[1];
//Logger.log('limitCalendrierMatch' + limitCalendrierMatch[1]);
} else {
//Logger.log('aucun');
var limitparticipant = 1;
}
}
Logger.log('event'+event)
// Obtenez l'adresse e-mail de l'organisateur (créateur) de l'événement.
const guestListWithoutOwner = event.getGuestList(); // get the guest list without the owner
// Comptez le nombre de participants.
var numberOfParticipants = guestListWithoutOwner.length;
// filter the guest list to ingore duplicate emails leaving only the owner
// based on https://stackoverflow.com/a/42995029/1027723, https://pulse.appsscript.info/p/2022/04/getting-a-google-calendar-event-owner-using-google-apps-script/
Logger.log('guestListWithoutOwner'+guestListWithoutOwner)
var owner = participants.filter(o1 => !guestListWithoutOwner.some(o2 => o1.getEmail() === o2.getEmail()));
Logger.log('owner'+owner[0]);
var organizerEmail = owner[0].getEmail();
Logger.log('organizerEmail'+organizerEmail);
// Savoir si l'organisateur participe
organisateurParticipe = 'YES';
Logger.log('participants'+participants[i]);
for (var i = 0; i < participants.length; i++) {
Logger.log(participants[i].getEmail());
if (participants[i].getEmail() == organizerEmail) {
if(participants[i].getGuestStatus() == 'NO') {
organisateurParticipe = false;
}
break;
}
}
/*
Atelier
*/
// Récupérez si il y a ANNULÉ dans le titre.
if (title.indexOf("Atelier") !== -1 && !limitEventMatch && !limitCalendrierMatch) {
limitparticipant = 10; // Définissez la limite à 10 s'il y a "Atelier" dans le titre et pas de limite définie.
description += "nlimite : " + limitparticipant; // Ajoutez la limite dans la description.
event.setDescription(description); // Mettez à jour la description de l'événement.
}
/*
NOMBRE DE PARTICIPANTS
*/
// Récupérez le nombre d'organisateurs maximum (limite).
var organisateurMatch = description.match(/organisateurs*:s*(d+)/i);
var limitorganisateur = organisateurMatch ? organisateurMatch[1] : 1;
// Recherche le compteur existant dans le titre.
var titleMatch = title.match(/(d+/d+)/);
if (numberOfParticipants === 0) {
// Si aucun participant, retirez le compteur.
title = titleMatch ? title.replace(titleMatch[0], '') : title;
} else {
// Calculez le nombre de participants en soustrayant le nombre d'organisateurs.
var participants = parseInt(numberOfParticipants) - parseInt(limitorganisateur); // -1 on retire l'organisateur
// Mettez à jour le titre avec le nombre de participants et la limite.
// Si le compteur existe déjà dans le titre, je le mets à jour, sinon je l'ajoute
title = titleMatch
? title.replace(titleMatch[0], '(' + participants + '/' + limitparticipant + ')')
: title + ' (' + participants + '/' + limitparticipant + ')';
}
/*
ANNULÉ
*/
// Récupérez la couleur initiale
var couleurMatch = description.match(/couleurs*:s*(d+)/i);
if (couleurMatch) {
var couleurInitiale = parseInt(couleurMatch[1]);
} else {
// Si la couleur initiale n'est pas définie, obtenez la couleur actuelle de l'événement
var couleurInitiale = event.getColor() ? event.getColor() : 0;
// Ajoutez la couleur à la description
description += "ncouleur : " + couleurInitiale;
// Mettez à jour la description de l'événement
event.setDescription(description);
}
// Récupérez si il y a ANNULÉ dans le titre.
var annuleMatch = /ANNULÉ /.test(title);
if (!organisateurParticipe) {
if(!annuleMatch) {
title = 'ANNULÉ ' + title;
}
event.setColor(CalendarApp.EventColor.GRAY);
} else {
title = title.replace('ANNULÉ ', '');
// Restaurez la couleur initiale
event.setColor(couleurInitiale);
}
// Mettez à jour le titre de l'événement.
event.setTitle(title);
}
options.nextPageToken = pageToken;
} while (pageToken != null);
} catch (ex) {
// Gérez les exceptions s'il y en a.
Logger.log('Exception: %s', ex);
}
}
I attempted alternatives solutions, such as the one below, but it did not yield the expected results:
const guestListWithoutOwner = event.getGuestList() || [];
var numberOfParticipants = guestListWithoutOwner.length;
if (numberOfParticipants > 0) {
var owner = participants.filter(o1 => !guestListWithoutOwner.some(o2 => o1.getEmail() === o2.getEmail()));
if (owner.length > 0) {
var organizerEmail = owner[0].getEmail();
}
} else {
// Handle the scenario when there are no participants (or no owner) if necessary.
}
Do you have any insights into why this might be happening?
Thank you very much,
Guillaume.



