0
function checkWho(n,b)
{
 // n and be are comparing two different cells to check if the name is in the registry
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var glr = sheet.getLastColumn();
 var glr2 = sheet.getLastRow();
for(var i = 9; i <= glr; i++)
 {
   for(var z = 10; z<= glr2; z++)
    {
      if( n == b)
      {
       var courts = sheet.getRange(3,i).getValue();
       var times = sheet.getRange(z,10).getValue();
       return(b+ " "+"has booked"+" "+ courts+" "+"at"+times);
}
}
}
}

I am having issues printing out the values contained in var courts and var times. My code consists of two for loops iterating through columns and rows and eventually spitting out the users name, what court they've booked and at what time. As of now the name gets printed, but the courts and the times don't.

it currently prints: "(name) has booked at"
When I want it to print:" (name) has booked court 1 at 4:30"

Any help on the situation?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Amazerhf
  • 15
  • 2
  • 6

2 Answers2

0

What is happening is that the the nested for statements are overwriting the result. It's very likely that the court and time are "" (empty strings) because the iteration is done from a start column/row and repeated for the next columns/rows. It's very common that the last column/rows are empty.

Side notes:

  1. The script include a comment mentioning that custom function arguments are cells but custom function can't use cells as argument in the same sense of getCurrentCell(). Custom functions arguments types could be String, Number, Date or Array.

  2. It doesn't make sense to compare the arguments inside the nested for statements as they doesn't change on each iteration.

  3. Including a return inside the nested for statement will stop the iterations. As the arguments are not iteration dependent, only the first iteration is made for the case considered in the question.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    Thank you for this advice! I looked into it and it looks like you're correct. I changed it so that it the ranges were between C9:G9 and B10:B35. This return a value(even though it wasn't what I was looking for). Thanks for the help! – Amazerhf Jul 13 '18 at 20:04
  • What would you recommend to fix the issue of empty strings? – Amazerhf Jul 13 '18 at 21:51
  • @Amazerhf : A common workaround is to remove unused rows and columns. In this specific case it's not clear what could be the minimal change to the question script as there isn't enough details, like the reason to use nested for statements. Consider to create a demo spreadsheet having the minimum to reproduce the problem and share it with anyone with the link to view. – Rubén Jul 13 '18 at 21:59
0

If you return a string and your matter is to print those variables, then replace your return statement like this.(same as java script ES6 )

return(`${b} has booked ${courts} at ${times}`);

App script is grooming scripting language. My suggestion is working properly now.

Pasindu Perera
  • 489
  • 3
  • 8