using formulas and hyperlink in the same cell - google-apps-script

So the main idea is I want to put a Hyperlink on a cell that contains a formula, But google sheet don't like that.
I had 2 ideas.
1- Put the formula in cell A1 and the output of that formula is dumped into cell A2 which is gonna be Hyperlinked.
(I tried to use FILTER to pull data into the Hyperlinked cell but it would give me a #REF! error:Array result was not expanded because it would overwrite data in A2.)
2- Use some sort of apps-script of which I have very little understanding of, so I didn't find any scripts relating to my question.

I didn't understand the reason why you can't put a Hyperlink AND a formula in the same cell, But as far as I understood:
Hyperlink is a formula it self → =HYPERLINK(url, [link_label])
So instead of Hyperlinking our cell using the UI, we can use the formula form to Hyperlink the cell and use our formula in the place of [link_label] which is gonna be the name of our cell:
=HYPERLINK( url , [link_label])
=HYPERLINK("google.com", SUM(B2:B5 ))

Related

Automatic Replace "0" with blank cell

I have a sheet called "Test". Column B shows dynamic API calls, then I use a macro to copy all cells from Column B to Column C every 1 hour in order to store those prices, however sometimes API call fails, so it shows 0. I want to mass replace all cells containing "0" (match exact case) from all columns of sheet "Test" without changing Column B (which is the column for the API calls so we don't want to change its formula).
How can I do that?
Thank you!
Google sheets doesn't have this function yet but this could do the trick:
Select all cells (Ctrl + A) click Format > Conditional Formatting
Use the dropdown to select Equal To and type 0
Tick the text tickbox and change the text color to white, this will
effectively hide all your zero values.
You can define a format as
0;-0;

Google Sheet custom function - update cell references when the sheet is modified/the formula is copied to new cells

I have a custom formula, code below, to extract the URL of cells containing hyperlinks. I need to do this for a whole bunch of cells, so I need to ensure the reference cell is correct.
However, I have to explicitly type out and manually modify the cell reference every time I use the corner drag function to expand the formula to other cells, as well as every time a row is added or deleted, etc - the reference doesn't smartly update the way it does for built-in functions.
Is there a way to update the script or otherwise induce Sheets to smartly update the cell reference?
Custom function script:
function GETLINK(input){
return SpreadsheetApp.getActiveSheet().getRange(input).getRichTextValue().getLinkUrl();
}
Starting sheet example:
A
B
C
Index
Cells with hyperlinks
Formula
1
Link Text 1
=GETLINK("C1")
2
Link Text 2
If I click and drag the corner of C2, I wanto see this output:
A
B
C
Index
Cells with hyperlinks
Formula
1
Link Text 1
=GETLINK("C1")
2
Link Text 2
=GETLINK("C2")
But instead I get this:
A
B
C
Index
Cells with hyperlinks
Formula
1
Link Text 1
=GETLINK("C1")
2
Link Text 2
=GETLINK("C1")
^ the same kind of non-updating happens if I happen to insert some rows/cells - the references just stay absolute and don't intelligently update.
Thank you!
Use this instead:
=GETLINK(CELL("address", C1))
Then add this function in your script to remove the $ returned by the CELL() function:
function GETLINK(input){
var rangeName = input.replace(/[^a-zA-Z0-9 ]/g, "");
return SpreadsheetApp.getActiveSheet().getRange(rangeName).getRichTextValue().getLinkUrl();
}
References:
Regex
CELL(info_type, reference)

Set default value after form submit in google sheets

I want to set a cell to a default value as soon as a form is submitted to a spreadsheet (I want to add a value after the last value from that the spreadsheet gives me).
Since upon a form submit it erases all the values in the row, I can't have a formula in this cell, I think I'd have to do it with AppScripts.
I tried doing it with onFormSubmit but I did not get very far with it. Is there a way to do this?
If the formula is going to be the same for each row, you could add an ARRAYFORMULA version of your formula in the second row of your Responses sheet. By putting your formula in the second row you can still add a header to that column.
For example, if you wanted to get the first three characters of whatever is in Column B, in the second row of the column you want your formula in (in your Form Responses sheet), you could use:
=ARRAYFORMULA(IF(B2:B="","",LEFT(B2:B,3)))
If it's just a default value you want in that column, then use something like this:
=ARRAYFORMULA(IF(B2:B="","",your_default_value))
Good luck!

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")

Function not triggering

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.