I have a Google spreadsheet which contains two worksheets. Each sheet contains a column of URLs followed by 6 columns of data. I would like to highlight on the second sheet if the value is different, ideally styling the particular cell with a red or green background depending on the value.
Sheet 1
╔═════════════╦═════╦════╦═════╦════╦════╦════╗
║ URL ║ d1 ║ d2 ║ d3 ║ d4 ║ d5 ║ d6 ║
╠═════════════╬═════╬════╬═════╬════╬════╬════╣
║ example.com ║ 400 ║ 11 ║ 2.2 ║ 50 ║ 60 ║ 70 ║
╚═════════════╩═════╩════╩═════╩════╩════╩════╝
Sheet 2
╔═════════════╦═════╦════╦═════╦════╦════╦════╗
║ URL ║ d1 ║ d2 ║ d3 ║ d4 ║ d5 ║ d6 ║
╠═════════════╬═════╬════╬═════╬════╬════╬════╣
║ example.com ║ 400 ║ 11 ║ 2.2 ║ 55 ║ 68 ║ 90 ║
╚═════════════╩═════╩════╩═════╩════╩════╩════╝
In this case, I'd like the 55, 68 and 90 value to be highlighted as they are different from sheet 1.
My quick solution was to export each sheet as a CSV and then run a diff over them to highlight changes, but this doesn't fully achieve what I'd like.
The conditional formatting can do that, but it can only reference cells within the same sheet. To get around this restriction, place the command such as
=arrayformula(Sheet1!B2:H2)
somewhere on the second sheet (e.g., in cell AB2). Then use conditional formatting for the range B2:H2 with the rule "value is not equal to ... =AB2".
(The conditional formatting formula is entered as it should be read for the upper left corner of the range being formatted).
Another approach, avoiding duplication of data, is to use an Apps script. Here is sample code that sets backgrounds according to the situation you described. Note this will not update automatically unless it's set to trigger on every edit.
The aforementioned approach with conditional formatting is to be preferred.
function compare() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName('Sheet2');
var values1 = sheet1.getRange('B2:H2').getValues()[0];
var values2 = sheet2.getRange('B2:H2').getValues()[0];
var backgrounds = [];
for (var k = 0; k < values1.length; k++) {
backgrounds.push(values1[k] == values2[k] ? 'white' : 'red');
}
sheet2.getRange('B2:H2').setBackgrounds([backgrounds]);
}