mercredi 10 janvier 2018

Using an If statement to create different types of calendar events

I'm using a spreadsheet to update Google Calendar for scheduling. I want to be able to have it create an all-day event when there is nobody scheduled but am not clear how to proceed.

Here's my code so far.

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the exportEvents() function.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * http://ift.tt/VRwPtT
 */
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 lc = sheet.getLastColumn()
  var data = range.getValues();
  var calId = "7cm4jkd7g5ohmfsougc4b90r94@group.calendar.google.com"; //actual
  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[13];           // Fourteenth column
    var tstart = new Date(row[3]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[4]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = "1117 w 24th Street, Los Angeles, CA 90007";
    var desc = row[14];
    var id = row[15];              // Sixteenth column == eventId actual
    // Check if event already exists, delete it if it does
    try {
      var event = cal.getEventById(id)
      event.deleteEvent();
      row[15] = '';  // Remove event ID    
      }
    catch (e) {
        // do nothing - we just want to avoid the exception when event doesn't exist
      }
      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[15] = newEvent;  // Update the data array with event ID
    debugger;


    }
// Record all event IDs to spreadsheet
var id_data = data.map(function (row) {
  return [row[15]];      // keep only that column
});
sheet.getRange(1, 16, id_data.length, 1).setValues(id_data);  // write it in the sheet
}

I think that if I have an IF statement that looks to see if my tstart variable is blank could then trigger it to create an all-day event, and then if it's not blank create an event as it currently does. I'm just not clear how to make that happen, my attempts so far have each broken the code.

Aucun commentaire:

Enregistrer un commentaire