javascript - 链接谷歌表格以自动创建日历事件并在更新谷歌表格时更新它们

标签 javascript google-apps-script google-sheets google-calendar-api

我正在尝试将我的 google 表格链接到日历以自动创建日历事件并在 google 表格中更新时更新它们。我的 google 表格跟踪新建筑开放日期和新建筑施工开始日期,因此我需要它为每一行创建两个适用的日历事件(有时只填写一个日期)。

工作表的标题是“Loc. #”、“Location”、“Cons Start”和“Whse Open”。 这些标题中的每一个的值都是从对不同工作表的引用填充的,并且由于是引用而从该工作表自动更新。

我不是最喜欢 javascript 的,但到目前为止,我为 google apps 脚本编写的代码如下:

// Calendar ID can be found in the "Calendar Address" section of the Calendar Settings.
var calendarId = 'costco.com_19rlkujqr1v5rfvjjj8p1g8n8c@group.calendar.google.com';

// Configure the year range you want to synchronize, e.g.: [2006, 2017]
var years = [2017,2020];

// Date format to use in the spreadsheet.
var dateFormat = 'M/d/yyyy H:mm';

var titleRowMap = {
  'loc#': 'Loc. #',
  'location': 'Location',
  'conStart': 'Cons Start',
  'whseOpen': 'Whse Open',
};
var titleRowKeys = ['loc#', 'location', 'conStart', 'WhseOpen'];
var requiredFields = ['loc#', 'location', 'conStart', 'WhseOpen'];

// This controls whether email invites are sent to guests when the event is created in the
// calendar. Note that any changes to the event will cause email invites to be resent.
var SEND_EMAIL_INVITES = false;

// Setting this to true will silently skip rows that have a blank start and end time
// instead of popping up an error dialog.
var SKIP_BLANK_ROWS = false;

// Updating too many events in a short time period triggers an error. These values
// were tested for updating 40 events. Modify these values if you're still seeing errors.
var THROTTLE_THRESHOLD = 10;
var THROTTLE_SLEEP_TIME = 75;

// Adds the custom menu to the active spreadsheet.
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
    {
      name: "Update from Calendar",
      functionName: "syncFromCalendar"
    }, {
      name: "Update to Calendar",
      functionName: "syncToCalendar"
    }
  ];
  spreadsheet.addMenu('Calendar Sync', menuEntries);
}

// Creates a mapping array between spreadsheet column and event field name
function createIdxMap(row) {
  var idxMap = [];
  for (var idx = 0; idx < row.length; idx++) {
    var fieldFromHdr = row[idx];
    for (var titleKey in titleRowMap) {
      if (titleRowMap[titleKey] == fieldFromHdr) {
        idxMap.push(titleKey);
        break;
      }
    }
    if (idxMap.length <= idx) {
      // Header field not in map, so add null
      idxMap.push(null);
    }
  }
  return idxMap;
}

// Converts a spreadsheet row into an object containing event-related fields
function reformatEvent(row, idxMap, keysToAdd) {
  var reformatted = row.reduce(function(event, value, idx) {
    if (idxMap[idx] != null) {
      event[idxMap[idx]] = value;
    }
    return event;
  }, {});
  for (var k in keysToAdd) {
    reformatted[keysToAdd[k]] = '';
  }
  return reformatted;
}

不太确定下一步该怎么做才能实现这一目标。关于如何实现这个有什么建议吗?

最佳答案

您可以尝试查看这些教程和有关如何使用 Google Apps 脚本创建日历事件的问题。这些链接包含一些示例代码,您可以复制这些代码。

有关更多信息或更多想法,您还可以查看本教程,了解如何使用 Google Apps Script in creating a calendar events from Google Form .

关于javascript - 链接谷歌表格以自动创建日历事件并在更新谷歌表格时更新它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623256/

相关文章:

javascript - 防止用户删除 TextInput 上的默认值

javascript - 未选中特定复选框时, react 切换选择字段不会删除

time - 如何使用 google apps 脚本在 googlespreadsheet 中读取时间?

javascript - 如何循环遍历嵌套数组,然后使用 Javascript 以预定义的顺序存储输出

google-apps-script - 将 gzip 压缩的数据导入 Google 表格

google-apps-script - "MMMM yy"- 谷歌电子表格中的日期

javascript - 我应该如何正确触发更改事件以在单元测试中触发函数?

javascript - 如何访问http请求响应中的 'this'?

google-sheets - 根据 Google 电子表格上的日期切换行背景

regex - 在 Google 表格中的列中的所有单元格内的子字符串后求和多个值