Jeremy Smith
Jeremy Smith

Reputation: 1

Duplicates in Function

When I hit the trigger, it automatically duplicates the events after each trigger. How do I eliminate this issue:

function myFunction() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var eventCal = CalendarApp.getCalendarById('[email protected]');
  var lr = spreadsheet.getLastRow();
  var count = spreadsheet.getRange("B2:G"+lr+"").getValues();
  for (x=0; x<count.length; x++) {
    var shift = count[x];
    var name = shift[0];
    var description = shift[1];
    var startTime = shift[2];
    var endTime = shift[3];
    var location = shift[4]; 
    var event = {'location': location,'name': name,}
    eventCal.createEvent(description + '-' + name, startTime, endTime, event)
  }
}

Upvotes: 0

Views: 37

Answers (1)

Cooper
Cooper

Reputation: 64140

This adds a column at column G to record that the events are completed.

function myFunction() {
  var sh=SpreadsheetApp.getActiveSheet();
  var eventCal=CalendarApp.getCalendarById('[email protected]');
  var count=sh.getRange(2,2,sh.getLastRow()-1,6).getValues();
  for (x=0;x<count.length; x++) {
    var shift=count[x];
    var name=shift[0];
    var description=shift[1];
    var startTime=shift[2];
    var endTime=shift[3];
    var location=shift[4]; 
    var complete=shift[5];//new column
    var event={'location': location,'name': name,}
    if(complete!='complete') {
      eventCal.createEvent(description + '-' + name, startTime, endTime, event)
      sh.getRange(x+2,7).setValue('complete');
    }
  }
}

(typo fix)

Upvotes: 1

Related Questions