Add Checkbox in Sheets based on Criteria - google-apps-script

I'm looking for a way to add a functional checkbox to a cell, based on the value of another cell. I assume it may need to be script, as everything I've found so far has stated checkboxes are input only, and cannot be created with formulas.
I'm looking to create an unchecked checkbox in column C if the value in column B is a certain value (="Tech Notes"). If the value in column B does not fall under the specific criteria, no checkbox should appear at all in column C.
I did find a thread where checkboxes would appear in all cells of a column, but would be checked/unchecked based on criteria, but I'm looking for the criteria to determine whether the checkbox appears at all.
Here's a sample sheet
Is this possible?
Thank you!

Solution:
Since a script is acceptable, you can make a simple trigger to create and remove checkboxes upon editing the cell:
function onEdit(e) {
if (e.range.getColumn() == 2) {
var sheet = e.source.getActiveSheet();
if (e.value === "Tech Notes" ||
e.value === "Intake Process")
sheet.getRange(e.range.getRow(),3).insertCheckboxes();
else
sheet.getRange(e.range.getRow(),3).removeCheckboxes();
}
}
You don't need to execute this function manually in Google Apps Script, it will trigger everytime you edit the sheet.
Sample Data:
References:
Insert Checkboxes
Simple Triggers

Related

Show and hide selected columns with single macro in google sheets

Starting out like all others. I am new to macros and I know what I'm trying to do is easy, but I can't get it working.
So I'm making buttons on a spreadsheet to show and hide columns, if the column is hidden then show, if the column is shown then hide. So a toggle button between show and hide basically.
I will continue to research while this is here as I'm sure the answer is simple. Just an If, then, else but I'm a noob.
Any help would be appreciated. Here's one of the 'show' ones I have if it's easier to edit that.
Thanks
function ShowCA() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().showColumns(6);
};
I believe your goal is as follows.
About the column "F", when the column is showing, you want to hide the column. When the column is hiding, you want to show the column.
You want to achieve this using a function of Google Apps Script.
In this case, I thought that isColumnHiddenByUser can be used. So, how about the following sample script?
Sample script:
function ShowCA() {
var column = 6; // This is column "F".
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.isColumnHiddenByUser(column)) {
sheet.showColumns(column);
} else {
sheet.hideColumns(column);
}
}
When this script is run, when the column "F" is showing, the column is hidden. When the column "F" is hiding, the column is shown.
References:
isColumnHiddenByUser(columnPosition)
showColumns(columnIndex)
hideColumns(columnIndex)

Google Sheets: Hiding Rows Based on Cell Value Yes/No

I am brand new to scripting so any help would be appreciated. I have searched and found some answers but still not finding a solution.
I have a simple spreadsheet called "Test", I have data validation for cell B3.
If the user inputs "No" from the drop-down box, I want the following 9 rows hidden.
if the user inputs "Yes" from the drop-down box, I want the following 9 rows to show.
Like I said there are some solutions that I have search but still not producing the result that I am trying to achieve.
enter image description here
Hide and Show Rows based upon Validation Selection
You need to add the sheet name and set up the validation for B3.
function onEdit(e) {
if(e.range.columnStart==2 && e.range.rowStart==3 && e.range.getSheet().getName()=='Sheet92') {//add your sheet name
if(e.value=='Yes') {
e.range.getSheet().showRows(4,9);
}
if(e.value=='No') {
e.range.getSheet().hideRows(4,9);
}
}
}

How to protect 1 column from editing?

I have big spread sheet with multiple tabs and i am sharing it with my coworkers.
In column D on each tab I have same set of formulas.
Unfortunately my collages keep deleting them so I'm desperate to find the way to block it from editing. G sheets protection doesn't work as it disables filter function which is much needed.
I'm trying to block 1 column from editing in every tab on the spreadsheet.
I tried to use this but I don't understand why it isn't working really
function onEdit() {}
if(entryRange.getColumn() == 2) {
e.range.setValue((e.oldvalue == undefined? "": e.oldvalue))
}

Trigger onEdit() to update the same cell value in Google Sheets

When I add a value to a cell it should get converted (in place) to a formatted value (a hyperlink in my case). This should not trigger another onEdit().
So say I enter "foo" the text automatically becomes http://bar.com/zee/foo
How do we do this using onEdit? Is there a better way to achieve the same result?
A script modifying cell content does not trigger the "edit" event. So you can achieve your goal without worrying about creating an infinite loop: modify the active cell using the new value in this cell.
function onEdit(e) {
if (e.value.oldValue === undefined) {
e.range.setValue('http://example.com/' + e.value);
}
}
The reason for conditional statement is to avoid putting content in the cell when the edit amounts to clearing out the cell. Such edits are detected as follows: when the cell content is deleted, e.value becomes an object in which the property oldValue represents the old value in the cell.

Increment Cell value by one by clicking

I want to write a formula in Google sheets so that I can attach to a button to make the value in a cell increase by 1 each time you click it.
Here is the best I could find on this topic.
I attempted to do this, but to no avail. I am using Windows 7 and Chrome.
First create a script to increment the cell value. For instance, to increment cell "A1" by one the following script would work:
function increment() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + 1);
}
Save this script and open your spreadsheet and follow these steps:
Go to "Insert" > "Drawing" and draw a button using shapes and text.
Position your button accordingly, and right-click on it to display an
arrow on the side of it.
In the drop-down menu, select "Assign Script." and type the name of
your function. (In this case "increment")
The first time when you click on it, it'll ask for permission but
should work subsequently.
Update 2020
Motivation:
I would like to provide a different approach based on Marlon's comment:
what if we want to create multiple buttons to different rows and each
button modifies its own row? Is there a way not create this manually?
Solution:
You can use onSelectionChange to capture single click events and mimic the behaviour of a button.
The following script will increment the value of a cell in column A upon clicking on a cell in column B of the same row.
It also creates a "button" text on the fly but this is optional and up to the user.
To use this solution, simply copy & paste it to the script editor and save the changes. After that, it will automatically be used upon single click events. In this solution I used Sheet1 as the sheet name.
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if (as.getName() == 'Sheet1' && col == 2){
const range = as.getRange(row,1);
range.setValue(range.getValue()+1);
e.range.setValue('Button');
}
}
Demonstration:
Restrictions:
While this approach works pretty well, there are two drawbacks:
There is a small delay between the clicks and the updates.
The trigger is activated when selecting a cell. If a cell is already selected, if you click it again the function won't trigger. You have to remove the current selection and then select it again.
Assign the following to a drawing inside your sheet and it will increase the value of the currently selected cell by 1:
function plusOne() {
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}