Fill Dropdown from Selected Item

喜夏-厌秋 提交于 2020-07-23 10:15:26

问题


What I am trying to make is a workbook that users can select from a dropdown of a group of items (Sheet1, Column A) and then Row B lookup that selected item in sheet "Dataset" and return that value with integers that go from 0 to the corresponding stock quantity total in (Sheet "Dataset" column C)

here is a Sample spreadsheet

I got some awesome code from @iamblichus that will fill the dropdowns from the corresponding stock quantity see his code here that I have somewhat implemented Here using a query formula to lookup the group stock quantities. I'm not sure how to make this happen across two sheets though.


回答1:


Answer:

Extending the code that @iamblichus provided here, you can specify the Sheets from which to get the data from and use an onEdit() trigger to automatically change the dropdown when the cell is edited.

Code:

Attach this to the Sample Spreadsheet you provided:

function onEdit(e) {
  var ss = SpreadsheetApp.getActive(); // Get the spreadsheet bound to this script
  var dataSetSheet = ss.getSheetByName("Dataset"); // Get the sheet called "Working with script" (change if necessary)
  var fillSheet = ss.getSheetByName("Sheet 1");

  // Get the different values in column C (stock quantities):
  var firstRow = 3;
  var firstCol = 3;
  var numRows = dataSetSheet.getLastRow() - firstRow + 1;
  var stockQuantities = dataSetSheet.getRange(firstRow, firstCol, numRows).getValues();
  var stockNames = dataSetSheet.getRange(firstRow, firstCol - 1, numRows).getValues();

  // Iterate through all values in column:
  for (var i = 0; i < stockQuantities.length; i++) {
    Logger.log(stockNames);
    Logger.log(stockQuantities);
    var stockQuantity = stockQuantities[i][0];
    var values = [];
    // Create the different options for the dropdown based on the value in column C:

    if (stockNames[i] == e.value) {
      for (var j = 0; j <= stockQuantity; j++) {
        values.push(j);
      }   
      // Create the data validation:
      var rule = SpreadsheetApp.newDataValidation().requireValueInList(values).build();
      // Add the data validation to the corresponding cell in column B:
      fillSheet.getRange(e.range.getRow(), 2).clear();
      var dropdownCell = fillSheet.getRange(e.range.getRow(), 2).setDataValidation(rule);
    }
  }
}

Something to note:

I have put this as an onEdit() function because SpreadsheetApp is called in Read Only mode when inside a custom function and so no set*() methods can be called. This includes setDataValidation().

As per the Documentation, the Spreadsheet service is supported, however under 'Notes' it reads:

Read only (can use most get*() methods, but not set*()). Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).

I hope this is helpful to you!

References:

  • Stackoverflow - Dropdown auto generating a range based on a total
  • Google Apps Script - Range.clear() method
  • Google Apps Script - Simple Triggers
  • Google Apps Script - Custom Functions in Google Sheets


来源:https://stackoverflow.com/questions/59959984/fill-dropdown-from-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!