If else error

77 views
Skip to first unread message

Silvio Cesar Dos Santos Junior

unread,
Apr 23, 2021, 7:32:50 PM4/23/21
to Google Apps Script Community
Hi guys, first of all sorry for my english i'am from Brazil.

I already have a script to schedule events in Google Calendar, someone answers a Google Forms, that response goes to a Spreadsheet and i'am using that Spreadsheet like a database, the schedule script insert the event ID he created in that sheet.

So now i'am creating a new script to cancel a event if is needed but i'm struggling to make the if/else statement. The script will look into another sheet who receives answers from another Google Forms get some cells and one of those cell is the client email, the script will check the client email in the schedule/database and if matchs he will get de event ID and use that ID to delete the event else he will send a email warning he couldn't found a match.

The error is he goes to the else condition even if has a match, for some reason the script in not checking all the lines in the for loop, if the first line he checks doesn't match with the client email he sends the email warning the error, he is not checking all the lines if the first line he checks is not whats he is looking for and i can't found what i'am doing wrong, pls someone save me.

My for loop is to check all the database cells starting from the database lastrow and stoping at the line 2.

Everything that has age_ is from the spreadsheet/database, both scripts are in the same Project.

My Delete Event script is (i have a lot of Looger.log because i was using then to find the error):

function canc_responses(canc_page, canc_lastRow) {

var canc_timeStamp = canc_page.getRange(canc_lastRow, 1).getValue();
var canc_type = canc_page.getRange(canc_lastRow, 2).getValue();
var canc_name = canc_page.getRange(canc_lastRow, 3).getValue();
var canc_email = canc_page.getRange(canc_lastRow, 4).getValue();
var canc_emailStamp = canc_page.getRange(canc_lastRow, 5).getValue();

Logger.log(canc_timeStamp);
Logger.log(canc_type);
Logger.log(canc_name);
Logger.log(canc_email);
Logger.log(canc_emailStamp);

canc_checkID(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);

}

function canc_checkID(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp) {


for (var r = age_lastRow; r > 1; r--) {

var canc_dataRangeEmail = age_page.getRange(r, 4);
var canc_dataRangedate = age_page.getRange(r, 8);

var canc_emailCheck = canc_dataRangeEmail.getValue();
var canc_date = canc_dataRangedate.getValue();

Logger.log(r);

Logger.log(canc_emailCheck);
Logger.log(canc_date);

Logger.log(canc_emailCheck);
Logger.log(canc_email);

if (canc_emailCheck == canc_email) {

var canc_eventId = age_page.getRange(r, 11).getValue();

Logger.log(canc_eventId);

Logger.log("Keep Going");

//canc_deleteEvent(canc_page, canc_eventId, canc_date, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);

break;

} else {

Logger.log("Error");

//canc_erros(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);

break;

}

}


}

cbmserv...@gmail.com

unread,
Apr 23, 2021, 8:15:46 PM4/23/21
to google-apps-sc...@googlegroups.com

Hi Silvio,

 

Do you have other code somewhere else that you are not showing us? There are some variables that you are using in your functions that are not initialized to anything. Are these global variables you defined elsewhere?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f52bdd8a-5f83-4237-9eef-555044968985n%40googlegroups.com.

Šáhàr

unread,
Apr 24, 2021, 12:13:40 AM4/24/21
to google-apps-sc...@googlegroups.com
Send me the web as link 

‫أُرسلت من الـ iPhone‬

‫في 24‏/04‏/2021 الساعة 3:15 ص، كتب/كتبت cbmserv...@gmail.com:‬



Silvio Cesar Dos Santos Junior

unread,
Apr 26, 2021, 9:52:08 AM4/26/21
to Google Apps Script Community
Sorry for the late

Hi George, that project has a main file who calls all the other files and it declares some global variables that other files may need to use.

Hi Sahar, i didn't understand your request.

CBMServices Web

unread,
Apr 26, 2021, 9:42:43 PM4/26/21
to google-apps-sc...@googlegroups.com
If you are using global variables from another source, then this is basically a library to you. So when the global variable is used, you need to reference it as follows:

LibraryName.varName

Just using the variable name by itself would not work.


Silvio Cesar Dos Santos Junior

unread,
Apr 29, 2021, 12:00:27 PM4/29/21
to Google Apps Script Community
I don't think the Global Variables are the issue, with de Logger.Log i can see the correct value of those variables is reaching de If Else and when i remove de else condition the script works just fine but when i insert the else condition the error appears again and i want to have the else because i'am using it to create custom error messages to the user.

Clark Lind

unread,
May 3, 2021, 7:14:10 AM5/3/21
to Google Apps Script Community
You may be running into a problem I had one time. When checking email addresses, make sure they are formatted exactly the same:

