Using cell contents to modify drop-down menu list - validation

I have a Google Sheets where I am saving information about universities and I have a column for regions and countries (regions or continents, however you want to see it) and what I'd like to do is have the regions column contain a drop-down menu (data validation) of all the continents and depending on what continent you select, the countries column will have the corresponding countries.
I have another sheet containing the required data with the first row being the continent names and the remaining rows being the countries in that continent.
I've tried custom formulas but ended up having something too complex that didn't work so I deleted it.
If anyone knows how I could do this with either the script editor or directly in data validation, that'd be great!

Continent and Country Dropdowns
I apologize but this is a little involved. Let's start with where I got the data. This is the website: https://datahub.io/JohnSnowLabs/country-and-continent-codes-list
I actually just copied and paste it into a Sheet named "C&C". I sorted it by continents and then by countries and I depend upon it remaining like that since it reduces the amount of effort to get the countries.
And here is the Google Script Code:
function onEdit(e) {
var dlm=" - ";
var msg="Start" + dlm;
var sh=e.range.getSheet();
if(sh.getName()!='DropDown')return;
if(e.range.getA1Notation()=='A2' && e.value) {
var dsh=e.source.getSheetByName('C&C');
var drg=dsh.getRange(1,1,dsh.getLastRow(),1);
var dvA=drg.getValues();
var cA=[];
var cStart=0;
var cEnd=0;
for(var i=0;i<dvA.length;i++) {
if(!cEnd && !cStart && dvA[i][0]==e.value) {
cStart=i+1;
msg+="cStart: " + cStart + dlm;
//e.source.toast(msg);
}
if(!cEnd && cStart && dvA[i][0]!=e.value || i==dvA.length-1) {
cEnd=i+1;
msg+="cEnd: " + cEnd + dlm;
//e.source.toast(msg);
break;
}
}
var crg=dsh.getRange(cStart,3,cEnd-cStart+1,1);
var cvA=crg.getValues();
e.source.getRangeByName('Countries').clearContent();
sh.getRange('B2').clearContent();
e.source.getSheetByName('NamedRanges').getRange(2,2,cvA.length,1).setValues(cvA);
}
msg+="Exit" + dlm;
e.source.toast(msg);
}
I left the debug toasts in there in case you want them.
Here's what the C&C page looks like:
And here's the page where I placed the NamedRanges. I named that page NamedRanges:
And here's the page with the two dropdowns:
It's displaying the South America continent selection.
Here's the NamedRanges setups:
And here's the Data Validation Setups:

Related

Google Sheets - QUERY, adding a cell to hide a row

I'm hoping to use a Google Form to have my students sign up for activities. I added a column to the right of the form data where I can put an "x" once I have met with them. I have a separate tab called "Ordered" where I use QUERY to sort and show only the entries without an x. Once I meet with a student, I can put an x on the original data tab, and the entry will hide from the Ordered tab (kind of a queue for my students).
I would love to not have to switch between the 2 tabs every time I have to check someone off. Is there a way to add a similar column on the Ordered tab that will hide the finished entry?
Here is the sheet I'm referring to:
https://docs.google.com/spreadsheets/d/1fQHF0EoGLk5NEI6GvyRk4InBl-uEq0jq7qqmyOmFjy8/edit#gid=379798836
I'm sharing this sheet with other teachers who aren't familiar with spreadsheets, so the solution has to be ludite friendly.
As #Aerials said, you are likely to run into circular dependency issues here. Because of this, I'd suggest using Apps Script and get rid of the QUERY formula. You want to do the following:
Every time someone submits the form, the Ordered sheet gets updated with the new submission data.
Every time an x is added to the Form Responses 1 sheet, the corresponding row in Ordered gets removed.
Every time an x is added to the Ordered sheet, the corresponding row in Ordered gets removed, and an x gets added to the corresponding row in Form Responses 1.
A possible way to go would be something along the following lines (open the script bound to your spreadsheet by clicking Tools > Script editor):
Install an onFormSubmit trigger so that Ordered gets updated with new data every time the form is submitted. The trigger can be installed manually or programmatically, copying this function to your script and running it once:
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("copyAndFilterData")
.forSpreadsheet(ss)
.onFormSubmit()
.create();
}
Once the trigger is installed, the function copyAndFilterData will execute every time the form is submitted. This function should updated Ordered based on the data in Form Responses 1, removing all the submission that have been marked as complete x. This function could be the following:
function copyAndFilterData() {
var ss = SpreadsheetApp.getActive();
var sheet1 = ss.getSheetByName("Form Responses 1");
var sheet2 = ss.getSheetByName("Ordered");
var sourceData = sheet1.getDataRange().getValues();
var filteredData = sourceData.filter(sourceRow => sourceRow[5] !== "x")
.sort((a, b) => a - b)
.map(sourceRow => {
sourceRow.pop();
sourceRow.splice(1, 1);
return sourceRow;
});
sheet2.getRange(1, 1, sheet2.getLastRow(), 4).clear({contentsOnly: true});
var destRange = sheet2.getRange(1, 1, filteredData.length, filteredData[0].length)
destRange.setValues(filteredData);
}
At this point, another piece of functionality would be needed: update the spreadsheet based on the addition or removal of x on any of both sheets. For this, an onEdit trigger would be needed, like this:
function onEdit(e) {
var editedSheet = e.range.getSheet();
if (editedSheet.getName() === "Form Responses 1") copyAndFilterData();
else if (editedSheet.getName() === "Ordered") removeRows(e, editedSheet);
}
Once this function is copied and the project saved, every time Form Responses 1 is edited, the Ordered sheets gets updated (function copyAndFilterData), and every time Ordered is edited, the function removeRows is fired. This function should do two things: (1) remove the row which was marked as complete in Ordered (x added) and (2) add the corresponding x to Form Responses 1. The submission dates can be compared to identify the row:
function removeRows(e, editedSheet) {
var range = e.range;
var column = range.getColumn();
var row = range.getRow();
var value = range.getValue();
if (column == 5 && row > 1 && value == "x") {
var date = editedSheet.getRange(row, 1).getValue();
var formSheet = e.source.getSheetByName("Form Responses 1")
var values = formSheet.getRange(2, 1, formSheet.getLastRow() - 1).getValues();
var markedRow = values.findIndex(value => value[0].getTime() === date.getTime());
formSheet.getRange(markedRow + 2, 6).setValue("x");
editedSheet.deleteRow(row);
}
}
Reference:
Overview of Google Apps Script
Simple Triggers
Installable Triggers

