在Google表格中插入日历ID

时间:2014-12-18 18:38:55

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

我正在尝试使用我找到的代码Here

将事件从Google工作表导出到Google日历

我已更新它以适合我的工作表,但股票代码用单位替换了单元格中的公式,使工作表无用。我尝试替换最后一行

range.setValues(data);

range.setFormulas(data);

但这只是使所有细胞都空白。

是否可以使用.setValues来设置Id?

我的代码的Coppy忘了添加

 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 headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "abeaqo4gf3of7u64sa8uufulpc@group.calendar.google.com";
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[21]);  // First column
    var title = row[22];           // Second column
    var tstart = new Date(row[23]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[24]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[25];
    var desc = row[26];
    var id = row[27];    // Sixth column == eventId
    var tep = row[28];
    if (i > tep) continue;
    // Check if event already exists, update it if it does
    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();
      row[27] = newEvent;  // Update the data array with event ID
    }
    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;
  }
  // Record all event IDs to spreadsheet

} 

1 个答案:

答案 0 :(得分:0)

您可以使用范围获取特定单元格(您要更新事件ID的位置),并将值设置为每行中的该单元格,而不是将所有数据设置在for循环之外。

这是代码。您可以相应地更改行和列值:

  function exportEvents() {
     var sheet = SpreadsheetApp.getActiveSheet();
     var headerRows = 1;  // Number of rows of header info (to skip)
     var range = sheet.getDataRange();
     Logger.log(range.getLastRow());
     var data = range.getValues();
     Logger.log(data);
     var calId = "your calendar id";
     var cal = CalendarApp.getCalendarById(calId);
     for (i in data) {
       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

        var event = cal.getEventSeriesById(id);
        //Logger.log(id);

        range =  SpreadsheetApp.getActiveSheet().getRange(i, 6);

        if (!event) {

      var newEvent = cal.createEvent(title, tstart, tstop,{description:desc,location:loc}).getId();
      //row[6] = newEvent;  // Update the data array with event ID
      range.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);
        range.setValue(event.getId());
    }
     //sheet.getRange(i,6).setValue(newEvent); 
   }

 }