Disable and Enable cells based on another cell in Google Spreadsheets - google-sheets

The first column of my spreadsheet has dropdown lists in each cell with the options "yes" and "no".
If "yes" is selected, I want to enable the cell in the same row, next column with another dropdown list with options "red" and "blue". If "no" is selected, then the next cell should remain disabled.
So far I have tried the data validation menu with custom formula is set to =if(B2="Yes", "red", "blue") in cell B3, but it doesn't work.
How can I do this?

You can do this with onEdit and script like this:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var col=e.range.getColumn()
var ro=e.range.getRow()
var nota=e.range.getA1Notation()
if(col==1){
var val=e.range.getValue()
if(val=="No"){
s.getRange(ro,2,1,1).setDataValidation(null)
}
else{
s.getRange(ro,2,1,1).clearContent()
var cell = s.getRange(ro,2,1,1);
var rule =
SpreadsheetApp.newDataValidation().requireValueInList(['Red', 'Blue']).build();
cell.setDataValidation(rule);
}}}
Here is a share example. Copy and try it.
https://docs.google.com/spreadsheets/d/1wHA1Rz9U9dr4n8DntLvZWPe4izck7xgi3ygUpx-uNmo/edit?usp=sharing

Related

Force Dropdowns to Display in B2:B When Adjacent Cell in A2:A Has Data

In this sample Google Sheet that you can edit, I'm trying to configure the data validation dropdown menus in B2:B to only display when the adjacent cells in A2:A have data in them. As you can see in my spreadsheet, I have the data validation configured alright, but it inserts dropdown menus all the way down column B, even if there's no data next to it in Column A, and I don't want that.
How can I configure this to where Data Validation dropdown menus only display when there is data in adjacent cells in column A?
Thanks for your help!
You need a script
function onEdit(event){
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (r.getColumn() == 1 && s.getName() == 'Dropdowns'){
r.offset(0,1).clearContent()
if (r.getValue() == ''){
r.offset(0,1).clearDataValidations();
}
else{
var lists = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Colors');
r.offset(0,1).setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(true)
.requireValueInRange(lists.getRange('A2:A'+lists.getLastRow()), true)
.build());
}
}
}

Multiple data validations in google sheets problem

I have a dropdown menu in A1 but I want it to reject input if B1 says "Closed". It won't let me keep the dropdown box as soon as I create the data validation for rejecting input. How do I fix this?
It is not possible to add 2 different data validation criteria in the selected range.
One workaround that you could do is to use data validation to have a drop-down menu and create an onEdit() function in Apps Script that will reject the inputs.
Sample Code:
function onEdit(e){
var cell = e.range;
var col = e.range.getColumn();
var ui = SpreadsheetApp.getUi();
//Check if current edited cell is in column 1 and if the cell besides the current cell is Closed
if(col == 1 && cell.offset(0,1).getValue() == "Closed"){
//Display warning message
ui.alert(
'Warning',
'You cannot modify cells that are closed, returning the original value of the cell',
ui.ButtonSet.OK
)
//return cell to its original value
cell.setValue(e.oldValue);
}
}
What it does?
Check if the modified cell is in column 1 and verify if the adjacent cell on its right has the value "Closed"
If true, display a warning message and return the cell to its original value
Output:
I used Range.offset(rowOffset, columnOffset) to get the value of the cell on the right side of the current cell by using +1 as column offset.
Additional Reference:
Container-bound Scripts
Google Sheets events

How can I apply multiple lines to the conditioned choices of a drop-down menu-Google-Spreadsheet?

I found a guide that allows me to condition a drop-down menu, depending on the choice made by another. The method I found, allows me to make this choice only for a couple of cells. How can I extend to more pairs of lines?
The steps I have taken are the following:
=Match(Pers!F3;C1:C;0)
=TRANSPOSE(INDIRECT("D" & B5 & ":K" & B5))
My list is composed:
With the formulas written before, I can get a cell conditioned by its previous one. If I select the Zone, I will present myself in the drop-down menu, only those in the area. Example if I choose Zone 1, I can choose New York, West Virginia, Virginia.
If I try to drag, I will always be able to select the states of the first Zone.
How can I do multiple lines?
Thank you!
If you don't mind using script, try this. The dropdowns are on Sheet1 and Zone and city data on Sheet2.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var sheet=e.source.getSheetName()
var target=ss.getSheetByName("Sheet2")
var col=e.range.getColumn()
var ro=e.range.getRow()
if(col==1){
var val=e.range.getValue()
if(sheet=="Sheet1" && val=="Zone1"){
s.getRange(ro,2,1,1).clearContent()//clear cell to update
var cell = s.getRange(ro,2,1,1)//get cell to update
var z1=target.getRange(2,2,1,3)//get cities for Zone1
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(z1)//define the rule
cell.setDataValidation(rule)//set the data validation rule
}
else{//for Zone2
s.getRange(ro,2,1,1).clearContent()
var cell = s.getRange(ro,2,1,1);
var z2=target.getRange(3,2,1,3)
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(z2)
cell.setDataValidation(rule);
}}}
Here is my test sheet you can copy and try. https://docs.google.com/spreadsheets/d/1z9Pw8rvFvJNhItqOUaMkmUt8lohwTJKA22GVgPAIfWU/edit?usp=sharing

Display a Message box based on a value in a cell in Google spreadsheet

I am trying to display a browser message box based on a particular value in range. I tried with the following
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var panType = sheet.getRange("A4:A100").getValues();
if (panType=="CSF");
Browser.msgBox('Make sure you enter the PAN No in the alloted slot');
}
But the problem is, with each and every edit in any cell, the message box pops up.
The said range A4:A100 has a validation dropdown with options like "New" and "CSF". So whenever the value CSF is chosen, the dialogue box should popup.
Thanks
You just need to check if it is the xorrect sheet, column, and value. Event has these values. If they match, show your messagebox.
function onEdit(event){
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Sheet1"){
if(editedCell.getColumn() == 1 && event.value=="CSF"){
Browser.msgBox('Make sure you enter the PAN No in the alloted slot');
}}}

Linking cells within one google spreadsheet

I am trying to create 'buttons' at the top of a spreadsheet, so when clicked the reader can jump around the spreadsheet. Ive created an editorial calendar and would like to be able to jump to different months on the sheet. Is this possible?
You can't add buttons but you can add a custom menu using Google Apps Script:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#addMenu(String,Object)
You can write a jump script using this Google Apps Script function: https://developers.google.com/apps-script/reference/spreadsheet/sheet#setActiveSelection(Range)
Here is a full working example:
function jumpToJanuary() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Now fixed cell - here you can search for specified cell value
var range = sheet.getRange("B4");
sheet.setActiveSelection(range);
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Jump to January",
functionName : "jumpToJanuary"
}];
sheet.addMenu("Jump Example Menu", entries);
};

Resources