2013-02-27 137 views
-2

我想优化一些东西。我的脚本:GAS循环“for”和调用函数

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

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

这个脚本永远不会完成!但如果我改变为

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

一切都很完美!我的问题是什么?

哦,我很愚蠢。当然我叫d2()。

我的脚本完全不同,我试图简化,但我想我最好发送原件,我希望它会很清楚。

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; 

} 

一边写邮件和测试,我发现我错了。

循环为projects.gs包含变量i 循环为holadays.gs包含varialble我太

我改变了变量,它的所有权利!

+0

我忘记了。函数b1由页面上的按钮调用。 – user2114934 2013-02-27 10:46:38

回答

1

由于您在d1() for循环内调用d1(),因此您有无限递归。

你的第一个功能应该是

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