下拉依赖选择列表

时间:2016-03-08 04:37:51

标签: javascript google-sheets

我有三个依赖的下拉列表工作,第四个失败,函数范围错误限制单元格列表;以下示例。从选择列表前三个工作和更改,我的第四个,最后一个选择不会将数据限制为先前的选择。

  function restrictCellToList(cellString, list) {
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange(cellString);
var rule = SpreadsheetApp.newDataValidation().requireValueInList(list,       true).build();
cell.setDataValidation(rule);
cell.setValue(list[0]);

}

  function initializeTrims() {
var selectedCell = SpreadsheetApp.getActiveRange();
var selectedMake = selectedCell.getValue();
var selectedModel = selectedCell.getValue();
var selectedYear = selectedCell.getValue();
var selectedTrim = selectedCell.getValue();

var cars_raw =    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");
Logger.log("You have selected %s", selectedYear);

var trims = [];  
var carData = getCarData();
for (var i = 0; i < carData.length; i++) {
  var row = carData[i];
  var make = row[0];
  var model = row[1];
  var year = row[2];
  var trim = row[3];
  if (year == "") {
    continue;
   }
  if (year == selectedYear && trims.indexOf(trim) == -1) {
    trims.push(trim);
  }
}

 restrictCellToList("D2", trims);

 Logger.log("Trims: %s", trims);

}

1 个答案:

答案 0 :(得分:0)

 function initializeTrims() {
var selectedCell = SpreadsheetApp.getActiveRange();
var selectedMake = selectedCell.getValue();
var selectedModel = selectedCell.getValue();
var selectedYear = selectedCell.getValue();
var selectedTrim = selectedCell.getValue();
// this line was failing because the variables where looping           indefinitely I fixed it by changing to get range 

var selectedCell = SpreadsheetApp.getActiveRange();
  var selectedMake = ss.getRange(2, 1).getValue();
  var selectedModel = ss.getRange(2, 2).getValue();
  var selectedYear = ss.getRange(2, 3).getValue();
  var cars_raw =       SpreadsheetApp.getActiveSpreadsheet().getSheetByName("cars_raw");