Google脚本 - 将来宾添加到Google工作表中的日历活动中

时间:2017-01-25 20:18:54

标签: javascript google-apps-script google-sheets google-calendar-api

我已经创建了一个google表单,该表单会转到google表格,从中自动创建日历条目。但是,我无法弄清楚如何从我的工作表中邀请客人。我已经彻底搜索了它并找到了类似的解决方案herehere,但是他们的代码与我的代码完全不同,所以我无法弄清楚如何将它应用到我的代码中。< / p>

这是我最近的尝试:

//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();
};

然而,当我运行它时会发生一个事件,但仍然没有客人出现在日历事件中。来自here我知道我需要使用.addGuest(email),它似乎应该放在submitToGoogleCalendar函数中,因为事件中的所有其他信息都是,但是到目前为止没有运气。我还尝试添加event.addGuest(sheetData.mainGuest);,而不是将其添加到事件变量的事件中,但这也不起作用。

如何在不完全重写脚本的情况下添加此内容?我在这里走在正确的轨道上吗?

1 个答案:

答案 0 :(得分:0)

您必须将其作为带位置的高级选项包含在内。 sendInvites是第四个选项,其值为true或false,以通过电子邮件发送给客人。

Class Calendar: Create Event

&#13;
&#13;
event = cal.createEvent( sheetData.title,
                                 start,
                                 end,
                                 { 
                                     description : sheetData.description,
                                     location    : sheetData.location,
                                     guests      : sheetData.mainGuest,
                                     sendInvites : true
                                 } );
&#13;
&#13;
&#13;

相关问题