[英]GAS: Create calendar event from spreadsheet
I've the following columns in a spreadsheet:我在电子表格中有以下列:
Email ![]() |
Vorname![]() |
Name![]() |
Position ![]() |
Thema![]() |
19.02.23 ![]() |
22.02.23 ![]() |
28.02.23 ![]() |
---|---|---|---|---|---|---|---|
ab@gmail.com ![]() |
Diego![]() |
Flores![]() |
AB ![]() |
C ![]() |
1 ![]() |
||
bc@gmail.com ![]() |
Alex![]() |
Flores![]() |
DB ![]() |
F ![]() |
1 ![]() |
||
c.d@gmail.com ![]() |
Diego![]() |
Sanchez![]() |
GB![]() |
D![]() |
1 ![]() |
||
de@gmail.com ![]() |
Alex![]() |
Sanchez![]() |
FB![]() |
G ![]() |
1 ![]() |
The spreadsheet is linked with the following google apps script:该电子表格与以下谷歌应用程序脚本链接:
function createCalendarEvents() {
var spreadsheetId = '1NQULPeaxoaAaNWU4ojapb4R50JEdl62Ip2e9yjUB_sw';
var calendarId = "diego.flores@gmail.com";
var sheetName = '2';
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
var sheet = spreadsheet.getSheetByName(sheetName);
var calendar = CalendarApp.getCalendarById(calendarId);
var data = sheet.getDataRange().getValues();
var headers = data.shift();
// console.log("spreadsheet: "+spreadsheet);
// console.log("sheet: "+sheet);
// console.log("calendar: "+calendar);
// console.log("headers: "+headers);
// console.log("data: "+data);
console.log("email: "+email);
for (var i = 0; i < data.length; i++) {
var row = data[i];
var email = row[0];
var firstName = row[1];
var lastName = row[2];
var position = row[3];
var topic = row[4];
console.log("email: "+email);
for (var j = 5; j < row.length; j++) {
if (row[j] === '1') {
var date = new Date(headers[j]);
var eventTitle = firstName + " " + lastName + " - " + position + " - " + topic;
var event = calendar.createEvent(eventTitle, date, date);
event.setLocation("Office");
event.addEmailReminder(15);
}
}
}
}
When a column contains a 1 in the date columns, the script should create an event filled with infos from the row in the calender diego.flores@gmail.com.当日期列中的列包含 1 时,脚本应创建一个事件,其中填充日历 diego.flores@gmail.com 中行的信息。 But something it is not working, no calendar entry is created.
但是它不起作用,没有创建日历条目。 I'm 100% sure I have the authorization for the calendar and the script is connected to the right sheet.
我 100% 确定我拥有日历的授权并且脚本已连接到正确的工作表。 Calendar and Spreadsheet API is activated.
日历和电子表格 API 已激活。 Maybe someone can give me a hint or tips.
也许有人可以给我提示或提示。 Thanks!
谢谢!
From your reply and your script,从您的回复和脚本中,
I think that in your script, if (row[j] === '1') {
should be if (row[j] === 1) {
when row[j]
is a number, or if (row[j] == '1') {
.我认为在你的脚本中,
if (row[j] === '1') {
应该是if (row[j] === 1) {
当row[j]
是一个数字时,或者if (row[j] == '1') {
.
No calendar event is created by executing the script
might be due to this.No calendar event is created by executing the script
的原因可能是由于此。 From your sample data, I'm not sure whether the date of 19.02.23
, 22.02.23
, and 28.02.23
in your header titles are the date object.根据您的示例数据,我不确定您的 header 标题中的
19.02.23
、 22.02.23
和28.02.23
的日期是否为日期 object。
From the above situation, how about the following modification?从上面的情况来看,下面的修改怎么样?
if (row[j] === '1') {
var date = new Date(headers[j]);
var eventTitle = firstName + " " + lastName + " - " + position + " - " + topic;
var event = calendar.createEvent(eventTitle, date, date);
event.setLocation("Office");
event.addEmailReminder(15);
}
if (row[j] == "1") {
var date = headers[j] instanceof Date ? headers[j] : Utilities.parseDate(headers[j], Session.getScriptTimeZone(), "dd.MM.yy");
var eventTitle = firstName + " " + lastName + " - " + position + " - " + topic;
var event = calendar.createEvent(eventTitle, date, date);
event.setLocation("Office");
event.addEmailReminder(15);
}
19.02.23
, 22.02.23
, and 28.02.23
in your header titles are the date object, var date = headers[j];
19.02.23
、 22.02.23
和28.02.23
的日期是日期 object,则var date = headers[j];
can be used instead of {var date = headers[j] instanceof Date? headers[j]: Utilities.parseDate(headers[j], Session.getScriptTimeZone(), "dd.MM.yyyy");
{var date = headers[j] instanceof Date? headers[j]: Utilities.parseDate(headers[j], Session.getScriptTimeZone(), "dd.MM.yyyy");
{var date = headers[j] instanceof Date? headers[j]: Utilities.parseDate(headers[j], Session.getScriptTimeZone(), "dd.MM.yyyy");
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.