来自Google表单提交的Google日历createAllDayEvent

时间:2015-08-05 11:06:53

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

我正在尝试通过Google日历表单提交创建全天Google日历活动。

我已经设法从互联网上获取以下代码,并使用我的日历ID和列ID修改了它,但我无法弄清楚如何修改它以创建一个全天事件而不是开始/结束事件。

我知道我需要将createEvent(calendarId,title,startDt,endDt,desc)行修改为createAllDayEvent(title, date)行,但我不确定还需要改变什么。

非常感谢任何帮助。

//this is the ID of the calendar to add the event to, this is found on the calendar settings page of the calendar in question
//Look for "Calendar Address:" and the ID shows up beside it.
var calendarId = "CALENDAR_ID_GOES_HERE";

//below are the column ids of that represents the values used in the spreadsheet (these are non zero indexed)

//Column containg the Start Date/Time for the event
var startDtId = 4;
//Column containg the End Date/Time for the event
var endDtId = 5;
//Column containg the First Part of the Title for the event (In this case, Name)
var titleId = 2;
//Column containg the Second part of the Title for the event (In this case, Absence type)
var titleId2 = 3;
//Column containg the Comments for the event
var descId = 7;
//Column containg the Time Stamp for the event (This will always be 1)
var formTimeStampId = 1;

function getLatestAndSubmitToCalendar() {
//Allow access to the Spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();
//Removed setting of Hour and Minute for the Start and End times as these are set i our form
  var startDt = sheet.getRange(lr,startDtId,1,1).getValue();
  var endDt = sheet.getRange(lr,endDtId,1,1).getValue();
//Create an addition to the Description to included who added it and when
  var subOn = "Added :"+sheet.getRange(lr,formTimeStampId,1,1).getValue()+" by: "+sheet.getRange(lr,titleId,1,1).getValue();
//Setting the Comments as the description, and addining in the Time stamp and Submision info
  var desc = "Comments :"+sheet.getRange(lr,descId,1,1).getValue()+"\n"+subOn;
//Create the Title using the Name and tType of Absence
  var title = sheet.getRange(lr,titleId,1,1).getValue()+" - "+sheet.getRange(lr,titleId2,1,1).getValue();
//Run the Crete event Function
    createEvent(calendarId,title,startDt,endDt,desc);
};

function createEvent(calendarId,title,startDt,endDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
  var end = new Date(endDt);
//Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  var loc = 'Computer Centre';

//Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
};

编辑:以下是我尝试修改代码以接受全天活动。我不确定这是否正确。

//this is the ID of the calendar to add the event to, this is found on the calendar settings page of the calendar in question
//Look for "Calendar Address:" and the ID shows up beside it.
var calendarId = "itgeekgroup.com_eakl8j0m8vv2j2cv7jjteq0jd4@group.calendar.google.com";

//below are the column ids of that represents the values used in the spreadsheet (these are non zero indexed)

//Column containg the Start Date/Time for the event
var startDtId = 4;
//Column containg the First Part of the Title for the event (In this case, Name)
var titleId = 2;
//Column containg the Second part of the Title for the event (In this case, Absence type)
var titleId2 = 3;
//Column containg the Comments for the event
var descId = 5;
//Column containg the Time Stamp for the event (This will always be 1)
var formTimeStampId = 1;

function getLatestAndSubmitToCalendar() {
//Allow access to the Spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();
//Create an addition to the Description to included who added it and when
  var subOn = "Added :"+sheet.getRange(lr,formTimeStampId,1,1).getValue()+" by: "+sheet.getRange(lr,titleId,1,1).getValue();
//Setting the Comments as the description, and addining in the Time stamp and Submision info
  var desc = "Comments :"+sheet.getRange(lr,descId,1,1).getValue()+"\n"+subOn;
//Create the Title using the Name and tType of Absence
  var title = sheet.getRange(lr,titleId,1,1).getValue()+" - "+sheet.getRange(lr,titleId2,1,1).getValue();
//Run the Crete event Function
    createAllDayEvent(calendarId,title,startDt,desc);
};

function createAllDayEvent(calendarId,title,startDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
//Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  var loc = 'Computer Centre';

//Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
};

1 个答案:

答案 0 :(得分:0)

您需要使用整个方法。

您在第32行调用它而不引用父方法: createAllDayEvent(calendarId,title,startDt,desc);

在底部添加CalendarApp和getCalendarByID(id),然后调用createAllDayEvent

CalendarApp.getCalendarById("itgeekgroup.com_eakl8j0m8vv2j2cv7jjteq0jd4@group.calendar.google.com").createAllDayEvent(title,date);