Get Spreadsheet cell calling a custom function - google-apps-script

I am trying to figure out how to get the A1 Notation of a Cell calling a Custom Function.
I found this but this is for active cell. https://webapps.stackexchange.com/questions/54414/in-google-script-get-the-spreadsheet-cell-calling-a-custom-function
Essentially if I want Cell A5 to Call =TEST(). I want the function to return the text value A5.
I want to use this as a cache identifier for an API Call.

There are subtle differences in different range returning method names- active,current,selection:
The term "active range" refers to the range that a user has selected in the active sheet, but in a custom function it refers to the cell being "active"ly recalculated.
The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border.
A selection is the set of cells the user has highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the current cell, where the user's current focus is.
The first two are still range objects,while the latter is not. To reiterate, getActiveRange()
in a custom function it refers to the cell being "active"ly recalculated.
I want the function to return the text value A5.
Without Custom Functions,We can use:
=ADDRESS(ROW(),COLUMN())
With Custom function,We can use:
function test() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}

Related

Apps Script: do not recalculate if the cell on the right (next column) is marked as NO - How to get the value of the cell on the right of a give param [duplicate]

I am trying to figure out how to get the A1 Notation of a Cell calling a Custom Function.
I found this but this is for active cell. https://webapps.stackexchange.com/questions/54414/in-google-script-get-the-spreadsheet-cell-calling-a-custom-function
Essentially if I want Cell A5 to Call =TEST(). I want the function to return the text value A5.
I want to use this as a cache identifier for an API Call.
There are subtle differences in different range returning method names- active,current,selection:
The term "active range" refers to the range that a user has selected in the active sheet, but in a custom function it refers to the cell being "active"ly recalculated.
The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border.
A selection is the set of cells the user has highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the current cell, where the user's current focus is.
The first two are still range objects,while the latter is not. To reiterate, getActiveRange()
in a custom function it refers to the cell being "active"ly recalculated.
I want the function to return the text value A5.
Without Custom Functions,We can use:
=ADDRESS(ROW(),COLUMN())
With Custom function,We can use:
function test() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}

What's the difference between getActiveCell and getCurrentCell in Google Apps Script?

I've read the definitions a few times over and I still don't understand the difference.
getActiveCell: Range
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getActiveCell()
Summary: Returns the active cell in this sheet.
Description: Returns the active cell in this sheet.
getCurrentCell: Range
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getCurrentCell()
Summary: Returns the current cell in the active sheet or null if there is no current cell.
Description: Returns the current cell in the active sheet or null if there is no current cell. The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border. There is never more than one current cell. When a user selects one or more cell ranges, one of the cells in the selection is the current cell.
There's this note in getActiveCell's documentation: (copy-pasted including grammatical errors)
It's preferrable to use getCurrentCell(), which the returns the current highlighted cell.
Which doesn't make it any clearer
Answer:
getCurrentCell() returns the highlighted cell as seen by the dark border in the Sheets UI, whereas getActiveCell() returns the top-left cell of the selected range.
Example:
Say you have a range highlighted in a Sheet: B2:E10, for example. In the case where the you clicked on B2 and dragged down to E10:
Running the following code in the script editor:
function getCells() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
console.log(sheet.getActiveCell().getA1Notation())
console.log(sheet.getCurrentCell().getA1Notation())
}
You will see in the logs that both getActiveCell() and getCurrentCell() return B2.
However in the opposite scenario, where you clicked on E10 and dragged up to B2:
After running the same code, you will see that getActiveCell() still returns B2, but now getCurrentCell() returns E10.

Google Sheets Script – Function that outputs the background color of a cell

I would like to write my own function in Google Script that I can use in Google Sheets to get the background color of a cell. Let's call the function GETBACKGROUNDCOLOR.
I want to be able to pass a cell reference (in A1 notation) as parameter, e.g. if I call =GETBACKGROUNDCOLOR(B2), it should return the background color of cell B2. If I call the function without parameter, I want it to return the background color of the same cell in which it is called, e.g. calling =GETBACKGROUNDCOLOR() in C3 should return the background color of C3.
I have tried this:
function GETBACKGROUNDCOLOR(cell){
return SpreadsheetApp.getActiveSheet().getRange(cell).getBackground();
}
But when I call GETBACKGROUNDCOLOR(A1), the parameter is not the cell reference of A1, but the cell content of A1.
How can I solve this?
Bulletproof script below:
1. Paste to AppsScript:
function bgHex(cellAddress) {
var mycell = SpreadsheetApp.getActiveSheet().getRange(cellAddress);
var bghex = mycell.getBackground();
return bghex;
}
2. Back in sheets call it with =bgHex(CELL("address"; B4)) or =bgHex(CELL("address", B4)), depending on your locale. Look closely and note the difference: delimiters. Some countries like Poland use comma as decimal delimiter and we use semicolons for separating arguments. Others use dots as delimieters so they can use comma for separating args.
With that function you'll get the hex code for specified cell's background. Call any cell, no range errors, no mismatched data formats.
Now you can play with getting other stuff out of these cells, for example check cell values. Here's a test sheet so You can see how it works.
You will find a second function there, for extracting cell's value – the principle is the same.
function cellValue(cellAddress) {
var mycell = SpreadsheetApp.getActiveSheet().getRange(cellAddress);
var value = mycell.getValue();
return value;
}
Now go to https://developers.google.com/apps-script/reference/spreadsheet/range and play with other methods.
You could try something like
=GETBACKGROUNDCOLOR("A"&row(A1))
or
=GETBACKGROUNDCOLOR(cell("address", A1))
and see if that works?
(Note: depening on your locale you may have to use a semi-colon instead of a comma).
You can use =address to resolve name
=GETBACKGROUNDCOLOR(address(row(A1),column(A1)))
for relative cells or
=GETBACKGROUNDCOLOR(address(row(),column()))
for cell with formula
However, the big problem would be that then you'll change cell backround color, formula won't recalculate(function re-eval won't trigger), so it might not be exactly a way.
Example sheet:
https://docs.google.com/spreadsheets/d/1lfFRLVqhns0AJCbZd6ikgcDtvktcgpNWfNomMASWemE/edit#gid=0

code to find cell address for script - google sheets

I am using Google Sheets.
I have a spreadsheet with a column 'B' containing data which is conditionally formatted with background colors.
I need to place the hex code corresponding to the color in each cell of 'B' into the neighboring cell in 'C'.
I have the following code, which works:
function GetBackColorCode(cell)
{
return (SpreadsheetApp.getActiveSheet().getRange(cell).getBackground());
}
As you can see, however, this code requires manually inserting the proper column and row number in each and every cell. Thus, if I want the hex. code for the color in B14, I insert the following in C14:
=GetBackColorCode("b14")
This method would require me to manually imput about three hundred cells. Is there no way to automatically pick up the cell reference, with, for example, a 'this.someFunction()' call? Such that I can paste the call '=GetBackColorCode(this.someFunction())' into all the cells in column 'C' and each will automatically furnish the necessary reference to the cell to its immediate left?
Change your script to
function getHexCodes(range) {
return SpreadsheetApp.getActiveSheet()
.getRange(range).getBackgrounds();
}
then enter in C2 (or whatever your 'start row' will be)
=getHexCodes("B2:B")

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.