TLDR: script to fill dropdown creating integers that go from 0 to the corresponding Stock Quantity from dataset.
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)
this is all working thanks to @iamblichus and @Rafa Guillermo
but a also have items that do not require users to select the item from a dropdown.
I have tried to write some code (line 39 down) but i am stuck.
Any help would be very appreciated.
function generateDropdowns() {
//sets all the dropdowns from items non selected
// Get the different values in column C (stock quantities):
var firstRow = 3;
var firstCol = 3;
var numRows = dataSetSheet.getLastRow() - firstRow + 1;
var numRowsfill = fillSheet.getLastRow() - firstRow + 1;
var stockQuantities = dataSetSheet.getRange(firstRow, firstCol, numRows).getValues();
var stockNames = dataSetSheet.getRange(firstRow, firstCol - 1, numRows).getValues();
var itemName = fillSheet.getRange(3, 1, numRowsfill).getValues();
// Iterate through all values in volumn:
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] == itemName[i]) {
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:
var dropdownCell = fillSheet.getRange(i + firstRow, 2).setDataValidation(rule);
}
}
}
Past posts Dropdown auto generating a range based on a total enter link description here