Oracle APEX Interactive Grid: Is there a way to get the corresponding column name, when clicking in a data cell in the Interactive Grid

I have a Dynamic Action which should get the data from the model, depending on the clicked column. Let 's say I have two columns in the interactive grid, column A and B. Depending on the column I clicked in, the DA should be executed and execute a query with the value from column A or B.
The DA is actived on doubleclick and I have the following source to get the value from the IG model.
var regionStaticId = $(this.triggeringElement).closest("div.js-apex-region").attr('id');
var grid = apex.region( regionStaticId ).widget().interactiveGrid("getViews", "grid");
var model = grid.model;
var record = grid.getSelectedRecords()[0];
var value;
// Code to find the the clicked column comes here
if (record) {
value = model.getValue(record, columnName);
}
Now, what I can do is add an extra css class to the particular cells, with the name of the source column. But that would be like hardcoding in my opinion. Like this.
if ($(this.triggeringElement).hasClass('columnA')) {
columnName = 'COLUMN_A';
}
else if ($(this.triggeringElement).hasClass('columnB')) {
columnName = 'COLUMN_B';
}
Is there a way to determine the clicked column, based on the triggering element?
Help is very much appreciated.

Multiple dependent dynamic dropdowns with repeating column dropdowns in Google Sheets

The Google Sheet I have uses code made by user Max Makhrov, code here, to make multiple dependent dynamic dropdowns in columns D-F (for location) and columns H-L (for objectives & activities) in my sample sheet here.
I would like help to modify the script to do two things:
Whatever activity is selected from the dropdown menu in Column I, I would like the same dropdown menu options to be available (to repeat) for columns J-L. As you can see I found a way to do it, but to me it seems clunky and not ideal, and leaves too much room for errors. Users should not select the activity twice, but I've put conditional formatting in to flag that if they do. However:
Ideally, but less importantly, if the dropdown menu items could still repeat for columns J-L but once an activity is selected in previous cells, that option is removed from each of the following repeated dropdown menus in additional columns, up to and including column L. This would help avoid accidentally repeating an activity.
NB: Reference question "How do you do dynamic / dependent drop downs in Google Sheets?"
Thank You!
When one of the drop-down cells is edited you can use an onEdit trigger [1] to iterate through the 4 columns (I-L) and update the drop-downs in each cell removing the option selected in the edited cell. You also need to add the old selected value (previously deleted from other options) to the other drop-downs. For this, you can use getDataValidation [2] and getCriteriaValues [3] functions chained to a Range object to retrieve the current drop-down values array on that range and delete the option matching with the selected option.
Use newDataValidation() [4] function to create a new rule using your updated drop-down values array and setDataValidation [5] function to set the rule to the range.
function onEdit(event) {
var range = event.range;
var sheetName = range.getSheet().getSheetName();
var col = range.getColumn();
var newValue = event.value;
var oldValue = event.oldValue;
//If the edited range is in sheet '3W' and beetween columns I-L
if(sheetName == '3W') {
if(col>=9 && col<=12) {
for(var i=9; i<13; i++) {
//Don't change anything for edited cell
if(col == i) { continue; }
else {
//Get range to update and current dropdown values for that range
var rangeToUpdate = range.getSheet().getRange(range.getRow(), i, 1, 1);
var dropdownValues = rangeToUpdate.getDataValidation().getCriteriaValues()[0];
//Find new edited value and delete it from options array
var index = dropdownValues.indexOf(newValue);
if (index > -1) {
dropdownValues.splice(index, 1);
}
//If previous selected value is not beetween the options, add it
if(oldValue && dropdownValues.indexOf(oldValue) == -1) {
Logger.log(oldValue)
dropdownValues.push(oldValue);
}
//Set new dropdown values to range
var updatedRule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownValues, true).setAllowInvalid(false);
rangeToUpdate.setDataValidation(updatedRule);
}
}
}
}
}
Run just the first time to set all the drop-downs in columns I-L, which are get it from range E1:E10:
function setDropdownsInitially() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Range with the dropdown values
var sheet = ss.getSheetByName("indicators");
var dropdownValues = sheet.getRange("E1:E10").getValues();
//Data validation rule
var rule = SpreadsheetApp.newDataValidation().requireValueInList(dropdownValues, true).setAllowInvalid(false);
//Range where the dropdowns will be created
var targetSheet = ss.getSheetByName("3W");
var cells = targetSheet.getRange("I2:L");
//Set data validation rule
cells.setDataValidation(rule);
}
[1] https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
[2] https://developers.google.com/apps-script/reference/spreadsheet/range#getdatavalidation
[3] https://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder.html#getcriteriavalues
[4] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#newdatavalidation
[5] https://developers.google.com/apps-script/reference/spreadsheet/range#setdatavalidationrule

