Highlight Row if Value in Column is above 0 - Google Spreadsheet - google-sheets

I am trying to get a Google spreadsheet to automatically highlight all rows in a spreadsheet where the value in a particular column is above 0. I have looked for a few solutions but haven't got it to work.
I have got various metrics in the columns, so say I want to highlight all rows, in which column "I" has a value of more than zero.
Can someone help me with this?

On the conditional formatting page choose "Custom formula is" from the list of options available and then in the text field type:
=$I:$I>0
Select your formatting options and then in the range field type the range. For example:
A2:Z100

Because only to be applied to a single column there is a simpler version (that I have complicated so that text is not highlighted):
Clear formatting, select ColumnI and Format, Conditional formatting..., Format cells if... Custom formula is and:
=and(isnumber(I1),I1>0)
with fill of choice and Done.
If to format not just the relevant cell but the entire row then change the Apply to Range (say to A1:Z1000) and add anchors ($s) as below:
=and(isnumber($I1),$I1>0)

The only solution I'm aware of would be to write a script.
Link: Google Apps Scripts
The following is not pretty, but it works:
function myFunction() {
var I_INDEX = 1;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Highlight rows");
var dataRange = sheet.getDataRange();
var dataValues = dataRange.getValues();
for (var i=1; i<=dataRange.getNumRows(); i++) {
var row = sheet.getRange(i, 1, 1, 2);
if (dataValues[i-1][I_INDEX] > 0) {
row.setBackground("red");
}
}
}
See example, use "Tools" --> "Script Editor..." to view/run the script.

Related

Google sheets conditional formatting to colour cell if the same cell in another sheet is not empty

I have 2 sheets A and B. I want sheet A to colour cells if the exact same cell in sheet B is not empty. For example if cell A1 in sheet B has a string, then cell A1 in sheet A will be coloured.
Let's say I want to do this to compare a large range, how should I go about doing this?
Example: https://docs.google.com/spreadsheets/d/1P3Ob_mclpXWmILfKwD4R6JN2wAYPUcNZlmtF9LxilV0/edit?usp=sharing
Cells A2:C2, D5 and K2 in sheet B are not empty. So the corresponding cells in sheet A will be coloured red.
You can use the following formula as a conditional formatting rule:
=NOT(ISBLANK(INDIRECT("B!"&address(row(),column()))))
(the formula in A!A2 is there to just show you what happens in sheet B)
Explanation:
Not familiar how this could be done with a custom conditional formatting formula so wait for other guys to help you out with that.
However, I can offer you a Google Apps Script solution.
The following script will check for the data range in sheet B and it will construct a color array. An element of this array will have the white hex color #ffffff" if the value in the values array is empty, or a red color #ff0000 if the value is not blank.
Then it will color sheet A based on this color array in one go. The script is relatively fast even for large ranges.
Solution / Workaround:
Don't forget to adjust "sheet A" and "sheet B" to the name of your sheets.
function colorCells(){
const ss = SpreadsheetApp.getActive();
const sheetToColor = ss.getSheetByName("sheet A");
const sheetToCheck = ss.getSheetByName("sheet B");
const bValues = sheetToCheck.getDataRange().getValues();
const aColors=bValues.map(r=>r.map(v=>v==''?"#ffffff":"#ff0000"));
sheetToColor.getRange(1,1,sheetToCheck.getLastRow(),sheetToCheck.getLastColumn()).
setBackgrounds(aColors);
}
How to use that:
Click on Tools => Scipt editor, copy, paste & save the aforementioned code and then execute it by clicking on the play button.
If you want this to work "automatically" then you can set up a time trigger to execute this function for you every n minutes.
Bonus code:
You can do the same operation but on a more user-interactive way. Again, copy, paste & save the following script to the script editor but this time don't execute it as it can work by itself.
The following script will change the color of a cell in sheet A upon user edits on the same cell in sheet B:
function onEdit(e) {
// adjust this to your needs:
const sheetToColorName = "sheet A";
const sheetToCheckName = "sheet B";
const rng = e.range;
const cell = rng.getA1Notation();
if (rng.getSheet().getName()==sheetToCheckName){
const sheetToColor = e.source.getSheetByName(sheetToColorName);
if(rng.getValue()!=''){
sheetToColor.getRange(cell).setBackground("#ff0000"); //red
}
else {
sheetToColor.getRange(cell).setBackground("#ffffff"); //white
}
}
}

Google Sheet Chart's Data Range (in Chart Editor) formula from cell value

