-1

I am writing a script, and I get an error (red curly brackets)

Making the slight change in the code which as far as I know, shouldn't result in an error. I am checking it over and over for hours and I can't figure it out. the error appears on the last brackets (The one that ends the function)

And sometimes with the brackets before that too.

Here's the code:

function detectTraining() {
 
  logs.getRange(1,2,20).clear();
// Loop through Trainees
  for (a = 0; a < TraineesResultsData.length; a++){
    Logger.log("Trainee num " + (a+1))
    /////////////////////////////////
    // Results Declatations
    let mailR = TraineesResultsData[a][0]
    let NameR = TraineesResultsData[a][1]
    let Mass_FitR = TraineesResultsData[a][2]
    let TrainingPlanR = TraineesResultsData[a][3]
    let ExperienceR = TraineesResultsData[a][5]
    let GymR = TraineesResultsData[a][6]
    let BodyWeightOnlyR = TraineesResultsData[a][7]
    let PullUpR = TraineesResultsData[a][8]
    let MakbilimR = TraineesResultsData[a][9]
    let StrapsR = TraineesResultsData[a][10]
    let RubberBandsR = TraineesResultsData[a][11]
    let WeightsR = TraineesResultsData[a][12]
    let ParkR = TraineesResultsData[a][13]
    let TimesAweekR = String(TraineesResultsData[a][14])
    let PhysicalProblem = TraineesResultsData[a][15]

    
    // If trainee don't have training
    if (TrainingPlanR) {
      logs.getRange(a+1,2).setValue("Trainee Do have a training");
    }
    else {
      // if trainee have a physical problem
      if(PhysicalProblem) {
        logs.getRange(a+1,2).setValue("Physical Problem - Loop Stopped ");
      } 
      else {
        // if trainee is a begineer
        if (ExperienceR){
          logs.getRange(a+1,2).setValue("Trainee is a beginner ");
          // if trains in a gym
          if(GymR) {           
            logs.getRange(a+1,3).setValue("Trains in Gym " + TimesAweekR + " a week");
            
            // search results in trainingKeys
            for(t = 0; t < TrainingKeysData.length; t++) {
              /////////////////////////////////
              // Keys Declatations
              let experienceK = TrainingKeysData[t][0];
              let Mass_FitK = TrainingKeysData[t][1];
              let BodyWeightOnlyK = TrainingKeysData[t][2];
              let PullUpsK = TrainingKeysData[t][3];
              let MakbilimK = TrainingKeysData[t][4];
              let StrapsK = TrainingKeysData[t][5];
              let RubberBandsK = TrainingKeysData[t][6];
              let WeightsK = TrainingKeysData[t][7];
              let ParkK = TrainingKeysData[t][8];
              let GymK = TrainingKeysData[t][9];
              let TimesAweekK = String(TrainingKeysData[t][10]);
              let TrainingResultsCt = TrainingKeysData[t][16];
              
              if(experienceK && GymK && TimesAweekK.includes(TimesAweekR)) {
                // If there's more than 1 option of training
                Logger.log("Found row");
                if(TrainingResultsCt > 1){
                  Logger.log("More than 1 results")
                  let Rand = Math.floor(Math.random()*TrainingResultsCt);
                  Logger.log("Rand: "+Rand)
                  let resultsArea = TrainingKeys.getRange(t+2, 13, 1,TrainingResultsCt).getValues();
                  Logger.log(resultsArea)
                  let TrainingNum = resultsArea[0][Rand]
                  logs.getRange(a+1,4).setValue(TrainingNum)

                }
                // if there's only 1 training option.
                else {
                  Logger.log("1 result")
                  TrainingNum = TrainingKeysData[t][12];
                  logs.getRange(a+1,4).setValue(TrainingNum)
                }
                ////////////////////////
                //Done Beginner in Gym//
                ////////////////////////
              }
            }
          }
          // if not in Gym
          else if (!GymR) {
            logs.getRange(a+1,3).setValue("Do not Trains in Gym " + TimesAweekR + " a week");
            if (TraineesResultsData[a][12]) {

              if (PullUpsK || MakbilimK || StrapsK || RubberBandsK) {
                Logger.log("We can combinate Weights");
              } 
              else {Logger.log("He has only weights")} // The line in question
            }
          }
        }
      }
    }
  }
}

And here's the one with the error: screenshot

The only change is in the end when I added another row for the "else" curly brackets.

There was other if's and for's I tried to do, but every time I am adding anything to the code, it results in an error.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    Related [Last brace → } are turning red instead of green, but the script works normally](https://stackoverflow.com/q/59017564/1595451) – Rubén Sep 24 '20 at 13:43
  • 1
    Use descriptive titles. See [ask] – TheMaster Sep 24 '20 at 15:17
  • Off-note: please avoid the [pyramid of doom](https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming)) when writing code with a significant amount of control statements – Oleg Valter is with Ukraine Sep 24 '20 at 16:05
  • 1
    @OlegValter Given that we don't have support for null.coalescing `?.`, what do you suggest? – TheMaster Sep 24 '20 at 16:16
  • 2
    @TheMaster - what is usually suggested by best practices: avoiding `if...else` is one of the techniques ( for example, if there was a return in the first `if`, we could save up one level of nesting by removing first `else` ). Another one is wrapping in HoFs ( for example, all logic in loops can be extracted into reusable functions that accept data and indices as parameters ). And another one is using default values ( like `someArray || []` ) – Oleg Valter is with Ukraine Sep 24 '20 at 16:30
  • 2
    Related (older than the first one): [Why does google script editor stop paren matching after 100 lines in a function and does it affect the code?](https://stackoverflow.com/q/13711260/1595451) – Rubén Sep 24 '20 at 16:47

1 Answers1

2

It's a bug

It has already been reported on Google's Public Issue Tracker - see here and here.

It seems to occur when the code is longer than 100 lines (hence the line break makes a difference!).

It seems like it is already being worked on. Also, keep in mind that the new Apps Script IDE is coming out soon, the bug should not appear there.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33