0

有谁知道为什么这个简单的代码不起作用?:

var formattedSched = new Date(sched).setHours(15);

我只是想通过应用程序脚本向谷歌日历中的日历事件添加时间。我什至在 w3 页面上使用基本相同的样本设定时间。http://www.w3schools.com/jsref/jsref_sethours.asp

但是每次我运行脚本在谷歌日历中创建一个事件时,我都会收到一条错误消息,谷歌认为它是一个数字而不是日期对象。没有 setHours() 它可以正常工作,但有了它,谷歌认为这是一个数字。谢谢

更新:好的,我拆分了一个测试函数来尝试记录正在发生的事情:

function testsetcal() {

  var formattedSched = new Date(); Logger.log(formattedSched.setHours(15))

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

没有setHours的 Logger 结果是:Wed Nov 21 08:12:33 PST 2012

使用setHours 的记录器结果是:1.35352881266E12 它看起来与 w3 页面上的示例结果完全不同。

更新:仍然无法使用与主变量分离的 setHours 获得相同的记录器结果:1.35352881266E12

function testsetcal() {

  var formattedSched = new Date(); 
  var withHours = formattedSched.setHours(15);

  Logger.log(withHours);

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', withHours, withHours);
}

更新:好的哇 - 这很奇怪。我去我的谷歌日历删除了所有的测试事件,至少只是我认为成功的那些。然后我发现比我想象的要多,有些设置为 1500 - 然后我回到代码并发现 setHours() 在谷歌日历创建事件之外工作,如下所示:

function testsetcal() {  
  var formattedSched = new Date(); 

  formattedSched.setHours(15);

  Logger.log(formattedSched.setHours(15));

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

因此,此代码将小时设置为 1500,但该部分代码不包含在事件的创建中。完全让我感到惊讶,但这似乎是解决方案。

4

3 回答 3

0

我想我应该继续并关闭它。感谢您的回复。对我有用的代码可以在一段时间内将事件带入 GCal:

function testsetcal() {  
  var formattedSched = new Date(); 

  formattedSched.setHours(15);

  Logger.log(formattedSched.setHours(15));

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

.setHours必须在.createEvent.

于 2012-11-21T16:52:19.920 回答
0

不完全是,您当前正在将 setHours 响应返回给 var,请尝试

var formattedSched = new Date(sched);
formattedSched.setHours(15);
于 2012-11-21T15:48:30.067 回答
0

您需要按照您引用的示例将其分开。

var formattedSched = new Date(sched);
formattedSched.setHours(15);

您正在尝试实现setHours()尚未创建的日期对象。

于 2012-11-21T15:49:53.143 回答