I have a chart in Google Sheets, the "Data Range" is as follows...
MIR!J1:K4547,DIR!K1:K4547
What I am trying to do is reference a cell value for sheet "MIR" and "DIR" values.
Let's say I edit cell A1 to ZIR...
then the data range changes to
ZIR!J1:K4547,DIR!K1:K4547
I searched intensively in Google but maybe I am not using right keywords, will appriciate some feedback on this...
I'm not sure what type of chart you are trying to get, but you could create a dynamic range, however, this would only work for a line graph.
Choose any cell and enter this formula: =A1&"!K1:J4547"
Go to Data > Named Ranges and make the cell with the formula the named range. (This step is optional, but will make things easier)
You can now use this range for other formulas, for example, if you want a line graph: =SPARKLINE(INDIRECT(dynamicRange))
Reference: https://www.benlcollins.com/formula-examples/dynamic-named-ranges/
While writing this, I also realized a different work around, but using the same idea. Using the QUERY function, you can grab cell values, without needing to change the chart range. For example, placing the formula =QUERY(INDIRECT(A3)) in a cell, will grab all the values from DIR!K1:J4547. Changing the value of A1 in the sheet will change the sheet that it grabs from. From there, set the chart range to wherever you place the QUERY formula.
You could also just do =QUERY(INDIRECT(A3)), the named range is not necessary.
Hope this was helpful!
What I had done is the following
I created an empty spreadheet and used 3 columns on that sheet to construc the chart. Then I have created a script (I know very dirty code but it works).
The script replaces the data on the last sheet I created as follows:
function replace() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHART'); // or whatever is the name of the sheet
var range = sheet.getRange(1,1);
var range2 = sheet.getRange(1,2);
var data = range.getValue();
var data2 = range2.getValue();
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(data);
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ChartData");
var sheet2From = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(data2);
var sheet2To = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ChartData");
// Copy from 17th row, 4th column, all rows for one column
var valuesToCopy = sheetFrom.getRange(1, 10, sheetFrom.getLastRow(), 1).getValues();
var valuesToCopy2 = sheetFrom.getRange(1, 11, sheetFrom.getLastRow(), 1).getValues();
var valuesToCopy3 = sheet2From.getRange(1, 11, sheet2From.getLastRow(), 1).getValues();
//Paste to another sheet from first cell onwards
sheetTo.clearContents();
sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy.length,1).setValues(valuesToCopy);
sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy2.length,1).setValues(valuesToCopy2);
sheetTo.getRange(1,sheet2To.getLastColumn()+1,valuesToCopy3.length,1).setValues(valuesToCopy3);}
For changing the Data Range I pulled data from the cells.

How to remove conditional formatting in sheets using script

I'm currently using a modified script that allows me to copy an entire line from sheet#1, create a new line at the top of sheet #2, paste the copied line to that sheet, and delete the old line from sheet#1.
This is done often throughout the day by many users. The function is onEdit.
This is the script :
function onEdit(e) {
var ss = e.source;
var activatedSheetName = ss.getActiveSheet().getName();
var activatedCell = ss.getActiveSelection();
var activatedCellRow = activatedCell.getRow();
var activatedCellColumn = activatedCell.getColumn();
var activatedCellValue = activatedCell.getValue();
var URGENCE = ss.getSheetByName("List"); // source sheet
var COMPLET = ss.getSheetByName("Comp"); // target sheet
// if the value in column K is "x", move the row to target sheet
if (activatedSheetName == URGENCE.getName() && activatedCellColumn == 11 && activatedCellValue == "x")
{
COMPLET.insertRows(2,1);// insert a new row at the second row of the target sheet
var rangeToMove = URGENCE.getRange(/*startRow*/ activatedCellRow, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ URGENCE.getMaxColumns());
rangeToMove.moveTo(COMPLET.getRange("A2"));
URGENCE.deleteRows(activatedCellRow,1); // delete row from source sheet
}
}
Recently this has been crashing my sheet. Everytime someone puts an "x" in Column K, the sheet will stall and most of the time, it will crash and chrome will kill the page.
I could be wrong, but the problem I'm guessing is that most of the rows in sheet#1 have conditional formatting. When the line is copied, it also copies the conditional formatting. This results in my sheet#2 having hundreds of repeating conditional formatting: this sheet is VERY slow to open. IT could also be because this document is shared with about 30 people who view it and edit it very often: perhaps onEdit isn't the right function here?
Is there a simple script I could add to my function which would strip the conditional formatting on the pasted line? I don't need the conditional formatting in my sheet#2 and for some odd reason I can't find an answer to this anywhere.
Found this function from here.
clearFormats()
Clears the sheet of formatting, while preserving contents. Formatting
refers to how data is formatted as allowed by choices under the
"Format" menu (ex: bold, italics, conditional formatting) and not
width or height of cells.
Sample code:
function testKillFormatting (nameOfSheet) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(nameOfSheet);
sheet.clearFormats();
}

