I know that I can use conditional formatting to set a cell background based on the content of another cell. But, is there a way to directly set a cell background via another cell's content, eg using an rgb value?
Something like this:
Cell 1A = "255,0,0"
Cell 2A background is red.
Change Cell 1A to "0,255,0"
Cell 2A background changes to green.
?
I was able to set the cell background value using setBackground:
function onEdit() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets()[1];
var cell = sheet.getRange("A1").getValue();
var cell2 = sheet.getRange("B1");
cell2.setBackground(cell);
}
This is how the value was set in the Sheet:
If you want to use setBackgroundRGB then you can use this code instead:
function ondEdit() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets()[1];
var cell = sheet.getRange("A1").getValue();
Logger.log(cell)
let cellinput = cell.split(',')
const cellinput2 = cellinput.map((ele)=> (Number(ele)))
var cell2 = sheet.getRange("B2");
cell2.setBackgroundRGB(...cellinput2);
}
And this will be the output:
Related
I have a Google Form that is dumping data into this Google Sheet. The data from the form can be very long, with paragraph breaks which causes the row height to be very very tall.
So I found the following script and it works (mostly), except that if I set the second number (35 in this case, aka 'num_rows') to 999 it says it's out of bounds.
function ResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Sets the rows to height of 21 pixels
sheet.setRowHeightsForced(2, 35, 21);
}
Is there a way I can have the num_rows portion be dynamic so it'll run no matter if the sheet has 50 or 100 rows?
You get the error because you can only set height of existing rows.
Besides, it seems the row height set in advance does not apply to rows appended upon new responses.
You may consider to setRowHeightsForced with a onSubmit function.
I figured it out, I added a maxrows function and subtracted one since I'm ignoring the top (header) row.
function ResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This example assumes there is a sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var form = ss.getSheetByName("Form Responses 1");
var total = form.getMaxRows()-1;
// Sets the rows to height of 21 pixels
sheet.setRowHeightsForced(2, total, 21);
}
I want to change the background colour of a table cell in google docs if it matches a value. Here is the image of the table which I want to match
Here is my code where I am getting this table:
for(var dex= 0; dex <= 5 ; dex++){
var row = mytable.getRow(dex);
var res = row.getText();
if (res[dex]=="B+"){
mytable.getCell().setBackgroundColor("#FFFF00");
}
}
It is not colouring the particular with B+.
Please someone help.
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.
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!
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.