Dynamic data validation based on other cell values, and copying/filling validation criteria relatively down each row (Google Sheets)

I am trying to achieve the following situation using Data Validation in Google Sheets. I've provided a truncated version of situation in the image below.
I would like to set up data validation in Column B that automatically checks the options in column C (or multiple columns in the same row) to populate the dropdown menu for that row.
I did notice that Google Sheets has an option for formulas in the data validation screen and I tried writing an array formula in this area but I have not been having any luck getting any sort of output.
If it can't be done through this menu I would appreciate any idea how to achieve this through scripts.
One way to achieve this is to select list from a range in the "Criteria" section of the Data Validation editor. Then, select the cells whose values you want to appear in your dropdown.
The only issue is that when you try to fill this down across the column, Google Sheets will not update the criteria range. Say you do the following:
In cell B2, use data validation based on list from a range and select D2:F2 as the criteria range. Now the dropdown in B2 will have the options John's Mom, John's Dad, John's Grandma
Then, fill down or copy this cell down across the whole column
When you open the dropdown for cell B3, it will still have the options John's Mom, John's Dad, John's Grandma
Unfortunately Google Sheets does not currently have a built-in solution for copying/filling data validation references or formulas relatively, as far as I know. But it looks like somebody already wrote a nice script in this Google Docs forum post. To avoid just a link as an answer, I'm going to copy in the script and instructions here. Credit to AD:AM from Google Docs forum.
How to use their script:
Select a range of cells across which you want to copy a data validation rule, relatively
From the Validation+ custom menu, select the appropriate option (all references relative, columns absolute, or rows absolute)
The validation of the upper-left cell will be copied to the rest of the range
Link to original solution's example Google Sheets with script already included - you can save your own copy and then start using.
Or to recreate from scratch, here is the script.
function onOpen()
{
SpreadsheetApp.getActiveSpreadsheet().addMenu
(
"Validation+",
[
{name: "Copy validation (all relative references)", functionName: "copyValidation"},
{name: "Copy validation (relative rows, absolute columns)", functionName: "copyValidationColumnsAbsolute"},
{name: "Copy validation (absolute rows, relative columns)", functionName: "copyValidationRowsAbsolute"}
]
);
}
function copyValidation(rowsAbsolute, columnsAbsolute)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var r = ss.getActiveRange();
var dv = r.getDataValidations();
var dvt = dv[0][0].getCriteriaType();
if (dvt != SpreadsheetApp.DataValidationCriteria.VALUE_IN_RANGE) return;
var dvv = dv[0][0].getCriteriaValues();
Logger.log(dvv);
for (var i = 0; i < dv.length; i++)
{
for (var j = i ? 0 : 1; j < dv[0].length; j++)
{
dv[i][j] = dv[0][0].copy().withCriteria(dvt, [dvv[0].offset(rowsAbsolute ? 0 : i, columnsAbsolute ? 0 : j), dvv[1]]).build();
}
}
r.setDataValidations(dv);
}
function copyValidationRowsAbsolute()
{
copyValidation(true, false);
}
function copyValidationColumnsAbsolute()
{
copyValidation(false, true);
}