Trying to highlight passed days in google spreadsheets

i have a google spreadsheet document with dates on the left rows:
1.1.2016
2.1.2016
.....
i want to highlight days that have already passed, and i have got this code:
function myFunction()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetSelect = ss.getSheetByName("test");
var todayis = getCell(1,12);
{
for(var i = 0; i<sheetSelect.getMaxRows();i++)
{
if (getCell(i,1) == todayis)
{
var changeRange = sheetSelect.getRange(i,1);
changeRange.setBackgroundRGB(111, 111, 222);
}
}
}
}
This is not working, any suggestions ?
There's a less-complicated way to do it:
Right click on the cells you want to conditionally highlight.
Click "Conditional Formatting" from the right-click menu.
Under "Format cells if...", select "Date is before"
In the next drop-down select "today"
Select the background color you want.
Click "Done"
You can do this without a google apps script also if your dates are all formatted into text as it shows in your example you can use the conditional formatting with a custom formula - highlight the column you want, choose conditional formatting and enter in this formula:
=arrayformula(if(DATEVALUE(substitute(A1:A,".","/"))<today(),true,false))

Can you count links within a range?

I have many Google sheets that require manually hyperlinking (to specific unique documents) after other users have inserted values, but it is time-consuming to search through many sheets for cells yet to be linked. A basic solution would be to use COUNTA to get the number of cells within a range containing text, and a second function to count the number of links, showing the difference. I've tried many permutations of COUNTA and COUNTIF using wildcards but nothing seems to be able to recognise formulas. Is there a function within Google Sheets of getting the number of hyperlinked cells within a range?
If you are referring to urls in cells (like: www.google.com), you can try:
=SUM(ArrayFormula(N(ISURL(A3:A))))
Change range to suit.
This will not work if you are using =HYPERLINK() function.
EDIT: If you want to count the cells with text, but exclude the =Hyperlink() formulas (AND empty cells) you can try this custom function:
function countF(range) {
var r = SpreadsheetApp.getActive().getRange(range),
formulas = r.getFormulas(),
count = 0;
r.getValues()
.forEach(function (r, i) {
r.forEach(function (c, j) {
if (c && formulas[i][j].substring(1, 10) !== "HYPERLINK") count += 1;
})
})
return count;
}
This custom function can be used in your spreadsheet by entering
=COUNTCELLS("Sheet1!A1:A2")
If you want to exclude all formulas from your count, change the if-statement to:
if (c && !formulas[i][j]) count +=1
Make sure you always mention the sheet name.
EDIT2: to count the number of formulas, you can try something like this:
function countFormulas(range) {
var count = 0;
SpreadsheetApp.getActive()
.getRange(range).getFormulas()
.forEach(function(r) {
r.forEach(function(c) {
if (c.charAt(0) == '=') count += 1;
})
})
return count;
}
I suggest the following. The formula assumes the cells to check for Hyperlinks are in column A with header (adjust formula to your need). Select A2. Right click and select Conditional Formatting. Enter the following:
Apply to range
A2:A
Format cells if
Choose 'Custom formula is' from dropdown list and enter:
=AND(NOT(ISFORMULA(A2)),NOT(ISBLANK(A2)))
Choose the formatting style you want.
This will format and non blank cell not containing a formula. (No Hyperlink)
Two other options to get just a count:
In a column, lets say B enter the following formula(Note that ISFORMULA does not work in array formulas like ISBLANK does.) Copy the formula down. It will return FALSE if there is a Hyperlink and TRUE if there is not.
=NOT(ISFORMULA(A2))
Then to count use:
=countifs(B2:B,"TRUE",B2:B,"<>''")
The other option is script:
function noFormula() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s = ss.getSheets()[0];// [0] is Sheet1
var lr=s.getLastRow()
var rng =s.getRange(2, 1, lr-1, 1) //get column A data. Assumes header row.
var data=rng.getFormulas()
count=0
for(i=0;i<data.length;i++){
var hasFormula=data[i][0]
var eqSign=hasFormula.substring(0,1) //looks for first character "=". If there are formulas other than Hyperlinks change to substring(0,2) to look for "=H".
if(eqSign !="="){ //Not equal to"="
count = count+1
}}
var c=s.getRange(1,2).setValue(count)//Sheet1 B1
}

Resources