Google Sheet: Show first data validation value if cell is empty - google-apps-script

Good afternoon everyone,
I am looking for a way for my dropdown list to show the first value if the cells A18 and F18 are empty. The data validation range is from cell X18 to AA18 (4 values).
So basically, when I enter the name 'Test', it will fetch the data: "Healer", "Tank", "Paladin" and "Mixed DPS" from another tab and display it in the cells X18 to AA18. Using data validation a dropdown list is being generated with these values. However, as soon as I enter a name, I want the default value of the dropdown list to be the value of X18.
So: if A18 is empty, the value of F18 should be matching X18. (if no name is given, there wont be anything in F18 so it should stay empty)

If I understand correctly just put the formula in Cells F18:F.
=IF($A18<>"",$X18,"")
If column A is not empty then fetch the value in column X, otherwise blank (or select from dropdown). Take a look at the sheet you linked, I have updated it.
Selecting from the dropdown will overwrite the formula, if this is the issue you're referring to your only fix is to create a helper column or utilize scripts.
UPDATE
In order to accomplish this with scripts you can add the following:
function onEdit(e) {
var editRange = { // A18:A250
top : 18,
bottom : 250,
left : 1,
right : 1
};
//Get sheet from which the event occured
var range = e.range
var sheet = range.getSheet();
// Exit if wrong sheet
if (sheet.getSheetName() != "War Board") return;
// Exit if we're out of range
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
//Set default if value is not blank
if (!range.isBlank()) range.offset(0, 5).setValue(range.offset(0, 23).getValue());
//Set blank if value is blank
if (range.isBlank()) range.offset(0, 5).setValue("");
}
This will set the value of F to the corresponding value in X column if a name is given in column A. If the cell in column A is emptied it empties cell F.

Related

find value but replace a different cell