Limiting the results of drop-down in Google Sheets

I work as an operations manager at at small business and I'm trying to set up an order sheet that is easy for the salesman to use. In the order sheet, I've used the OFFSET function to refer to a master list containing customers and prices. Under Customer, I type the customer and it draws it from the master list via an auto-complete drop-down. The same happens with the product.
Here is the order sheet:
Order Sheet Example
My issue is I'll begin typing in the product e.g. 'prawn'. We have over a dozen prawn lines, but a particular customer will only take one. All of the other prawn results have no prices for that customer. However, the auto-complete function will offer up all the 'prawn' results.
In the Master list, I've entered prices for only the products that the particular customer uses. Take a look at what the Master List looks like:
Master List look
Without an excellent memory of what customer wants what, it's an exercise in trial and error. In the above example, I could type 'topside' and if I select the wrong one, no price comes up.
This is frustrating.
I was hoping for a way to limit the auto-complete so that when I type 'prawn' or 'topside' for that customer, it only comes up with auto-complete fields that have the price in it. Can anyone help? Or does anyone know of any work-arounds? I'd be really thankful, the current order system is quite difficult.
I believe this will do what you want. It is a little difficult to tell from your sample data. It uses google apps script and sheet names and/or columms may need to be changed in the script for your data. I am attaching a sample spreadsheet you can copy then try. You will have to approve the script in the copy you make.
function onEdit(e) {
var cust=e.value //The value of the edited cell
var sh=e.source.getActiveSheet().getSheetName()//Name of the active sheet.
var col=e.range.getColumn()//The edited column
var r=e.range.getRow()//The edited row
var row=e.range.offset(0,1).getA1Notation()//Cell A1 notation of cell in same row one column to the right.
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet().getSheetName()//Name of active sheet
var s1=ss.getSheetByName("Sheet1")//Variables for sheets
var s2=ss.getSheetByName("Sheet2")
var s3=ss.getSheetByName("TEMP")
var rng=s2.getDataRange().getValues()//Customer/Products
if(sh=="Sheet1" && col==1){//If sheet1 is active sheet and Customer (column A) is edited.
var array=[]//Array to hold customers products
for (var i=0;i<rng.length;i++){
if(rng[i][0]==cust ){
for(var j=1;j<rng[0].length-1;j++){
if(rng[i][j]!="" ){//If customers product has $ entry add to Array.
array.push([rng[0][j]])
}}}}
s3.clearContents()//Clear old product list from TEMP.
s1.getRange(row).clearContent()//Clear product dropdown
s3.getRange(1,1,array.length,1).setValues(array)//Set new customer product list in TEMP.
drop(row,cust)// Call drop function to build new dropdown.
}
if(sh=="Sheet1" && col==2){//If sheet1 is active sheet and Product (column B) is edited.
var cust1=e.range.offset(0,-1).getValue()//Get customer in A
var prod=e.range.getValue()//Get selected product
for (var i=0;i<rng.length;i++){//Get the customer/product price
if(rng[i][0]==cust1 ){
for(var j=1;j<rng[0].length-1;j++){
if(rng[0][j]==prod){
price=rng[i][j]
s1.getRange(r,5).setValue(price)//Set the price in column E
s1.getRange(r,2).clearDataValidations() //Remove the data validation dropdown in column B
}
}}
}}}
function drop(row,cust){
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s3=ss.getSheetByName("TEMP")
var s1=ss.getSheetByName("Sheet1")
var cell = s1.getRange(row);//set validation in B
var ocell=s1.getRange(row).offset(0, -1).getValue()//evaluate value in A
var cellVal=cell.getValue()
if(ocell==cust){
var lr= ss.getSheetByName("TEMP").getLastRow()
var range = ss.getSheetByName("TEMP").getRange(1, 1, lr, 1)
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();//Build the dropdown
cell.setDataValidation(rule)}//Set the validation rules (Customers
products)
}
Test spreadsheet:
https://docs.google.com/spreadsheets/d/1u86sdf1_mO-Mv7hQM_hRZl3Gma-Y2lsu9ZvxSW4jA1U/edit?usp=sharing

Resources