简体   繁体   English

GAS:从电子表格创建日历事件

[英]GAS: Create calendar event from spreadsheet

I've the following columns in a spreadsheet:我在电子表格中有以下列:

Email Email Vorname首字母 Name名称 Position Position Thema主题 19.02.23 23 年 2 月 19 日 22.02.23 22.02.23 28.02.23 28.02.23
ab@gmail.com ab@gmail.com Diego迭戈 Flores弗洛雷斯 AB AB C C 1 1个
bc@gmail.com bc@gmail.com Alex亚历克斯 Flores弗洛雷斯 DB D B F F 1 1个
c.d@gmail.com c.d@gmail.com Diego迭戈 Sanchez桑切斯 GB国标 D 1 1个
de@gmail.com de@gmail.com Alex亚历克斯 Sanchez桑切斯 FB面子书 G G 1 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') { .

    • I thought that the reason of your current issue of 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.2322.02.2328.02.23的日期是否为日期 object。

From the above situation, how about the following modification?从上面的情况来看,下面的修改怎么样?

From:从:

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);
}

To:到:

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);
}
  • If the date of 19.02.23 , 22.02.23 , and 28.02.23 in your header titles are the date object, var date = headers[j];如果您的 header 标题中的19.02.2322.02.2328.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"); . .

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM