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

时间:2017-08-10 20:50:29

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

我正在尝试将我的Google工作表链接到日历以自动创建日历活动,并在Google工作表中更新时更新它们。我的谷歌工作表跟踪新建筑开放日期和新建筑施工开始日期,因此对于每行我需要它来创建两个日历活动(有时只填写其中一个日期)。

表单的标题是“Loc。#”,“Location”,“Cons Start”和“Whse Open”。 每个标题的值都会从对不同工作表的引用中填充,并由于作为参考而自动从该工作表更新。

我不是最喜欢javascript的,但到目前为止,我对谷歌应用脚​​本的代码如下:

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

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

0 个答案:

没有答案