【发布时间】:2023-01-11 11:51:30
【问题描述】:
我正在使用 Goolge App Scripts,以便 Google 表单可以自动创建一个在 Google 日历上每周重复的事件系列。现在,该表单要求用户选择事件发生的所有工作日并将它们放入一个数组中。
要创建事件,我需要将数组的每个元素转换为 CalendarApp.Weekday 对象。这是我所拥有的:
event = ['MONDAY', 'TUESDAY', 'THURSDAY', 'SATURDAY']; //Placed here as an example; this is
defined by the user.
event1 = []; // New array to hold CalendarApp.Weekday objects.
for (i = 0; i < event.length-1; i++); {
event1[i] = `CalendarApp.Weekday.${event[i]}`
}//Expected output: [CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.TUESDAY,
CalendarApp.Weekday.THURSDAY, CalendarApp.Weekday.SATURDAY].
代码返回:[null, null, null, CalendarApp.Weekday.SATURDAY]。
对于我尝试的任何天数组合,除了最后一项返回预期输出外,所有内容都返回“null”。知道为什么,以及如何解决它?
非常感谢你。
【问题讨论】:
-
关于
Why is this forEach loop returning 'null' for all elements but the last?,当我看到你的脚本时,好像没有使用forEach。所以,我担心您可能抄错了脚本。这个怎么样? -
你有一个额外的
;,这使得你的for循环为空。在for循环退出后(i为3),进行单一赋值。关闭为错字。
标签: javascript arrays google-apps-script google-calendar-api google-forms