无法从谷歌表单创建一整天的活动系列到谷歌日历

时间:2015-03-09 17:10:33

标签: google-apps-script scripting google-calendar-api google-form

我已经尝试了好几天,阅读其他帖子,玩其他接近相同目的的脚本,但没有任何作用。我正在尝试创建一个脚本,该脚本将从基于Web的Google表单中获取信息,以及一个月/日,并将其转换为日历中的重新发生的事件。

它终于发布到了日历,但是每个事件都在2015年12月31日之前未定义 - 没有进一步的信息,至少它是重复发生的。

我会非常感激任何帮助,因为我试图理解这种编码以及如何做到这一点。谢谢!

//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
var calendarId = "id@group.calendar.google.com";

//below are the column ids of that represents the values used in the spreadsheet (these are non zero indexed)
var startDtId = 5;
var endDtId = 5;
var titleId = 2;
var descId = 3;
var formTimeStampId = 1;

function getLatestAndSubmitToCalendar() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();
  var startDt = sheet.getRange(lr,startDtId,1,1).getValue();
  //set to first hour and minute of the day.
//  startDt.setHours(0);
//  startDt.setMinutes(00);
  var endDt = sheet.getRange(lr,endDtId,1,1).getValue();
  //set endDt to last hour and minute of the day
//  endDt.setHours(23);
//  endDt.setMinutes(59);
//  var subOn = "Submitted on :"+sheet.getRange(lr,formTimeStampId,1,1).getValue();
  var desc = sheet.getRange(lr,descId,1,1).getValue();
  var title = sheet.getRange(lr,titleId,1,1).getValue();
  createAllDayEvent(calendarId,title,startDt,endDt,recurrence,loc,desc);
}​

  function createAllDayEventSeries(calendarId,title,startDt,endDt,recurrence,loc,desc) {
  var cal = CalendarApp.getCalendarById('id@group.calendar.google.com');
  var start = new Date(startDt);
  var end = new Date(endDt);
  var loc = descId;
  var desc = "Happy Birthday "+titleId+" of "+descId;
// Creates a rule that recurs every week for ten weeks.
  var recurrence = CalendarApp.newRecurrence().addYearlyRule();

  var event = cal.createAllDayEventSeries(title, start, recurrence, {
      description : desc,
      location : loc
  });
};

1 个答案:

答案 0 :(得分:0)

我创建了一个表单,并使用以下代码进行测试:

// Column data constants
var nameCol = 2;
var birthdayCol = 3;
var descriptionCol = 4;
var locationCol = 4;
var calendarId = '[id]@group.calendar.google.com';

/* Send Confirmation Email with Google Forms */

function Initialize() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  ScriptApp.newTrigger("CreateCalendarEvent")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit()
  .create();
}

function createEvent() {
  var ss = SpreadsheetApp.getActiveSheet();
  var rows = ss.getDataRange();
  var lr = rows.getLastRow();
  var start = ss.getRange(lr,birthdayCol,1,1).getValue();
  start.setHours(0);
  start.setMinutes(00);

  var title = ss.getRange(lr,nameCol,1,1).getValue() + " Birthday";
  var desc = ss.getRange(lr,descriptionCol,1,1).getValue();
  var loc = ss.getRange(lr,locationCol,1,1).getValue();

  var recurrence = CalendarApp.newRecurrence().addYearlyRule();

  Logger.log("accessing calendar");
  var externalCalendar = CalendarApp.getCalendarById(calendarId);

  externalCalendar.createAllDayEventSeries(title, start, recurrence,         {
      description : desc,
      location : loc
  });
}

function getRelativeDate(daysOffset, hour) {
  var date = new Date();
  date.setDate(date.getDate() + daysOffset);
  date.setHours(hour);
  date.setMinutes(0);
  date.setSeconds(0);
  date.setMilliseconds(0);
  return date;
}

function CreateCalendarEvent(e) {
  try {
    Logger.log("creating event");
    createEvent();
  } catch (e) {
    Logger.log(e.toString());
  }

}

这会在提交表单时设置触发器功能,请确保将日历ID的值更改为日历提供的值。