如何在脚本编辑器中的创建日历事件中引用指定数量的列中的活动行?

时间:2016-06-12 13:16:57

标签: google-apps-script google-sheets

我正在尝试通过Google电子表格在日历中创建活动。 我需要脚本只引用工作表的前6列,而只引用包含数据的单元格

var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getSheets()[0].getRange("A8:G11");
SpreadsheetApp.setActiveRange(range);
var headerRows = 0;  // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();

Operations sheet

1 个答案:

答案 0 :(得分:0)

通过一点帮助,我可以很好地完成所有工作。 我只是遍历保存事件ID的单元格并更新它们,而不是更新每个单元格。 这是完整的代码。感谢社区帮助我解决这个问题。

function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};

/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();

var headerRows = 6;  // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getDisplayValues();
// var calId = "your cal id"; // PRODUCTION
var calId = "your cal id"; // TEST
Logger.log("length");
Logger.log(data.length);
var cal = CalendarApp.getCalendarById(calId);
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; // Skip header row(s)
var row    = data[i];
var date   = new Date(row[0]);  // First column
var title  = row[1];           // Second column
var tstart = new Date(row[2]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date(row[3]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc  = row[4];
var desc = row[5];
var id   = row[6];              // Sixth column == eventId
// Check if event already exists, update it if it does
Logger.log("+++++++++++++++++++++")
Logger.log(id.length);
if (id.length > 0) {
  try {
    var event = cal.getEventSeriesById(id);
  }
  catch (e) {
    // do nothing - we just want to avoid the exception when event     doesn't exist
  } 
}
if (!event) {
  //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new   Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
  var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
  var r = i + 1;
  var cell = sheet.getRange("G" + r);
  cell.setValue(newEvent);
}
else {
  event.setTitle(title);
  event.setDescription(desc);
  event.setLocation(loc);
  // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
  // ... but we CAN set recurrence!
  var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
  event.setRecurrence(recurrence, tstart, tstop);
}
debugger;

} }