2013-02-27 74 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!"); 
}