canc_emailCheck  = canc_emailCheck.toLowerCase().trim();
canc_email =  canc_email.toLowerCase().trim();
then
if (canc_emailCheck == canc_email) {

or simply:

if ( canc_emailCheck.toLowerCase().trim()   ==  canc_email.toLowerCase().trim()  ) {

Clark Lind

unread,
May 3, 2021, 7:29:04 AM5/3/21
to Google Apps Script Community
Also consider using a switch instead of an If. That way you can keep the comparison from kicking you out of the For loop. Otherwise, I believe you use "continue" instead of "break".

switch(canc_emailCheck) {

  case canc_emailCheck == canc_email:

    var canc_eventId = age_page.getRange(r, 11).getValue();
    Logger.log(canc_eventId);
    Logger.log("Keep Going");
    //canc_deleteEvent(canc_page, canc_eventId, canc_date, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);
    break;

  default:
    Logger.log("Error");
    //canc_erros(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);
}

Silvio Cesar Dos Santos Junior

unread,
May 3, 2021, 12:39:27 PM5/3/21
to google-apps-sc...@googlegroups.com
Thanks for the help Clark.

Now the script is no more stopping at the first wrong match, the for loop checks every line but he still can't find the right match. You have any idea why this keep happening?

My canc_checkID function is now that:

function canc_checkID(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp) {


for (var r = age_lastRow; r > 1; r--) {

var canc_dataRangeEmail = age_page.getRange(r, 4);
var canc_dataRangedate = age_page.getRange(r, 8);

var canc_emailCheck = canc_dataRangeEmail.getValue();
var canc_date = canc_dataRangedate.getValue();

Logger.log(r);
Logger.log(canc_emailCheck);
Logger.log(canc_email);

var canc_emailCheck = canc_emailCheck.toLowerCase().trim();
var canc_email = canc_email.toLowerCase().trim();

switch (canc_emailCheck) {

case canc_emailCheck == canc_email:

var canc_eventId = age_page.getRange(r, 11).getValue();

Logger.log(canc_eventId);
Logger.log("Keep Going");
//canc_deleteEvent(canc_page, canc_eventId, canc_date, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);
break;

default:
Logger.log("Error");
//canc_erros(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);
break;

}

}

}

In the execution Log i can see with the logger.log the canc_emailCheck and canc_email matches but the script doesn't see it.
13:37:52
Informação
21.0
13:37:52
Informação
13:37:52
Informação
13:37:52
Informação
Error

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/p42UPgUur1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f15ec86b-0f25-4ade-9c77-d3e8a6f756dcn%40googlegroups.com.


--
Atenciosamente.

Clark Lind

unread,
May 4, 2021, 2:54:56 PM5/4/21
to google-apps-sc...@googlegroups.com
Ok, try changing the switch condition to:

switch (true)  { 

I may have gotten it wrong before.. lol

Silvio Cesar Dos Santos Junior

unread,
May 4, 2021, 3:51:13 PM5/4/21
to google-apps-sc...@googlegroups.com
Thanks Clark, with that modification he found the match and canceled the event but he didn't stop at the first match, how can i do that ?



--
Atenciosamente.

Clark Lind

unread,
May 4, 2021, 6:20:59 PM5/4/21
to google-apps-sc...@googlegroups.com
I've looked at the code and rethought it better. I think this will work smoother and faster since it only makes 1 or 2 calls to the sheet instead of a LOT lol 

This works from top down and only on the first one it finds, so if you MUST do it from bottom up, let me know and I'll rewrite this:

function canc_checkID(canc_pagecanc_lastRowcanc_typecanc_namecanc_emailcanc_emailStamp) {
  const flatten = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a)); //this is a utility function I use to flatten arrays
  var tempArr = age_page.getRange(24ws.getLastRow(), 1).getValues(); //get column D values from row2 to last row
  var canc_dataRangeEmail = flatten(tempArr); //convert array of arrays to single array ('flatten')

  canc_dataRangeEmail.map(item => item.toLowerCase().trim()); //go through array and turn to lowercase and trim

  var row = canc_dataRangeEmail.indexOf(canc_email.toLowerCase().trim()); //check to see if the lowercase/trimmed email is inside the array, and at which location.

  if (row === -1) { //if email is not inside the array, index will be -1
    // canc_erros(canc_page, canc_lastRow, canc_type, canc_name, canc_email, canc_emailStamp);
  } else {
    row += 2//need to add 2 to the index since we skipped first row, and array starts at zero. This will give us the correct row
    var canc_eventId = age_page.getRange(row11).getValue();
var canc_date = age_page.getRange(row8).getValue();
Reply all
Reply to author
Forward
0 new messages