使用Google Scripts中的getCalendarById创建一个AllDayEvent

时间:2015-04-12 20:00:50

标签: google-apps-script

尝试使用getCalendarById创建一个AllDayEvent但没有成功。我当前的脚本将使用getDefaultCalendar运行,但是当我尝试用getCalendarById替换时,我收到一条错误消息,"动作不被允许"。

Code.gs:

//Numeric IDs for Column values

//Column containing PBG code
var titleId = 3
//Column containing delivery period
  var titleId2 = 5
//Column containing delivery notes
  var descId = 8
//Column containing TimeStamp
  var formTimeStampId = 1
//Column containing Regional Coordinator
  var regionalcoordinatorId = 2
//Column containing certification status
  var certId = 6
//Column containing delivery date
  var delivdateId = 4
//Column containing Delivery Coordinator
  var deliverycoordinatorId = 9
//Column containing dozens
  var dozensId = 7  

//Calendar Id
  var calendarId = "CALENDAR_ID_GOES_HERE"


function sendFormToCalendar() {
//Allow access to spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();
  var dataRange = sheet.getRange(lr,1,1);
  var data = dataRange.getValues();
  var title = sheet.getRange(lr,titleId,1,1).getValue()+" :: "+sheet.getRange(lr,dozensId,1,1).getValue()+" :: "+sheet.getRange(lr,titleId2,1,1).getValue();

//Increasing content for Description
    var subOn = "Posted:"+sheet.getRange(lr,formTimeStampId,1,1).getValue()+"\n"+"Posted By: "+sheet.getRange(lr,deliverycoordinatorId,1,1).getValue();
    var desc = "Certification:"+sheet.getRange(lr, certId, 1, 1).getValue()+"\n"+sheet.getRange(lr,descId,1,1).getValue()+"\n"+subOn;
    var loc = 'Perry Import-Export SA';  

//Create an all-day event and log the ID
  var date = sheet.getRange(lr,delivdateId,1,1).getValue();
  var event = CalendarApp.getDefaultCalendar().createAllDayEvent(title, new Date(date),
                                                                 {location: loc, description: desc});
 Logger.log('Event ID: ' + event.getId());
  }

1 个答案:

答案 0 :(得分:0)

以下是正确的语法。如果日历不存在或您没有访问权限,则getCalendarById()将返回null。我猜这个错误是从createAllDayEvent()抛出的。如果您打破下面的呼叫,您将能够更好地确定错误的来源。

var cal = CalendarApp.getCalendarById(calendarId);
var event =  cal.createAllDayEvent(title, new Date(date),{location: loc, description: desc});

编辑:回应以下评论:

设置写入权限的简单方法是转到日历设置 - >分享此日历设置。如果您的帐户具有“所有者”访问权限(“在日历设置中进行更改并管理共享”),您也可以使用高级日历服务在代码中执行此操作。

var scope = Calendar.newAclRuleScope();
scope.type = "user";
scope.value ="user@example.com";

var aclRule = Calendar.newAclRule();
aclRule.scope = scope
aclRule.role = "writer";

// This is shorthand for all of the above
// {"scope":{"value":"user@example.com", "type":"user"}, "role":"writer"}

Calendar.Acl.insert(aclRule, "myCalendarId")