Google spreadsheet color sheet tab - google-sheets

I am using google script in a spreadsheet and I would like to color the sheet tab. Do you have any idea how to do ?
Thank you for your answer

You need to use the setbackground's function
I show you an example :
Function colorRow(r){
var sheet = SpreadsheetApp.getActiveSheet();
var c = sheet.getLastColumn();
var dataRange = sheet.getRange(r, 1, 1, c);
dataRange.setBackground("white");
}
Doc : https://developers.google.com/apps-script/reference/spreadsheet/range#setBackground(String)

You can use the setTabColor().
From the Google documentation you use it like this:
// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.setTabColor("ff0000"); // Set the color to red.
first.setTabColor(null); // Unset the color.

Related

How can I make a Google Sheets macro to search all rows for a particular fill color, copy the rows and paste them at the bottom of the sheet?

The rows in my sheet have a fill color in column A of that particular row. I want to be able to use a macro or maybe for loop to search for these rows with the color identifier, copy and paste them below, return to the row where I was and continue the search until the I've hit the bottom of the original list.
Update -
Basically I want to start with a sheet like this.
Google Sheet before macro
and have the end result look like this
Google sheet post macro
If I understand you correctly, you want to loop through each row in your sheet and copy/paste the ones which have a certain background color in column A after the last row. If that's the case, then you could use something along the following lines using Google Apps Script (you would have to create a script bound to your spreadsheet):
function appendRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var color = "#582323"; // Please change accordingly
var firstRow = 1;
var numRows = sheet.getLastRow() - firstRow + 1;
var column = 1;
var numCols = sheet.getLastColumn() - column + 1;
var range = sheet.getRange(firstRow, column, numRows); // Get column A
var backgrounds = range.getBackgrounds(); // Get backgrounds of each cell in column A
for (var i = 0; i < backgrounds.length; i++) { // Iterate through each cell in column A
if (backgrounds[i][0] == color) { // Check background color
var rowToCopy = sheet.getRange(i + 1, column, 1, numCols); // Row to be copied
var lastRow = sheet.getLastRow(); // Row index to copy to
rowToCopy.copyTo(sheet.getRange(lastRow + 1, column, 1, numCols)); // Copy row to the bottom
}
}
}
Notes:
Please change the background color you want to look for.
Check inline comments for more information on what the script is doing, line by line.
Reference:
getBackgrounds
getRange
copyTo
I hope this is of any help.

Conditional Formatting (Color) of cells on a sheet based on values on another sheet

I am trying to achieve conditional formatting but Unable to sort out a system.
Things i am trying to achieve:
Match Styles and Pattern column in both sheets. Range: A21: B40 with A2: C63.
Upon Match, Column headers ( G1 : AO1 ) in Order Sheet would be matched with the Guide Sheet Row Values A21: B40, where color of cell would change according to the guide sheet in the order sheet. If any column headers dont match then it would stay white.
I have attached an example sheet where the ST5 in the order sheet was used as example but manually. If the process could be made automatic it would ease my work in office.
Example Sheet
https://docs.google.com/spreadsheets/d/1cTgAKhIO0_OJgoReuAMSaZU-7BE4P2xHdt3ASsPgIH0/edit?usp=sharing
I am also willing to do a Hangouts call to sort these system.
Sorry for the incapability to explain clearly and Thanks in Advance.
For Guide you will need 4 rules. Add in cell C21. This will color code Guide.
Apply to Range C21:R1017 (for all 4 rules)
Custom Formula
=match(C21,$C$2:$C$10,0)
=match(C21,$E$2:$E$16,0)
=match(C21,$G$2:$G$9,0)
=match(C21,$I$2:$I$4,0)
For Orders, you need script. Try the following:
function colorCells(){
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName("Orders")
var s1=ss.getSheetByName("Guide")
var lr=s.getLastRow()
var lc=s.getLastColumn()
var lr1=s1.getLastRow()
var lc1=s1.getLastColumn()
var orderdata=s.getRange(1, 1, lr, lc).getValues()
var guidedata=s1.getRange(21, 1, lr1=21, lc1).getValues()
var headers=s.getRange(1, 7, 1, lc-6).getValues()
for(var i=0;i<orderdata.length;i++){
for(var j=0;j<guidedata.length;j++){
if(orderdata[i][1]==guidedata[j][0] && orderdata[i][2]==guidedata[j][1]){
var row=i+1
var row1=j+21
var line=s1.getRange(row1, 3, 1,18).getBackgrounds()
var lval=s1.getRange(row1, 3, 1,18).getValues()
for(var k=0;k<headers[0].length;k++){
for(var l=0;l<lval[0].length;l++){
var t=headers[0][k]
var t1=lval[0][l]
if(t==t1){
var c=s1.getRange(row1, l+3, 1,1).getBackground()
var sc=s.getRange(row, k+7, 1,1).setBackground(c)
}}}}}}}
This is a working sample of your spreadsheet. Make a copy and try it:
https://docs.google.com/spreadsheets/d/12KoRLAnkJwQB0OKTmauxmqxq5BT71gyIgCo3Wf-jemk/edit?usp=sharing

Google Sheets set tab color of one sheet based on value of cell in another sheet

I would like to change the tab color of a sheet (sheet 2) to the same color as the background color of a cell in another sheet (sheet 1). For example, sheet 1 acts as a table of contents and lists all the other sheets in the workbook. When I set the background color of the cell in my table of contents, I would like the corresponding sheet's tab to change to that same color.
Any help would be appreciated.
Thanks!
Yes, I did try to code something, and I actually figured it out. This is what I ended up doing:
function getSheetTab() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var tabColor = activeSheet.getActiveCell().getBackground();
var sheetName = activeSheet.getActiveCell().getValue();
var newSheet = app.getActiveSpreadsheet().getSheetByName(sheetName);
newSheet.setTabColor(tabColor);
}
Took me quite a bit of time, as I'm a bit of a noob to Google Sheets.
Thanks!

Google Sheets tab color for conditional formatting

I'm trying to use the tab color to conditionally format a cell. I've got a custom function that returns all the tabs from my spreadsheet:
function STABS() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var stabs = [];
for (i = 0; i < ss.getNumSheets(); i++) {
stabs.push(ss.getSheets()[i].getName());
}
Some of these tabs are colored as a visual cue to our workflow. I would like to have the cell highlighted the same color as the tab.
Any help would be greatly appreciated.
Thanks,
Lynn
To get the color of the tab in the script you can use the getTabColor() function for the Sheet class.
See the Sheet API here
The you can use functions like setBackgroundColor() for a range to set the formatting.
See the Range API here

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();
}

Resources