简体   繁体   English

Google 表格中的多个相关下拉列表

[英]Multiple Dependent Drop Down lists in Google Sheets

I've found this code snippet which reads data from certain columns and then automatically creates dependent drop down lists from that data.我发现这个代码片段从某些列中读取数据,然后自动从该数据中创建相关的下拉列表。

Here in my example It creates dependent drop down lists on SheetB and it's working for that purpose.在我的示例中,它在 SheetB 上创建了相关的下拉列表,并且正在为此目的工作。

//CREATE PRIMARY DROPDOWN LIST
function createPrimaryDrpdwon() {
/* SET FOLLOWING VARIABLES */

var dataSS           = "Options";         //Name of the sheet that contain data for dropdown lists
var dropSS           = "SheetB";      //Name of the sheet which dropdown list to be created
var primaryDataRange = "N7:N500";       //Data range for primary dropdown
var primaryDropRange = "C2:C500";       //Range which primary dropdown set

var primaryDropList  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(dataSS).getRange(primaryDataRange).getValues();
var primaryDropRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(dropSS).getRange(primaryDropRange);
var validationRule   = SpreadsheetApp.newDataValidation().requireValueInList(primaryDropList).build();

primaryDropRange.setDataValidation(validationRule);
}


//CREATE SECONDARY DROPDOWN LIST
function onEdit(){
/* SET FOLLOWING VARIABLES */
var dataSS       = "Options";         //Name of the sheet that contain data for dropdown lists
var dropSS       = "SheetB";      //Name of the sheet which dropdown list to be created
var allDataRange = "N7:O500";       //Data range for dropdown list (both primary and dependent)
var primaryDDCol = 3;               //Column number of the primary drop down

var dropSS_      = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(dropSS);
var dropDData    = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(dataSS).getRange(allDataRange).getValues();
var activeCell   = dropSS_.getActiveCell();
var activeColumn = activeCell.getColumn();
var activeRow    = activeCell.getRow();

if(activeColumn==primaryDDCol){
    var dep_Col         = primaryDDCol+1;
    var dep_Row         = activeRow;
    var depCell         = dropSS_.getRange(dep_Row, dep_Col);
    var primarySelected = activeCell.getValue();
    var validationRule  = SpreadsheetApp.newDataValidation().requireValueInList(getDependentList(dropDData,primarySelected)).build();
    
    depCell.setDataValidation(validationRule);
}
}

function getDependentList(dropDData,primarySelected){
var dependenList = [];
var j = 0;
if(dropDData != null){
    for(i=0; i<dropDData.length; i++){
    if(dropDData[i][0]==primarySelected){
        dependenList[j] = dropDData[i][1];
        j++;
    }
    }
}
return dependenList;
}

But what if I want to have another drop down list that reads data from different columns on Options Sheet and creates dependent drop down lists on SheetC for example, and if I want to have more of those in the future?但是,如果我想要另一个下拉列表来读取选项表上不同列的数据并在 SheetC 上创建相关下拉列表,并且如果我想在未来拥有更多这样的下拉列表,该怎么办?

I tried copy/pasteing and renaming all vars and changing input and output data in first copy, but I couldn't make it work.我尝试复制/粘贴和重命名所有变量并在第一个副本中更改输入和输出数据,但我无法使其工作。

EDIT: To clarify with the image example of what I'm trying to achieve:编辑:用我想要实现的图像示例来澄清:

Sheet Options - data range for the first dependent drop lists which will show on SheetB工作表选项 - 将显示在工作表 B 上的第一个相关下拉列表的数据范围

工作表选项 1

SheetB - First dependent drop down lists generated by script SheetB - 脚本生成的第一个依赖下拉列表

表 B

Short Gif of how script currently works on SheetB脚本当前如何在 SheetB 上运行的简短 Gif

表 B - 视频

Sheet Options - data range for the second dependent drop lists which will show on SheetC工作表选项 - 将在 SheetC 上显示的第二个相关下拉列表的数据范围

工作表选项 2

SheetC - Second dependent drop down lists generated by script SheetC - 脚本生成的第二个相关下拉列表

表 C

Script currently generates drop down lists on SheetB, but I need the same thing on SheetC, possibly SheetD, E etc. :)脚本目前在 SheetB 上生成下拉列表,但我在 SheetC 上需要同样的东西,可能是 SheetD、E 等:)

I believe your goal is as follows.我相信你的目标如下。

  • You want to put the dropdown list to the column "C" in "SheetB" and "SheetC" when a function of createPrimaryDrpdwon() is run.当运行createPrimaryDrpdwon()函数时,您希望将下拉列表放入“SheetB”和“SheetC”中的“C”列。
  • When the dropdown list of column "C" is changed, you want to set the dropdown list to the column "D" by the value of column "C".当更改“C”列的下拉列表时,您希望通过“C”列的值将下拉列表设置为“D”列。

In this case, how about the following modification?在这种情况下,如何进行以下修改?

Sample script:示例脚本:

function createPrimaryDrpdwon() {
  var dataSS = "Options";

  // Please set the sheet names and ranges.
  var dropSSs = [
    { dropSS: "SheetB", primaryDataRange: "N7:N500" },
    { dropSS: "SheetC", primaryDataRange: "Q7:Q500" },
  ];

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  dropSSs.forEach(({ dropSS, primaryDataRange }) => {
    var primaryDropRange = "C2:C20";
    var primaryDropList = ss.getSheetByName(dataSS).getRange(primaryDataRange).getValues();
    var primaryDropRange = ss.getSheetByName(dropSS).getRange(primaryDropRange);
    var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(primaryDropList).build();
    primaryDropRange.setDataValidation(validationRule);
  });
}

function onEdit(e) {
  // Please set the sheet names and ranges.
  var dropSSs = { "SheetB": "N7:O500", "SheetC": "Q7:R500" };

  const ss = e.source;
  const range = e.range;
  const sheet = range.getSheet();
  const sheetName = sheet.getSheetName();
  if (!Object.keys(dropSSs).includes(sheetName) || range.columnStart != 3 || range.rowStart == 1) return;

  // I modified your onEdit function a little as follows.
  var dataSS = "Options";
  var primaryDDCol = 3;
  var dropSS_ = sheet;
  var dropDData = ss.getSheetByName(dataSS).getRange(dropSSs[sheetName]).getValues();
  var activeCell = range;
  var activeColumn = activeCell.getColumn();
  var activeRow = activeCell.getRow();
  if (activeColumn == primaryDDCol) {
    var dep_Col = primaryDDCol + 1;
    var dep_Row = activeRow;
    var depCell = dropSS_.getRange(dep_Row, dep_Col);
    var primarySelected = activeCell.getValue();
    var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(getDependentList(dropDData, primarySelected)).build();
    depCell.setDataValidation(validationRule);
  }
}

function getDependentList(dropDData, primarySelected) {
  var dependenList = [];
  var j = 0;
  if (dropDData != null) {
    for (i = 0; i < dropDData.length; i++) {
      if (dropDData[i][0] == primarySelected) {
        dependenList[j] = dropDData[i][1];
        j++;
      }
    }
  }
  return dependenList;
}
  • When this script is run, the dropdown list is created to the column "C" of the sheets of dropSSs by retrieving the values from "Options" sheet.运行此脚本时,通过从“选项”表中检索值,将下拉列表创建到dropSSs表的“C”列。 And, when the dropdown list of column "C" is changed, the dropdown list is inserted to the column "D" by the value of column "C".并且,当更改“C”列的下拉列表时,下拉列表将通过“C”列的值插入到“D”列。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM