Google Spreadsheet Custom Script? - google-apps-script

I need to change the background color of a certain range of cells based on a list of values (9 possible) in a single cell. I want the background change to happen across multiple different tabs.
Range of Cells: Tab 1(A1:L1, A3:L3, A33:L33) Tab 2(A1:X1, Column C) Tab 3(A1:M1, D5:G5, Column D, Column, G) There are many more, but I think if I see how it's done, I can play with the code enough to make it work!
Cell: Tab 2(B3)
Values: 'The Reach' = green, 'The North' = light grey, 'The Iron Islands' = dark grey, 'The Riverlands' = dark blue, 'The Vale' = light blue, 'The Crownlands' = dark red, 'The Westerlands' = red, 'The Stormlands' = gold, 'Dorne' = orange
To be honest, the colors don't matter. If you can make the code, I can just tweak the background colors with specific hex code. Also, if possible, could text color be changed as well?
I'm not sure how to begin this, as I have no expertise coding. Would any of you be able to help? Sorry if this is a lot work, I just have NO idea.
This is the sheet I'm working with. I basically just want to change the headers when the region changes.

If you just want to trigger colors changes, I would give the built-in conditional formatting a try first. If you need help formulating a statement after reviewing that, there's already numerous examples / threads in the official Docs Product Forum that use the "Custom formula is" box.
At this point, the only reason to get a script involved is to trigger some thing more advanced to happen when your cell value changes like sending an email or moving a row.

