Spreadsheet,带有条件下拉菜单,基于另一个 sheet 的选项

Spreadsheet with conditional dropdown based on options from another sheet

我有一个目的地 sheet,在 Col "B" 中有一个 "Companies" 的列表,在 Col "K" 中,我想通过下拉菜单选择"Address" 可用(那个特定的 "Company")取自另一个 sheet,位于 B 和 J 栏。

如何使用脚本执行此操作?

https://docs.google.com/spreadsheets/d/15g_3TMmVufKZogCbO3SUWBUp3iwc21nQgPBw6_GWXQQ/edit

我相信你的目标如下。

  • 您想将数据验证规则放入 sheet destination 中的 "K2:K" 列。
  • 您想通过从 sheet source.
  • 中的 "B" 和 "J" 列检索值来创建规则

为此,这个答案怎么样?

流量:

  1. 从两个 sheet 中检索值。
  2. 创建用于创建规则的对象。
  3. 创建规则。
  4. 将规则放入 sheet destination 中的 "K" 列。

示例脚本:

function myFunction() {
  // 1. Retrieve values from the both sheets.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const src = ss.getSheetByName("source");
  const dst = ss.getSheetByName("destination");
  const srcValues = src.getRange("A2:J" + src.getLastRow()).getValues();
  const dstValues = dst.getRange("B2:B" + dst.getLastRow()).getValues();

  // 2. Create an object for creating the rules.
  const obj = srcValues.reduce((o, [a,b,,,,,,,,j]) => Object.assign(o, {[a]: [b, j]}), {});

  // 3. Create the rules.
  const rules = dstValues.map(([e]) => ([SpreadsheetApp.newDataValidation().requireValueInList(obj[e]).build()]));

  // 4. Put the rules to the column "K" in the sheet `destination`.
  dst.getRange("K2:K" + dst.getLastRow()).setDataValidations(rules);
}

参考文献:

已添加 1 个:

对于你在回复中的另外3个问题,我回答如下。

  • Q1:如果将来我想从 sheet 源中的更多列中检索值。如何编辑脚本?
    • A1:这种情况下,请修改[a,b,,,,,,,,j]。现在,abj分别是CompanyAddress options 1Address options 2
  • Q2:要用脚本更新sheets,你建议我激活一个触发器?
    • A2。我不明白你想做什么。
  • 问题 3:如果我在 col "B"(目标)中放入一个值,而 col "A"(源)中不存在该值,则脚本无法运行。我需要这个。

    • A3:请修改上面的脚本如下。
    • 来自

      - const rules = dstValues.map(([e]) => ([SpreadsheetApp.newDataValidation().requireValueInList(obj[e]).build()]));
      
    • const rules = dstValues.map(([e]) => ([obj[e] ? SpreadsheetApp.newDataValidation().requireValueInList(obj[e]).build() : null]));
      

添加了 2 个:

The question 2, I explain. Naturally when I run the script the dropdown are populated, but if I add a new company, I must to run again the script and so on... to avoid this, how can I do?

对于上面你附加的第二个问题,示例脚本如下。在这种情况下,编辑destination的sheet的列"B"时,脚本为运行。在这种情况下,可以使用简单的触发器。

示例脚本:

function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  if (range.getColumn() != 2 || sheet.getSheetName() != "destination") return;

  // 1. Retrieve values from the both sheets.
  const ss = e.source;
  const src = ss.getSheetByName("source");
  const dst = ss.getSheetByName("destination");
  const srcValues = src.getRange("A2:J" + src.getLastRow()).getValues();
  const dstValues = dst.getRange("B2:B" + dst.getLastRow()).getValues();

  // 2. Create an object for creating the rules.
  const obj = srcValues.reduce((o, [a,b,,,,,,,,j]) => Object.assign(o, {[a]: [b, j]}), {});

  // 3. Create the rules.
  const rules = dstValues.map(([e]) => ([obj[e] ? SpreadsheetApp.newDataValidation().requireValueInList(obj[e]).build() : null]));

  // 4. Put the rules to the column "K" in the sheet `destination`.
  dst.getRange("K2:K" + dst.getLastRow()).setDataValidations(rules);
}