Gas Loop "For" and Call Function

I am trying to optimise something. My script:

function d1() {     
  for (i = 0; i < 1; i++) {    
     d1();     
   }     
   Logger.log("DONE!");   
 }

 function d2() {    
   for (i = 1; i < 2; i++)       
     Logger.log("IN PROGRESS...");     
 }

This script will never complete! But if I change to

function d1() {
  for (i = 0; i < 1; i++) {
    for (i = 1; i < 2; i++)
      Logger.log("IN PROGRESS...");
  }
  Logger.log("DONE!");
}

Everything perfect! What's my problem?

Oh, I'm stupid. of course I call d2().

My script is completely different, I tried to simplify, but I guess I'd better send the original, I hope it will be clear.

projects.gs

 function ViewNewProjects() { 

  var app = UiApp.getActiveApplication();  

  var wait = app.getElementById("wait");  
  wait.setStyleAttribute("display","none");

  var spreadsheet = SpreadsheetApp.openById("0AnYzxNSJL2u_dDVETm9JNVAxQ3BESmdoNGZnc1JDN2c");
  var sheet = spreadsheet.getSheetByName("Заявки Технического Отдела");
  var data = app.getElementById("Data_Panel");
  var range = sheet.getDataRange();
  var values = range.getValues();
  var bg = true;

  data.clear();

  for (i=2; i<values.length; i++) {

    var row = app.createHorizontalPanel();

    var techSpec = app.createHTML(values[i][4]);
    var status = app.createHTML(values[i][11]);

    if (values[i][4].length == 0 && values[i][12] != "Закрыт") {

      var projectLabel = app.createHTML(values[i][0] + ", " + values[i][2]);
      var typeLabel = app.createHTML(values[i][1]);
      var postLabel = app.createHTML(values[i][5]);
      var startDateLabel = app.createHTML(values[i][6]);
      **var expDateLabel = app.createHTML(EstimateData(values[i][7]));**
      var buttonGet = app.createButton("Взять проект");

      var getProject = app.createServerHandler("ViewProject");
      var ShowWait = app.createServerHandler("ShowWait"); 

      projectLabel.setId("project_id_" + i);
      projectLabel.addClickHandler(ShowWait);
      projectLabel.addClickHandler(getProject);

      row.setWidth("100%");

      projectLabel.setStyleAttribute("margin", "3"); 
      projectLabel.setStyleAttribute("padding", "10px"); 

      typeLabel.setStyleAttribute("margin", "3"); 
      typeLabel.setStyleAttribute("padding", "10px"); 

      postLabel.setStyleAttribute("margin", "3"); 
      postLabel.setStyleAttribute("padding", "10px");

      startDateLabel.setStyleAttribute("margin", "3"); 
      startDateLabel.setStyleAttribute("padding", "10px");

      expDateLabel.setStyleAttribute("margin", "3"); 
      expDateLabel.setStyleAttribute("padding", "10px");

      if (bg == true) {

        projectLabel.setStyleAttribute("background-color", "#e9e9e9");
        typeLabel.setStyleAttribute("background-color", "#e9e9e9");
        postLabel.setStyleAttribute("background-color", "#e9e9e9");
        startDateLabel.setStyleAttribute("background-color", "#e9e9e9");
        expDateLabel.setStyleAttribute("background-color", "#e9e9e9");

        bg = false;

      } else {

        projectLabel.setStyleAttribute("background-color", "#f9f9f9");
        typeLabel.setStyleAttribute("background-color", "#f9f9f9");
        postLabel.setStyleAttribute("background-color", "#f9f9f9");
        startDateLabel.setStyleAttribute("background-color", "#f9f9f9");
        expDateLabel.setStyleAttribute("background-color", "#f9f9f9");

        bg = true;

      }

      projectLabel.setStyleAttribute("cursor", "pointer");  
      projectLabel.setStyleAttribute("color", "blue");  
      projectLabel.setStyleAttribute("text-decoration", "underline"); 

      row.add(projectLabel)
         .add(typeLabel)
         .add(postLabel)
         .add(startDateLabel)
         .add(expDateLabel)
         .add(buttonGet);

      row.setCellHorizontalAlignment(projectLabel, UiApp.HorizontalAlignment.LEFT);
      row.setCellVerticalAlignment(projectLabel, UiApp.VerticalAlignment.MIDDLE);
      row.setCellWidth(projectLabel, "320px");

      row.setCellHorizontalAlignment(typeLabel, UiApp.HorizontalAlignment.CENTER);
      row.setCellVerticalAlignment(typeLabel, UiApp.VerticalAlignment.MIDDLE);
      row.setCellWidth(typeLabel, "120px");

      row.setCellHorizontalAlignment(postLabel, UiApp.HorizontalAlignment.CENTER);
      row.setCellVerticalAlignment(postLabel, UiApp.VerticalAlignment.MIDDLE);
      row.setCellWidth(postLabel, "200px");

      row.setCellHorizontalAlignment(startDateLabel, UiApp.HorizontalAlignment.CENTER);
      row.setCellVerticalAlignment(startDateLabel, UiApp.VerticalAlignment.MIDDLE);
      row.setCellWidth(startDateLabel, "80px");

      row.setCellHorizontalAlignment(expDateLabel, UiApp.HorizontalAlignment.CENTER);
      row.setCellVerticalAlignment(expDateLabel, UiApp.VerticalAlignment.MIDDLE);
      row.setCellWidth(expDateLabel, "80px");

      row.setCellHorizontalAlignment(buttonGet, UiApp.HorizontalAlignment.CENTER);
      row.setCellVerticalAlignment(buttonGet, UiApp.VerticalAlignment.MIDDLE);

      data.add(row);

    }

  }

  return app;

 };

holidays.gs

function EstimateData(date_to_estimate) {

  var date = new Date();
  var count = 0;

  var mounth = new String(date.getMonth() + 1);
  var day = new String(date.getDate());
  var year = new String(date.getFullYear());

  if (mounth < 10)

    mounth = new String("0" + mounth);

  var current_date = year + mounth + day;

  var ___date_to_estimate = new String(date_to_estimate);
  var date_to_estimate_array = ___date_to_estimate.match(/(\d+)\.(\d+)\.(\d+)/); 
  var ___date_to_estimate = date_to_estimate_array[3] + date_to_estimate_array[2] + date_to_estimate_array[1];

  current_date = Number(current_date);
  ___date_to_estimate = Number(___date_to_estimate);

  if (current_date > ___date_to_estimate) {

    Logger.log("Текущая дата больше рассчитываемой");

    var difference = Number(current_date - ___date_to_estimate);

    Logger.log("Разница " + difference);

    if (difference == 0) {

      return date_to_estimate;

    } else if (difference < 31) {

      var weekday = date.getDay();

      Logger.log("День недели " + weekday);

      for (i = 1; i < difference; i++) {

        if (weekday == 7)

          weekday = 0;

        if (weekday > 0 && weekday < 6) {

          count++;

        }

        weekday++;

      }

    } else if (difference < 1130) {

    } else {

    }


  } else if (current_date == ___date_to_estimate) {

    date_to_estimate = '<b>Сегодня!!!</b>';

  } else {

  }

  Logger.log("Количество рабочих дней " + count);

  return date_to_estimate;

}

while writing the message and testing, I found my mistake.

loop for in projects.gs contain variable i loop for in holadays.gs contain varialble i too

I changed the variable and it's all right!

1

1 Answer

You have infinite recursion because you are calling d1() inside of d1() for loop.

Your first function should be

function d1() {     
  for (i = 0; i < 1; i++) {    
     d2();     
   }     
   Logger.log("DONE!");   
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest