2012-07-03 90 views
1

我想使用Google Apps腳本動態更改Google Spreadsheet單元格驗證功能中候選列表的值。如何在Google應用程序腳本中創建動態下拉列表

我無法使用Google公式功能在Google電子表格中創建動態下拉列表。在Excel中,可以在Excel Spreadsheet中使用在下拉列表功能工作的單元格中設置的INDIRECT函數,引用單元格的值爲動態設置的列表範圍名稱。

回答

3

不幸的是,使用Apps腳本來管理單元驗證是不可能的。有關於此的增強request opened。你應該用它來記錄更新和投票的種類。

另外,Excel INDIRECT功能不適用於Google Spreadsheets。

1

Times have changed see this blog post

My spreadsheet example that could use some improvements

博客帖子一個簡單的方法來做到這一點。我的代碼啓動器是

function loadMenu() { 
    var ga = SpreadsheetApp.getActive(); 

    var firstMenu = 'B3:B4'; 
    var firstValues = 'E3:F3'; 
    var staticValue1 = 'E3'; 
    var staticValue2 = 'F3'; 

    var secondMenu = 'C'; 
    var secondMenuRow = 3; 
    var secondMenu1 = 'E4:E'; 
    var secondMenu2 = 'F4:F'; 

    dynamicDrop(ga, firstMenu, firstValues, staticValue1, staticValue2, secondMenu, secondMenuRow, secondMenu1, secondMenu2); 

} 


function dynamicDrop(ga, fm, fv, sv1, sv2, sm, smr, sm1, sm2) { 

    //first drop choices 
    var cells = ga.getRange(fm); 
    var range = ga.getRange(fv); 
    var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build(); 
    cells.setDataValidation(rule); 

    // set as static value 
    var staticVar1 = ga.getRange(sv1).getValue(); 

    // set as static value 
    var staticVar2 = ga.getRange(sv2).getValue(); 

    // set to chosen value of fm 
    var chosenVar1 = ga.getRange(fm).getValues(); 

    for (var row in chosenVar1) { 

    for (var col in chosenVar1[row]) { 

     if (chosenVar1[row][col] == staticVar1) { 

     num = parseInt(row) + smr; 
     var cells = ga.getRange(sm+num); 
     var range = ga.getRange(sm1); 
     var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build(); 
     cells.setDataValidation(rule); 

     } else if (chosenVar1[row][col] == staticVar2) { 

     num = parseInt(row) + smr; 
     var cells = ga.getRange(sm+num); 
     //range is Static values 
     var range = ga.getRange(sm2); 
     var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build(); 
     cells.setDataValidation(rule); 

     } 
    } 
    } 
} 
相關問題