Increment Cell value by one by clicking - google-apps-script

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);
}

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)

How can I have the text I enter into each cell automatically appended to the same root URL without creating any new columns?

I am rephrasing my original question to make it more intelligible.
I put spaces between "https" below because StackOverflow would not allow me to enter the actual URL.
Of course I could enter the text "apple" in cell A1 and then add a link from A1 to foo.com/Apple so that I would end up with [Apple](h t t p s://foo.com/Apple) in A1. However, I want that done automatically.
In other words, I want to...
Go to a cell, and then
type "apple" on my keyboard, and then
press enter, and then
instead of merely seeing "apple" in plain text, I actually I want to see
"apple" hyperlinked to foo.com/Apple like this [Apple](h t t p s://foo.com/Apple).
To get what you want you can use Google Apps Script. Adding a trigger that fires every time column A is edited, getting the entered value and combining it with the =HYPERLINK(url, link_label) function, should get what you need.
Open the Google Apps Script editor, Tools>Script Editor
Add the following code
function onEdit(e) {
// Check the edit is in column A
if(e.range.getColumn()==1){
// Get the values from the onEdit
let cell = e.range.getA1Notation()
let value = e.value
let ss = e.source
// Adding as HYPERLINK Formulas
ss.getRange(cell)
.setFormula(`=HYPERLINK("https://example.com/${value}","${value}")`)
}
}
Documentation
HYPERLINK Function
onEdit(e) Trigger
SpreadSheetApp Overview
try:
=INDEX("foo.com/"&B2:B3)

Add Checkbox in Sheets based on Criteria

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

Google Apps Script - Going up from bottom of sheet to an available value

I'm trying to do a macro in google sheets where I select a point in column N (in this case N100) and then simulate control up button to move up to the next available value in column N. But when I do this, I see the cell that's activated is the top of the column (looks like control up is pressed twice).
The code I have is below, any ideas on why this seems to be the case?
function ClearFormq() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Team1");
sheet.getRange("N100").activate()
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
}
Thanks!

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.