I’m developing an automation script using Google Apps Script to streamline the process of creating events in Google Calendar from data stored in a Google Sheets spreadsheet. My problems here are:
Retrieving the Hangouts link after creating the event: I need assistance with retrieving the Hangouts link associated with a calendar event immediately after creating it through Apps Script. This link is essential for integrating video conferencing functionality into the events.
Designating an attendee as a co-organizer: I would also like to designate one of the attendees as a co-organizer of the event. However, I’m unsure about the specific method or property to use in Google Apps Script to achieve this.
function crearEvento() {
let ultimaCelda = encontrarUltFila();
// Recorrer las filas con datos en la hoja "Calendar"
for (let fila = 3; fila <= ultimaCelda; fila++) {
const nombreEvento = hojaCalendario.getRange("B" + fila).getValue();
const fechaInicio = hojaCalendario.getRange("C" + fila).getValue();
const fechaFin = hojaCalendario.getRange("D" + fila).getValue();
const coanfitrion = hojaCalendario.getRange("E" + fila).getValue();
const calendarID = hojaCalendario.getRange("F" + fila).getValue();
const valorDia = hojaTecnicas.getRange('I' + fila).getValue();
const calendar = CalendarApp.getCalendarById(calendarID);
try {
const options = {
guests: coanfitrion,
sendInvites: false,
conferenceDataVersion: 1
};
// Verificar si valorDia es un día válido y construir la recurrencia
let recurrence;
switch (valorDia) {
case "Lunes":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.MONDAY);
break;
case "Martes":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.TUESDAY);
break;
case "Miercoles":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY);
break;
case "Jueves":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.THURSDAY);
break;
case "Viernes":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.FRIDAY);
break;
case "Sabado":
recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekday(CalendarApp.Weekday.SATURDAY);
break;
default:
console.log("Día no válido:", valorDia);
continue; // Saltar a la siguiente iteración del bucle
}
const fechaFinalizacion = new Date("2024-7-31");
recurrence.until(fechaFinalizacion);
// Crear el evento con o sin recurrencia
if (recurrence) {
const eventSeries = calendar.createEventSeries(nombreEvento, new Date(fechaInicio), new Date(fechaFin), recurrence, options);
const eventSeriesId = eventSeries.getId();
hojaCalendario.getRange("G" + fila).setValue(eventSeriesId);
console.log('Evento recurrente creado:', nombreEvento, 'Fecha de inicio:', fechaInicio, 'Fecha de fin:', fechaFin, 'Día de la semana:', valorDia);
} else {
const event = calendar.createEvent(nombreEvento, new Date(fechaInicio), new Date(fechaFin), options);
const eventoId = event.getId();
hojaCalendario.getRange("G" + fila).setValue(eventoId);
console.log('Evento creado:', nombreEvento, 'Fecha de inicio:', fechaInicio, 'Fecha de fin:', fechaFin, 'Día de la semana:', valorDia);
}
} catch (error) {
console.error('Error al crear el evento:', error);
}
}
}
function modificarEventos(){
let ultimaCelda = encontrarUltFila();
// Recorrer las filas con datos en la hoja "Calendar"
for (let fila = 3; fila <= ultimaCelda; fila++) {
const colorF = hojaTecnicas.getRange("F" + fila).getValue();
const calendarID = hojaCalendario.getRange("F" + fila).getValue();
const eventId = hojaCalendario.getRange("G" + fila).getValue(); // Obtener el ID del evento
try {
// Retrieve the event object using calendar ID and event ID
const event = CalendarApp.getCalendarById(calendarID).getEventById(eventId);
if (event) {
// Determinar color según el valor de la columna F
let color;
switch(colorF) {
case "FI":
color = CalendarApp.EventColor.PALE_BLUE; // Pavo real
break;
case "FH":
color = CalendarApp.EventColor.PALE_RED; // Flamenco
break;
case "FJSyC":
color = CalendarApp.EventColor.GREEN; // Salvia
break;
case "FAE":
color = CalendarApp.EventColor.YELLOW; // Banana
break;
default:
color = null; // Sin color específico
}
event.setColor(color);
event.setGuestsCanInviteOthers(false)
event.setGuestsCanSeeGuests(true)
event.setGuestsCanModify(false)
} else {
// Log a message if the event is not found
console.log('Event not found for calendar ID:', calendarID, 'and event ID:', eventId);
}
} catch (error) {
// Handle potential errors during event modification
console.error('Error modifying event:', error);
}
}
}
function encontrarUltFila(){
var ultimaFila = hojaCalendario.getLastRow();
var valoresColumnaF = hojaCalendario.getRange('B1:B' + ultimaFila).getValues();
for (var i = ultimaFila; i > 0; i--) {
if (valoresColumnaF[i - 1][0] != "") {
var ultimaCelda = i;
Logger.log("La última celda con valor en la columna B es: " + ultimaCelda);
break;
}
}
return ultimaCelda;
}
Luca Invernizzi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.