I have a 2 columns 1 column with data and the other with with drop-down lists in every cell.
When I make a choice on the dropdown list I want google sheet to auto select the same items the the other dropdown lists based on the value of the cell from the first row.
(I dont want to do the same thing over and over)
Currently I am struggeling on when I found a cell with a specific value, how do I then replace the value of another cell. I found some examples using textFinder, but now it replaces the value of the current cell.
When I found the value I need to target the cell that I want to modify (the cell on the second column)
I have the following script:
function onEdit() {
var activesheet = SpreadsheetApp.getActiveSheet()
var Cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var Column = Cell.getColumn();
if (activesheet.getName()=='Transacties'){
if (Column == 5 && SpreadsheetApp.getActiveSheet()){
var Target = SpreadsheetApp.getActiveSheet().getRange(Cell.getRow(), Column + 1);
var Reknr = SpreadsheetApp.getActiveSheet().getRange(Cell.getRow(), Column + -1); //this is the bill column
var Juistecat = SpreadsheetApp.getActiveSheet().getRange(Cell.getRow(), Column + 0); //this is the drop-list column
var Options = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(Cell.getValue());
var Valrek = Reknr.getValue(); //waarde huidig rekening nr
var Valcat = Juistecat.getValue(); //waarde huidig rekening nr
SubcategoryDropdown(Target, Options);
}
//Reknr.setValue("Hello"); //werkt, juiste cell iig
// var vv = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
SpreadsheetApp.getUi().alert("The active cell value is "+Valrek);
SpreadsheetApp.getUi().alert("The active cell value is "+Valcat);
var textFinder = Reknr.createTextFinder(Valrek);
var range = SpreadsheetApp.getActive().getRangeByName("Reknr");
var values = range.getValues();
SpreadsheetApp.getUi().alert("The active cell value is "+valrek);
values.forEach(function(row) {
row.forEach(function(col) {
textFinder.replaceAllWith(Valrek); // But I dont want to replace the current, I need to replace
//Target.setValue("Hello");
});
});
}
I made a sample code which should do what you need. However, since I do not know how the information is arranged in your Google Sheet, I cannot customize the code to match exactly with the information on your sheet and some changes will be necessary.
I tried to comment on the code as much as possible so it can be easily changed and customized to your data.
This is how I arrange my data:
function onEdit(e) {
// you need to add the name of your sheet
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
// Automatically gets the range that was edited
const range = e.range;
// Gets the A1 notation of the cell example 'B2'
let current_cell =range.getA1Notation();
// Gets the value in the range that was modify
// In this case the value inside the drop-down
let value_selected = sheet.getRange(current_cell).getValue();
// Gets the column that was edited
col = range.getColumn();
// My drop-down was in column B
// This will filter only the changes on column B, and excludes the rest
// you can change this to the column where you drop-down is located
if (col == 2){
//Gets the number of row where the value in column B was change
let row = range.getRow();
// The data I was searching was in column 1, so I use 'A'+row
// You can replace the A for the column where the search will be done
new_cell = sheet.getRange('A'+row).getValue();
// Searches all the values in column A
// that match the one next to the value edited
list = sheet.createTextFinder(new_cell).findAll();
// Loops inside the list of array that was created with the search
for (let i = 0; i < list.length; i++){
let ranges_info = list[i];
// Gets the row in each iteration of the range inside 'list'
let ranges_row = ranges_info.getRow();
// set the value, I selected in the first drop-down
// to the rest of the cells in B that match the same value in A
sheet.getRange('B' + ranges_row).setValue(value_selected)
};
}
}
And here is a gif with the code running:

I am trying to set the value of multiple cells when the value of once cell changes in google sheets

I am trying to set the values of several cells in the same row of a cell that I am changing.
IF the value in AJ2 is True then the range AK2:AX2 should also be true.If I change AJ2 to false, then I should be able to uncheck any of the values in the range and they would then be false. I 'sort of' have this logic working. IF I have AJ2 checked and uncheck AK2, when I uncheck AJ2 it rechecks AK2.
I also need this to happen on all rows if I change the value in the column of AJ for their rows. not just row 2. I am fairly new to working with sheets and could use some help.
function onEdit(e) {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getRange("AJ2");
var range =ss.getRange("AK2:AX2")
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 36&&ss.getActiveSheet().getName()=="MasterData") {
if (Cell = "TRUE")
range.setValue("TRUE")}
}
Try this
function onEdit(e) {
const sheet = e.source.getActiveSheet();
const cell = e.range;
// AJ = column 36 (master column)
if(sheet.getName() == "MasterData" && cell.getColumn()==36 && cell.isChecked()){
sheet.getRange(cell.getRow(),37,1,14).setValue('TRUE')
}
// individual columns : 37 (AK) to 50 (AX)
if(sheet.getName() == "MasterData" && cell.getColumn()>=37 && cell.getColumn()<=50 && !cell.isChecked()){
sheet.getRange(cell.getRow(),36,1,1).setValue('FALSE')
}
}
explanation
when a box is checked in column 36 (AJ = 'master column'), all the boxes of the same row between 37 to 50 (AK to AX) will be checked
when an individual box is unchecked between column 37 to 50 (AK to AX) the master box of the same row will be also unchecked
reference
onEdit trigger
The onEdit(e) simple trigger returns an event object.
The event object contains the information about the spreadsheet that was edited, which you use instead of the "SpreadsheetApp.getActiveSpreadsheet()"
To access the information in the event object, you need to use the 'e':
function onEdit(e) {
//the sheet that was edited
var sheet = e.source.getActiveSheet();
//the cell that was edited
var cell = sheet.getActiveCell();
//the row number of the cell that was edited
var row = cell.getRow();
//the column number of the cell that was edited
var column = cell.getColumn();
From there, proceed as you normally would with sheets and ranges.
To update multiple cells at once, you can set the range with getRange(row, column, numRows, numColumns) and then setValues(values). Note that it's setValues(), not setValue().

Hiding Columns based on a Cell Values

I am trying to hide columns based on the selection in A2.
Here is the code I am working with and I am hard stuck.
function onEdit(e) {
var myRange = SpreadsheetApp.getActiveSheet().getRange("A:J");
//Let's get the row & column indexes of the active cell
var row = e.range.getRow(1);
var col = e.range.getColumn(0);
//Check that your active cell is within your named range
if (col >= myRange.getColumn() && col <= myRange.getLastColumn() && row >= myRange.getRow() && row <= myRange.getLastRow()) {
if e.myRange === "Renewal" ? myRange.hideColumns(5)
}
}
If anyone could point me in the right direction it would be greatly appreciated.
Based on your code, I assume you want to hide column 5 (E) if dropdown in A2 is "Renewal". If that's what you want, then you got many issues in your script.
Code:
function onEdit(e) {
// get current sheet
var sheet = e.source.getActiveSheet();
// get row and column of edited cell
var row = e.range.getRow();
var col = e.range.getColumn();
// make sure to only hide/show column if edited cell is A2 in 'Sheet1'
if (col == 1 && row == 2 && sheet.getSheetName() == 'Sheet1') {
// if value of A2 in Sheet1 is Renewal, hide columns, if not Renewal, then show the same columns back
if (e.value === "Renewal") {
sheet.hideColumns(5, 2); // hide column 5-6 (E-F)
sheet.hideColumns(8, 1); // hide column 8 (H)
}
else {
sheet.showColumns(5, 2); // show column 5-6 (E-F)
sheet.showColumns(8, 1); // show column 8 (H)
}
}
}
Mistakes:
getRow() and getColumn() gets the current row and column of the range respectively. You don't pass a parameter here
You do not need to identify if your edited cell is within the range A:J, you only need to specify that row and column should be pointing to A2 (and a specific sheet if needed be which is considered in )
Ternary operators do not need if statement before it and it always need the else part. Thus I added showColumns to show the hidden column if A2 is not Renewal.
hideColumns() is a sheet method, not range, thus you need to get the sheet you currently edit. Additionally, it accepts 2 parameters, the index where it starts hiding a column and how many columns you need hidden.
Output:
Resources:
getRow/getColumn
hideColumns/showColumns
Ternary operators

Automatically hide rows based on other cell value

We have a spreadsheet that automatically attributes status for each row based on information enteredin cells located in column T. The stauts appear on column M showing open, pending and closed. I'm using the following code:
// Sheet the data is on.
var SHEET = "DLT";
// The value that will cause the row to hide.
var VALUE = "Closed"
// The column we will be using
var COLUMN_NUMBER = 14
function onEdit (e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
//Ensure on correct sheet.
if(SHEET == activeSheet.getName()){
var cell = ss.getActiveCell()
var cellValue = cell.getValue();
//Ensure we are looking at the correct column.
if(cell.getColumn() == COLUMN_NUMBER){
//If the cell matched the value we require,hide the row.
if(cellValue == VALUE){
activeSheet.hideRow(cell);
};
};
};
}
I understand that there is a mistake on the code, as the spreadsheet changes automatically the "stauts" value on column M as soon as information is entered on column T, and this code this won't work because technically the Active.cell bit is telling the script to look at the current cell being edited (which in this case will be the cell on column T as this is the last field to be filled before triggering the spreadsheet change status to closed.)
Doubt is, how to make the script still looks into the current row being edited but consider the value on column M and not the active cell.
function onEdit (e) {
var sh = e.range.getSheet();
//column 20 is T
if(sh.getName()== 'DLT' && e.range.columnStart == 20 ){
SpreadsheetApp.flush();//This will provide enough time to allow the spreadsheet to complete all of it's calculations.
//column 13 is M
if(e.range.offset(0,-7).getValue()=="Closed") {
sh.hideRows(e.range.rowStart);
}
};
}

How do you show/hide rows based on a particular cell value?

I am attempting to put some show/hide row logic into a google sheet based on a response from a data validation list.
If the user selects the value 'General Account Call' from the data validation list in cell C42 I would like to show rows 43 through to 53 and rows 78 through to 101. Also if a user ticks a check box in cell C40 I would like to show row C41.
I don't know if I can 'monitor' multiple cells with the onEdit function.
I have found a couple of similar(ish) scripts which hide a row based on a value in a cell in the same row and have tried amending them to no avail. I have then tried going back to basics and putting something to monitor a cell and hide a different row, again to no avail
function onEdit(e) {
var ss = SpreadsheetApp.getActive()
var sheet = SpreadsheetApp.getActiveSheet()
var cell = sheet.getRange('D39')
var cellContent = cell.getValue()
if(cellContent === Y) {
Sheet.hideRow(40)
}
}
Your code should be:
function onEdit() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange('D39');
var cellContent = cell.getValue();
if(cellContent == 'Y') {
sheet.hideRows(11);
}
}
There were a few things wrong:
Even if the ss and sheet declaration work, you were getting the
Sheet from the SpreadsheetApp and not from ss, so ss was not
being used.
cellContent == 'Y' is the correct way to compare the cell to a String, if you remove the quotes, it will interpret it as a variable Y that doesn't exist. Also, === is not necessary as it checks the type and the value, and we only want the value. In fact, it will return false even if the values are the same but are a different type
You wrote Sheet.hideRow instead of sheet.hiderow. Javascript is case-sensitive, so be careful with that!
The method hideRow hides the rows in a given Range, so we have to use hideRows, were you can use the integer (11 in this case) to hide the entire row.
You didn't put any semicolons ;
You didn't get any of these errors as I suppose you were testing the code changing the value of D39. If you execute the code manually, all of these will show up.
So, in order to hide several rows depending of a few cells, you have to get the event information (e), and get the cell that is being edited.
Do an if like this:
var cell = e.range.getA1Notation(); //Gets the Cell you edited
var cell_value = e.range.getValue(); //Gets the value of the cell
if(cell == 'C42' && cell_value == 'General Account Call'){
sheet.showRows(43, 10);
sheet.showRows(78, 23);
}
if (cell == 'C40' && cell_value){ //The checkbox value returns true or false
sheet.showRows(41);
}
The second number in showRows is the number of rows that will be shown.