最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google sheets api - How do I make a drop down of variations for a product? - Stack Overflow

programmeradmin5浏览0评论

When I type "product" in a cell. The cell next to it would show a list of available variations via a drop-down menu?

Something like this? This is the data sheet

DATA SHEET:

Column A Column B Column C
Cell 1 Products Variation
Cell 2 Robot Blue
Cell 3 Soldier Yellow
Cell 4 Robot Green

When I type "product" in a cell. The cell next to it would show a list of available variations via a drop-down menu?

Something like this? This is the data sheet

DATA SHEET:

Column A Column B Column C
Cell 1 Products Variation
Cell 2 Robot Blue
Cell 3 Soldier Yellow
Cell 4 Robot Green

And I was hoping to do something like this When I type "ROBOT" in a cell. The cell next to it would limit my choices/data validation or filter the choices/data validation to only the "Variation" under Robot.

NEW SHEET:

Column A Column B Column C
Cell 1 Products Variation
Cell 2 I type ROBOT here The choices for available variation appears here

Would it be possible?

Share Improve this question edited Mar 24 at 3:49 Alma_Matters 1,0061 gold badge2 silver badges13 bronze badges asked Mar 18 at 5:25 Jenn PomonaJenn Pomona 1 3
  • Please provide a sample sheet and a minimal reproducible example. Also, show us what you have tried so far. – 4thAnd1 Commented Mar 18 at 6:41
  • Please make use of the code block fences (```) so your example table is at least legible. – Stelio Kontos Commented Mar 18 at 21:36
  • See table syntax. (I'd edit the question, but a pending edit is already queued) – Stelio Kontos Commented Mar 18 at 21:49
Add a comment  | 

1 Answer 1

Reset to default 2

Create a drop down of variations for a product

You can achieve this using Google Apps Script, Inserting a value from DATA SHEET to NEW SHEET and creating a drop-down in the next cell in any cell.

Script Used

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var targetSheetName = "NEW SHEET";
  var dataSheetName = "DATA SHEET";

  if (sheet.getName() !== targetSheetName) {
    return;
  }

  var editedCell = e.range;
  var product = editedCell.getValue().toString().toLowerCase();
  var nextColumn = editedCell.getColumn() + 1;

  if (editedCell.getRow() === 1) {
    return;
  }

  var dataSheet = e.source.getSheetByName(dataSheetName);
  var dataRange = dataSheet.getDataRange().getValues();
  var variations = [];

  for (var i = 1; i < dataRange.length; i++) {
    if (dataRange[i][0].toString().toLowerCase() === product) {
      variations.push(dataRange[i][1]);
    }
  }

  var validationRange = sheet.getRange(editedCell.getRow(), nextColumn);

  if (variations.length > 0) {
    var rule = SpreadsheetApp.newDataValidation()
      .requireValueInList(variations, true)
      .build();
    validationRange.setDataValidation(rule);
  } else {
    validationRange.setDataValidation(null);
  }
}

Sample Output

DATA SHEET
Robot Blue
Robot Green
Soldier Yellow
Robot Red

References

  • Data Validation

  • Event Object

发布评论

评论列表(0)

  1. 暂无评论