Simple conditional formatting in Google Sheets Script Editor - google-apps-script

I am trying to create some basic conditional formatting rules with Scripts instead of the conditional formatting editor in sheets, because when a new row is added or removed, it breaks the rules already set. For example, when a row is deleted, the condition may be for the range A:C but then it adds "A1:C5,A6:C898". This causes some rows to be skipped by the rules so I am hoping a script will solve this.
So I want to simply change the cell background to green if the cell text is exactly "Y". I want to change it to red if text is exactly "N". I have other rules that I want to use but I am having trouble with the basics on this.
What I have so far in my script:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PRECLOSING");
var range = sheet.getRange("E:E,I:J,M:M,P:P,S:V,AB:AC,AF:AF");
range.activate();
var values = rangeY.getValues();
//for each row that data is present
for(var i = 0; i < values.length; i++) {
var cell = sheet.getRange(i + 1, 2);
if ( values == "X" )
{
cell.setBackground('black');
return;
} else {
cell.setBackground('white');
}
}
}

I made some modifications to your code.
The function getRange i don't think it can accept the ranges in that way, instead i think it can get just ranges that are continue (eg. A1:E10)
For that I created an array with the ranges you need and then I loop through them.
The function "getValues" returns a two dimensional array so you will need a second loop to get the actual value from the cell.
Then as you already have the range, you need to get the cell inside that range and not a new complete range.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("PRECLOSING");;
var columns = ['E:E','I:J','M:M','P:P','S:V','AB:AC','AF:AF'];
for(var col in columns){ //loop to iterate the desired ranges
var range = sheet.getRange(columns[col]);
range.activate();
var values = range.getValues();
//for each row that data is present
for(var i = 0; i < values.length; i++) { //loop to iterate the rows
for( var j = 0; j< values[i].length ; j++){ //loop to iterate the columns
var cell = range.getCell(i+1, j+1);
if ( values[i][j] == "X" )
{
cell.setBackground('black');
}
}
}
}
}
Hope this helps.

Related

Google Script - Hide column if a cell within the column contains a specific value

I am trying to hide all columns in Google Sheets if a cell within that column contains the letter "S", I am checking row 6 which has the initials of each day of the week and want the ability to show and hide the weekends
Columns A6:G6 have M,T,W,T,F,S,S
Reason for the 9999 is due to this sheet containing multiple weeks and I am trying to look through all of them.
function Hide() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange();
var lastCol = data.getLastColumn()+1;
for(var i = 1; i < lastCol - 9999; i++) {
if(data.getCell(6, 9999 + i).getValue() === "S") {
sheet.hideColumns(9999 + i);
}
}
};
This is what I have so far but it doesn't seem to do anything, can anyone help me understand what I am missing please as I have been looking around and cannot find anything that has helped me with the issue?
Thank you!!
Try this. Google Apps scripts is basically Javascript. This is how I would loop through the columns since the used area of the sheet is dynamically changed, then it will stop at the last used column.
function Hide() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get the entire 6th Row
var range = sheet.getRange("6:6");
// get number of columns to loop through
var num_cols = range.getNumColumns();
// loop through columns and check value one by one
// if value is equal to "S", then hide the column
for (var i = 1; i <= num_cols; i++) {
var value = sheet.getRange(6,i).getValue();
if (value == "S") {
sheet.hideColumns(i);
};
}
}

Cant figure out IF(number) in Google Sheets macro to hide rows with a zero value

I'm brand new at this, so I may be way off base here. this is my code:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("To Order");
var maxRows = sheet.getMaxRows();
//show all the rows
sheet.showRows(1, maxRows);
//get data from clumn B
var data = sheet.getRange('B:B').getValues();
//iterate over all rows
for(var i=0; i< data.length; i++){
//compare first character, if asterisk, then hide row
if(Number == "0"){
sheet.hideRows(i+1);
}
}
}
My trouble seems to be that the if(number) isn't only selecting cells with 0 to hide. please help!
The code in the question compares an undefined variable called Number with "0" using the abstract equality comparator. This comparison always will return false.
Instead of Number, use data[i][0].

Google Script App change color cells when a cell have a specific value

