2014-03-25 129 views
0

我必須檢查天氣某個屬性是否存在,然後才能運行Google電子表格附加菜單中列出的功能。我不想爲每個函數創建相同檢查的副本,而是希望創建一個可以將該函數作爲參數運行的單個函數。我怎樣才能做到這一點?我怎樣才能傳遞一個函數來運行一個函數?

下面是我的非功能測試代碼,但您可能會明白。

function testRun(){ 
    //in practicality this would be an add-on menu 
    test1Check('test1()'); 
} 


function test1(){ 
    Logger.log("Function Ran"); 
} 


function test1Check(functionToRun){ 
     var labels = PropertiesService.getDocumentProperties().getProperty('labels'); 
    var ui = SpreadsheetApp.getUi(); // Same variations. 
    if (!labels) { 
    var result = ui.alert(
     'You have not yet set up the page for gClassFolders', 
     'Would you like to do this now?', 
     ui.ButtonSet.YES_NO); 
    // Process the user's response. 
    if (result == ui.Button.YES) { 
    // User clicked "Yes". 
    setupGCF(); 
    } 

    } 
    else { 
    functionToRun; 
    } 
} 

回答

0

我不得不從發送的參數中刪除(),並將add()添加到函數中的變量中。

function testRun(){ 

    test1Check(test1); 
} 


function test1(){ 
    Logger.log("Function Ran"); 
} 


function test1Check(functionToRun){ 
     var labels = PropertiesService.getDocumentProperties().getProperty('labels'); 
    var ui = SpreadsheetApp.getUi(); // Same variations. 
    if (!labels) { 
    var result = ui.alert(
     'You have not yet set up the page for gClassFolders', 
     'Would you like to do this now?', 
     ui.ButtonSet.YES_NO); 
    // Process the user's response. 
    if (result == ui.Button.YES) { 
    // User clicked "Yes". 
    setupGCF(); 
    } 

    } 
    else { 
    functionToRun(); 
    } 
} 
相關問題