Code is executed in IF statement when condition is not met

Adam Caudell

I have a sheet with over 400 calendar events and I am trying to write a script that will take these events from my spreadsheet and create events for them in my Google Calendar. To prevent the script from duplicating events every time it runs, I am setting the eventID as a check in the last column of my spreadsheet. The logic is that if the startTime column and endTime columns are not blank and the EventID column IS blank, then the script should run and create a new event, then log the ID into the last column.

I am finding that the code logs an eventID in the last column all the time, which means it creates an event every time, even when the conditions are not met.

function createCalendarEvent() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('MasterList');
  var data = sheet.getSheetValues(3, 1, sheet.getLastRow(), 12);
  var calendar = CalendarApp.getCalendarById('*CalendarID*');

  for (var i = 0; i <= sheet.getLastRow(); i++) {
    var title = data[i][0]; //String
    var date = new Date(data[i][1]); //Date
    var startTime = new Date(data[i][9]); //DateTime
    var endTime = new Date(data[i][10]); //DateTime
    var location = data[i][5]; //String
    var status = data[i][11]; //String
    var allDay = data[i][3]; //String

    if (allDay === 'ALL DAY' && status === ''){
      var allDayEvent = calendar.createAllDayEvent(title, date, {location: location});
      sheet.getRange(i + 3, 12).setValue(allDayEvent.getId());
    }
    else if (startTime !== '' && endTime !== '' && status === '' ) {
      var event = calendar.createEvent(title, startTime, endTime, {location: location});
      sheet.getRange(i + 3, 12).setValue(event.getId());
    } 
    else {
      Logger.log('\nRow %s is missing start and/or end time or the event has already been created.\n', i + 3);
    }
  }
}
Adam Caudell

Based on suggestions in the comments, I determined that the "empty" cells were being treated at the "zero" date of Google Sheets, which is 12/30/1899 0:00:00. So while I could not see a value in the cell, the code was still grabbing that value. Once I set the condition that the endTime had to be greater than the startTime, it worked out (and flushed out a few errors in the spreadsheet where one of the users indeed entered end times that were before the start time).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash condition not met when executed by crontab

From Dev

condition statement executes when conditions are not met

From Dev

VBA IF statement works when condition is met but get mismatch error when condition not met.

From Dev

"if" statement, condition will never be executed

From Dev

Python if statement skipping code even though condition is met

From Dev

Python if statement skipping code even though condition is met

From Dev

Having my else statement executed even if condition isn't met in jQuery

From Dev

Sql-Exit CASE Statement when condition is met

From Dev

How to stop execution of PHP script when a certain condition is met in IF statement?

From Dev

If statement not stopping after condition is met

From Dev

IF statement that changes based on met condition

From Dev

How to stop code when a condition is met c#, Debuging

From Dev

If condition is not met but code executes anyways

From Dev

If condition is not met but code executes anyways

From Dev

How to end code if "If" Condition is not met

From Dev

delete trigger when condition is met

From Dev

Add "?" to a string when a condition is not met

From Dev

Reset List When Condition Met

From Dev

Given an IF-ELSE statement inside a for loop, can I only skip the IF when the condition is met once? python

From Dev

Code not executed without a print statement

From Dev

Code not executed without a print statement

From Dev

Skip if-statement once condition is met

From Dev

Will an If Statement Stop Checking if the first OR condition is met in PHP?

From Dev

Trigger bootstrap modal if a condition statement is not met

From Dev

If Statement condition not being recognised despite being met

From Dev

Will an If Statement Stop Checking if the first OR condition is met in PHP?

From Dev

second condition not being met when condition is called

From Dev

Evaluating condition continues when condition is not met

From Dev

update statement after the condition met in select statement (mysql)

Related Related

  1. 1

    Bash condition not met when executed by crontab

  2. 2

    condition statement executes when conditions are not met

  3. 3

    VBA IF statement works when condition is met but get mismatch error when condition not met.

  4. 4

    "if" statement, condition will never be executed

  5. 5

    Python if statement skipping code even though condition is met

  6. 6

    Python if statement skipping code even though condition is met

  7. 7

    Having my else statement executed even if condition isn't met in jQuery

  8. 8

    Sql-Exit CASE Statement when condition is met

  9. 9

    How to stop execution of PHP script when a certain condition is met in IF statement?

  10. 10

    If statement not stopping after condition is met

  11. 11

    IF statement that changes based on met condition

  12. 12

    How to stop code when a condition is met c#, Debuging

  13. 13

    If condition is not met but code executes anyways

  14. 14

    If condition is not met but code executes anyways

  15. 15

    How to end code if "If" Condition is not met

  16. 16

    delete trigger when condition is met

  17. 17

    Add "?" to a string when a condition is not met

  18. 18

    Reset List When Condition Met

  19. 19

    Given an IF-ELSE statement inside a for loop, can I only skip the IF when the condition is met once? python

  20. 20

    Code not executed without a print statement

  21. 21

    Code not executed without a print statement

  22. 22

    Skip if-statement once condition is met

  23. 23

    Will an If Statement Stop Checking if the first OR condition is met in PHP?

  24. 24

    Trigger bootstrap modal if a condition statement is not met

  25. 25

    If Statement condition not being recognised despite being met

  26. 26

    Will an If Statement Stop Checking if the first OR condition is met in PHP?

  27. 27

    second condition not being met when condition is called

  28. 28

    Evaluating condition continues when condition is not met

  29. 29

    update statement after the condition met in select statement (mysql)

HotTag

Archive