I'm trying to use a function to get the hexadecimal code of the cell background and concatenate it with the text contained in the same cell.
i.e. in sht1.cell(A1) I type "Hello" with red background, I would have in sht2.cell(A1) the following text: "[#FF0000]Hello"
I was trying this:
function getHexValue(range) {
return SpreadsheetApp.getActiveSheet().getRange(range).getBackground();
}
but it doesn't always work, especially if I share the file. I tried to setup some triggers using edit, change or open events but id keeps not updating
this is what i tried to do.
Any suggestion?
If I type: =gethexvalue("A5") I get the color code, if I type the formula to another cell or another sheet and I update the values or reload the sheet it doesn't work.
In your formula =gethexvalue("A5"), "A5" is text, not a dynamic range.
Read How to pass a range into a custom function in Google Spreadsheets? for a full discussion and 11 different answers. This is itself a possible duplicate of passing cell references to spreadsheet functions.
This =getHexValue(ADDRESS(ROW(A1), COLUMN(A1)))&A1 is dynamic. It will return:
"#ff0000Hello"
. If you want the hex in Uppercase, then use UPPER with getHexvalue.
Related
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)
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
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")
I'm new to the world of google sheets. I was able to program very simple routines in Excel. Sorry if I couldn't find the answer anywhere on this site.
My question is simple, has 2 parts:
I would like to prompt a question box on opening the google sheet, asking a numeric value, after answering this, storing the numeric value to a specific cell, e.g. B3.
I would like to have a button on multiple pages of the sheet, prompting the same question box to edit the value.
Background:
I'm a medical doctor, I've created a sheet with many medications for small children in emergency situations. I would like a ask the user the age (and maybe later the weight, it's now calculated) of the child, so the age and weight are correct on all pages and the user is not in a hurry to find the right cell. If the user would like to edit the age, he uses the button, available on all sheets.
First, with the following function you can ask the user for the age and set it to a specific cell in a specific sheet:
function promptUserAge()
{
// Prompt for the value
var age = SpreadsheetApp.getUi().prompt("Please enter the age.").getResponseText();
// Get the sheet that you want store the value in and set the value in the cell B3
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Patient Information").getRange("B3").setValue( age );
}
Now, since you used the phrase
so the age and weight are correct on all pages
I am not sure if you are going to set the value in multiple sheets, and are you going to do that with Sheets functions or app script. That is why I am going to include the method to set the value in all sheets, in specific cell:
function promptUserAge()
{
// Prompt for the value
var age = SpreadsheetApp.getUi().prompt("Please enter the age.").getResponseText();
//Get all pages in spreadsheet and iterate through
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i = 0; i < sheets.length; i++ )
{
//Set the value in the cell B3 of the page
sheets[i].getRange("B3").setValue( age );
}
}
To get the prompt to pop-up when user opens the Spreadsheet you use call it in the onOpen function:
function onOpen(e)
{
promptUserAge();
}
And now, the part where you add the button to edit the value.
You could add a custom menu that would appear in the Spreadsheet's toolbar with a custom menu like this :
function createMenu()
{
SpreadsheetApp.getUi().createMenu("Fill information")
.addItem("Age", "promptUserAge")
.addToUi();
}
This is a good way if your users know what to look for, meaning that you have told them the custom menu exists. From my experience, some people have trouble finding the menu even if you tell them it is there, so since your background suggests you want to create an easy and fast way I would use the second method:
You could insert an image to the Spreadsheet by navigating from it's toolbar: Insert -> Image. Insert an Image that says 'Push here to edit information' in a very clear way and right click the image. You get the borders to edit the image size and in it's upper-right corner appears three dots. You click the dots and then "Assign script...". To the prompt, insert the function name without the parenthesis. In this example you would insert:
promptUserAge
Now the image works as a button to call the function.
I hope this helped.
I was sure to find that on StackOverflow but finally no. Maybe it's not possible?
I'd like to get the cell where my function is running.
If for example I have the function that is repeated in column D for line 1 to 10, I'd like that function to know that the current "processing" is D4 (for example).
I want this in order to be able to change the color of the cell based on the computation the function does. This, of course, changes based on the data input.
Is this possible? How? I wasn't able to find it.
Here's an applicable example code :
function ALERT_IF_NEGATIVE(input) {
if (input < 0) {
// Set that cell's color to red
}
}
Thank you.
Conditional formatting will solve this.
https://support.google.com/docs/answer/78413?hl=en
This is an example I have from a spreadsheet I use:
In my code it adds the row values:
ss.appendRow([account,sAccount.name,sAccount.id,response.access_token,d,expTime,'=if(E'+lastRow+'<NOW(), "EXPIRED","VALID")',]);`
In this case the formula in column 'G' looks at a timestamp in Column 'E' and sets the value in 'G'. The conditional format looks at the values in 'G' and changes the cells background color based on the value.
The formatting is recalculated when there is a change to the sheet, or can be set-up to recalculate on a timer in File->Spreadsheet settings.