
[英]Add or Update Google Calendar Event from Google Sheet with App Script
[英]Google Scripts - Add guests to a calendar event from a google sheet
我创建了一个 google 表单,它转到一个自动创建日历条目的 google 表格。 但是,我无法弄清楚如何从我的工作表中邀请客人。 我已经彻底搜索了它并在这里<\/a>和这里<\/a>找到了一些类似的解决方案,但他们的代码与我的完全不同,所以我无法弄清楚如何将它应用到我的代码中。
这是我最近的尝试:
//insert your google calendar ID
var calendarId = "CALENDARID";
//index (starting from 1) of each column in the sheet
var titleIndex = 4;
var descriptionIndex = 7;
var startDateIndex = 5;
var endDateIndex = 6;
var googleCalendarIndex = 10;
var locationIndex = 8;
var mainGuestIndex = 2;
/*
find the row where the Google Calendar Event ID is blank or null
The data of this row will be used to create a new calendar event
*/
function findRow( sheet )
{
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for ( var loopCounter = 0;
loopCounter < values.length;
loopCounter++ )
{
if ( values[ loopCounter ][ googleCalendarIndex - 1 ] == "" ||
values[ loopCounter ][ googleCalendarIndex - 1 ] == null )
newEvent( i + 1 );
}
};
/*
get the data of the new row by calling getSheetData() and
create a new Calendar event by calling submitToGoogleCalendar()
*/
function newEvent( row )
{
var sheet = SpreadsheetApp.getActiveSheet();
var eventId = submitToGoogleCalendar(getSheetData(sheet,row),null)
if ( eventId != null )
sheet.getRange( row,
googleCalendarIndex,
1,
1 ).setValue( eventId );
};
/*
Store the data of a row in an Array
*/
function getSheetData(sheet,row)
{
var data = new Array();
data.title = sheet.getRange( row,
titleIndex,
1,
1 ).getValue();
data.description = sheet.getRange( row,
descriptionIndex,
1,
1 ).getValue();
data.startDate = sheet.getRange( row,
startDateIndex,
1,
1 ).getValue();
data.endDate = sheet.getRange( row,
endDateIndex,
1,
1 ).getValue();
data.location = sheet.getRange( row,
locationIndex,
1,
1 ).getValue();
data.mainGuest = sheet.getRange( row,
mainGuestIndex,
1,
1 ).getValue();
return data;
};
/*
if a cell is edited in the sheet, get all the data of the corresponding row and
create a new calendar event (after deleting the old event) by calling submitToGoogleCalendar()
*/
function dataChanged( event )
{
var sheet = SpreadsheetApp.getActiveSheet();
var row = event.range.getRow();
var eventId = sheet.getRange( row,
googleCalendarIndex,
1,
1 ).getValue();
var eventId = submitToGoogleCalendar( getSheetData( sheet,
row),
eventId );
if ( eventId != null )
sheet.getRange( row,
googleCalendarIndex,
1,
1 ).setValue( eventId );
};
/*
This function creates an event in the Google Calendar and returns the calendar event ID
which is stored in the last column of the sheet
*/
function submitToGoogleCalendar( sheetData,
eventId )
{
// some simple validations ;-)
if ( sheetData.title == "" ||
sheetData.startDate == "" ||
sheetData.startDate == null )
return null;
var cal = CalendarApp.getCalendarById( calendarId );
var start = new Date( sheetData.startDate );
var end = new Date( sheetData.endDate );
// some simple date validations
if ( start > end )
return null;
var event = null;
//if eventId is null (when called by newEvent()) create a new calendar event
if ( eventId == null )
{
event = cal.createEvent( sheetData.title,
start,
end,
{
description : sheetData.description,
location : sheetData.location,
} ).addGuest( sheetData.mainGuest );
return event.getId();
}
/*
else if the eventid is not null (when called by dataChanged()), delete the calendar event
and create a new event with the modified data by calling this function again
*/
else
{
event = cal.getEventSeriesById( eventId );
event.deleteEventSeries();
return submitToGoogleCalendar( sheetData,
null );
}
return event.getId();
};
您必须将其作为带有位置的高级选项包含在内。 sendInvites 是第四个选项,值为 true 或 false 以向客人发送电子邮件。
event = cal.createEvent( sheetData.title, start, end, { description : sheetData.description, location : sheetData.location, guests : sheetData.mainGuest, sendInvites : true } );
Acredito que você precisa usar desta maneira。
calendar.createEvent(TITULO, DATA INICIAL, DATA FINAL, { description : Descrição da Reunião, location : Local da Reunião, guest : lista dos convidados, sendInvites : true - Enviar e-mail para os convidados });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.