I've been trying to look for what I need to do but I couldn't figure it out how to do this from the previous posts I found.
I have this Google Spreadsheet here
Want I' love to do is to analize the data in the sheet called "actual" and set the background color of the cells when it meets a condition. So basically, I'm looking for a script to do this:
get the data from the sheet called 'actual'
if a cell is error (equals to "#N/A") then set the color font to white
if a cell equals to "wnd" then set the background color to "red"
if the cell equals to "otc", then set the background color to "green"
etc..
It's going to have around 50 conditions and that is why I would love to do this with code instead of regular Conditional Formatting.
Thank you in advance.
Change your code to minimize the use of get calls. Those are slow calls. So you want to take one big one. Then work with all the data. Here's some example code:
Note This function checks for values of 3 and sets them red background with white text. The current range its working with is defined in var range. Set that up yourself to whatever you need.
function setCellColors() {
//Get the sheet you want to work with.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range = sheet.getRange("A1:E17");
var rangevalues = range.getValues();
//Loops through range results
for (var i in rangevalues) {
for (var j in rangevalues) {
//Get the x,y location of the current cell.
var x = parseInt(j, 10) + 1;
var y = parseInt(i, 10) + 1;
//Set the rules logic
if (rangevalues[i][j] == 3) {
//Set the cell background
sheet.getRange(y,x).setBackground("red");
sheet.getRange(y,x).setFontColor("white");
}
}
}
}
Hope this helps.
function setCellColors() {
//Get the sheet you want to work with.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
// determine how how big your data range will be
var lastColumn = sheet.getLastColumn() + 1
var lastRow = sheet.getLastRow() + 1
// iterate through the data.
for (var column = 1; column < lastColumn; column++) {
for (var row = 1; row < lastRow; row++) {
var cellget = sheet.getRange(row, column).getValue();
var cellset = sheet.getRange(row, column);
//Set the rules logic
if (cellget === "#N/A") {
//Set the cell background
cellset.setBackground("red");
cellset.setFontColor("white");
}
}
}
}

Implement vlookup between two ranges in Google Apps Script

I'm looking to find a way to write a script that behaves similarly to a vLookup in google sheets.
Here is a link to a simplified example document.
What I am trying to do is use "Sheet1" as a form of sorts. When I enter information in "Sheet1" column B I want to be able to hit a custom menu button and have the information in "Sheet1" column B automatically populate correspondingly based of the values in column A into the first empty column on "Sheet 2".
I can write the script to create the custom menu and execute the function but I'm unsure of how to write the function itself.
Here is how such a function can look like. It gets pointers to each sheet, then the appropriate range from each: columns A and B, ignoring empty rows at the bottom. Then gets the values and begins comparing them: when columns A match, column B is assigned to. The final line puts the modified array values back into Sheet2.
function vl() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName('Sheet2');
var range1 = sheet1.getRange(1, 1, sheet1.getLastRow(), 2);
var range2 = sheet2.getRange(1, 1, sheet2.getLastRow(), 2);
var values1 = range1.getValues();
var values2 = range2.getValues();
for (var i = 0; i < values1.length; i++) {
for (var j = 0; j < values2.length; j++) {
if (values1[i][0] === values2[i][0]) {
values2[i][1] = values1[i][0];
}
}
}
range2.setValues(values2);
}

get.Range with EQUIV Function ? (Or Vlookup or Match?)

I'm new to Google Apps script, so sorry if this is a too easy question.
I've searched in the existing questions but could not make this work.
I have this function :
SpreadsheetApp.getActiveSheet().getRange(5,1).activate(); that works fine.
But instead of (5,1) I would like to have a dynamic value for the row number.
I would like to return the row number of a specific text that I chose in a list in A1.
Here is what I do :
function GoTo () {
var sheet = SpreadsheetApp.getActiveSheet();
var c = "EQUIV($A$1,A2:A100,0)+1";
{ SpreadsheetApp.getActiveSheet().getRange(c,1).activate(); }
}
If I put = EQUIV($A$1,A2:A100,0)+1 in a cell, i do get the right number I'm looking for.
Thanks a lot !
Could you help me ?
If I understand what you want the following function looks in column A starting at A2 and searches for what you entered in A1. If found, it activates the cell of the found value. It also returns the row number of the found row in cell B1.
function findText() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var find = sheet.getRange("A1").getValue()// cell of value to find in column A
var values = sheet.getRange(2, 1, sheet.getLastRow()-1).getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == find) {
row = i+2;
sheet.getRange(1, 2).setValue(row); //row number found to C2
var range=sheet.getRange("A"+row)
range.activate() //activate cell of found value
}
}
}}
You could run this with an onEdit trigger.