This is just a 'general guide' (ie I can't be bothered to set up and explain nine different colours in three different tabs and multiple ranges). First give B3 in Holdfast Information a name - say Region. Then apply a conditional formatting rule to Income Row 2 of say:
Custom formula is =row()=2 Format: Text Colour: Black, Range: 2:2.
(To allow the next rule to apply to A1:L3.) Then:
Custom formula is =indirect("Region")="The Westerlands" Format: Text Colour: of your choice, Background Colour: Red, Range: A1:L3.
As an aside, you should perhaps be applying a lookup rather than that horrible formula in B3.

Related

Sheets lookup on conditional highlight colour

Is it possible to check a cell's conditional highlight colour and act accordingly?
I have a list of Names where some people belong to the "Yellow" team and some the "Pink" team. The Yellow team get more points allocated depending on their level (Cell B3).
Referring to the screenshots attached, I can't work out how to direct the lookup to the appropriate column of points per level in 'Sheet2' relative to the chosen Name. Conditional highlighting (see screenshot) sets the cell colour according to the Name input into cell B2.
Cell B6 contains the following formula:
=vlookup(B3,Sheet2!A1:E11,match(B2,Sheet2!A1:E1,0),)
The formula worked when the reference table had Column B named as "Bob" but that is not what I want.
I want the B6 formula to realize which colour team the B2 Name is and pick from the appropriate data set in 'Sheet2'.
Any help appreciated if it is at all possible to lookup against cell highlight conditions.
Thanks!
There's no direct way of doing it. You might want to use the getBackground() function in Google apps script, but I don't think it's worth using scripts for this case. That would mean making the thing more complicated and less maintainable.
The easiest way you can achieve it is by using a helper cell.
You'll need the two teams listed in two columns. I'll assume to make things simple to have the two ranges E2:E30 for yellow names and F2:F30 for pink names
Add Color in cell A4 and the following formula in B4:
=IF(ISERROR(MATCH(B2,E2:E30,0)),IF(ISERROR(MATCH(B2,F2:F30,0)),"No color","Pink"),"Yellow")
You could then use the result of cell B4 for the VLOOKUP in cell B6.
The added advantage is that you wouldn't need that many conditional formatting rules: you can simply compare the color in B4 and assign a conditional formatting rule accordingly in B2. It's way more maintainable and less error-prone.
Using Named Ranges for conditional formatting (no script, no helper cell)
Since Francesco's excellent proposition on using formulas instead of a script is your choice, you could also try the following.
Turn your team ranges to Named Ranges (by going to Data>>Named Ranges from the top menu) and name them e.g. yellow and PINK
Apply conditional formatting to your data validated cells (B3:E3) by using these two formulas (under Custom formula is found at the end of Conditional formatting rules):
=REGEXMATCH(B3,""&JOIN("|",FILTER(INDIRECT("yellow"),INDIRECT("yellow")<>""))&"")
and
=REGEXMATCH(B3,""&JOIN("|",FILTER(INDIRECT("PINK"),INDIRECT("PINK")<>""))&"")
(Make sure you adjust your ranges according to your data)
Advantages:
It is your original cells that get color coded.
Named Ranges can be placed anywhere, on any tab and can grow to your needs.
You do not need the extra helper cell.
Make a note:
We must use INDIRECT when using named ranges with conditional formatting for the function to work.
We must also surround the named range with double quotes "". So, to refer to the named range yellow we use INDIRECT("yellow).

How to divide Hex Code colors to a minimal lost for the main colors family?

We are building a comparison website based on items scraped from the internet.
One of the main filters for the user's comparison engine is filtering by products by colors.
The issue is that we are scraping information from many different ecommerce and we get an endless list of colors with many strange names like CAPULET OLIVE or CHARCOAL RED.
So i was thinking if we can automatically get the hex color by comparing the a name-hex list maybe we can work with that.
Hence the hex is dividing the RGB to 16,777,216 colors(red 1-255 optionsGreen 1-255 optionsBlue 1-255 options). if there was a table somewhere that is dividing the options to the main 6-12 colors (ref, green, blue, yellw, etc.) that can work pretty well.
i.e.CAPULET OLIVE hes the hex value of #656344 which is R=101,G=99,B=68 in decimal.
if i had a table showing this range or strength of red and green vs blue is actually a form of green i can tag this model as green.
Basically, what i'm asking, is there a table that allocating all of the hex codes 6-12 main colors?
So i think i found a simple way to implement this using HSV color model.
Step 1-
Use a search website engine to find HSV/B code by name for sites like this:
https://rgb.to/color/17148/spearmint-green
http://www.perbang.dk/rgb/696B4D/
Step 2-
Use a predefined table to allocate color to his family, for example this one:
https://mehrarodgers.files.wordpress.com/2013/05/hue-scale.png
Though i found some missing color ranges there, so it can be improved.
This article is very useful in dividing the HSV by color wheel:
http://warrenmars.com/visual_art/theory/colour_wheel/evolution/evolution.htm

conditional formatting - hightlight a cell

Can anyone assist me regarding conditional formatting in google sheets? It's pretty simple, though I can't seem to get it to work, my goal is to highlight or color the cell green (AQ) if (AR) is Y and red if (AR) is N. The custom value I'm trying to use is =$AR="Y". Then I choose background color green; however, it's not working. Please assist me. Thank you very much...
Neither AQ nor AR is a valid cell reference. Use AR1 or whatever cell you are relying on. Google's documentation is pretty good on the subject: see here.
Relative Reference Example
The following example highlights each cell in the range AQ1:AQ11 green if "Y" or "y" is in the adjacent cell in column AR.
Absolute Reference Example
If I wanted every cell in the range AQ1:AQ11 to be colored green based only on the value of AR1, I would use =$AR$1="Y" as my formula instead.

Conditional formating based on adjacent cell using script

I have a spreadsheet that looks like (but much larger):
Both the student names and the house assignments are pulled from other sheets.
I am looking for a google script that would conditionally format a student name based on the house they are assigned to. The house assignment will always be listed in the cell to the right of the name.
I realize this can be done with conditional formating (For instance: Custom formula =B3:B4="6-Blue"), but it becomes an arduous process because I have to go into each vertical range individually to make a change if the house name changes. With a google script, I could find and replace all instances of a house name.
Something like Eric Koleda's answer may work, but I am unsure how to reference the ranges and add multiple values to search for.
You can still do this with conditional formatting!
You can set up conditional formatting on a per column basis, you don't have to change the formatting, only the name of the color in your house column.
Conditional formatting formula: =ISNUMBER(FIND("Red", $C4))
Conditional formatting range: B4:C
This will try and find the text Red within all cells in Column C starting at C4. If Red is found in a cell, FIND() will return the index of Red, if it doe snot find Red is returns an error. You can check this with ISNUMBER(), which returns true or false if the value is a number or not.
This will color the adjacent columns on that row within the range. ie. A range of B4:C will color the cells in column C and B, a range of just B4:B will only cause the cells in column B to be colored.
You only need to setup one conditional formatting per color, per set of columns that you have. After that, just changing the colors under your houses will change the formatting.
Here is an example sheet I made for you: Example Google Sheet
Picture of Sheet with formatting:

Sparklines stacked bar color options only allow two color variations?

I'm using the sparkline function in Google Spreadsheets. Here is an example of some data:
Feb-2015 MTD Actual MTD Budget Budget
Campaign 1 $41.32 $13.71 $96.00
I'd like to create a stacked bar chart sparkline as per the documentation here.
=sparkline(D9:F9,{
"charttype","bar";
"color1","blue";
"color2","yellow";
"color3","red"})
D9:F9 corresponds to $41.42: $96
Here is what the result looks like:
I had expected a segment to be red (color3). But the sparkline only seems to customize 2 colors. Can't see anything in the documentation about this. Is this a bug? Or am I misunderstanding something?
If it's the case that I can only edit 2 colors using this function, I don't suppose it's possible to edit the functionality using Google-Apps-Script?
There is a trick if you want to have 100% 3-shares bar. So, if you have 3 values that gives sum of 100 do as follows:
Remove 3rd value from the data cell.
Set "max","100" (or "max"/"100" for Europe).
Make a color background for sparkline cell.
This way the third value that fills to 100 is represented by cell background - it is a third sparkline color.
There is only color1 and color2 options listed for a bar graph. Sparklines are not expressly exposed to Apps Script, but can be written to a cell as a value. For example:
ss.appendRow(['==sparkline(D9:F9,{"charttype","bar";"color1","blue";"color2","yellow";})']);
From the docs:
https://support.google.com/docs/answer/3093289?hl=en
For bar charts:
"max" sets the maximum value along the horizontal axis.
"color1" sets the first color used for bars in the chart.
"color2" sets the second color used for bars in the chart.
"empty" sets how to treat empty cells. Possible corresponding values include: "zero" or "ignore".
"nan" sets how to treat cells with non-numeric data. Options are: "convert" and "ignore".
"rtl" determines whether or not the chart is rendered right to left. Options are true or false.