Something seems to be off with the conditional formatting. One solution, anyway, as Vidar suggested, is to use Custom formula is
instead of Greater than
in the dropdown.
Use the formula =B5>$B$3
with your range.
Use the following code to accomplish your goal.
Code
function onOpen() {
SpreadsheetApp.getUi().createMenu('Custom Format')
.addItem('Max & Color', 'getmaxColor').addToUi();
}
function getmaxColor() {
var ss = SpreadsheetApp.getActive(), aRange = ss.getActiveRange();
var data = aRange.getValues(), colors = aRange.getBackgrounds();
var dest = aRange.offset(0, data[0].length, 1, 1);
var index = getmaxIndices(data);
var max = data[index.i][index.j], color = colors[index.i][index.j];
dest.setValue(max).setBackground(color);
}
function onEdit() {
var ss = SpreadsheetApp.getActive(), col = ss.getActiveCell().getColumn();
if(col < 7) {
var aRange = ss.getRange("A1:F7"), data = aRange.getValues();
var colors = aRange.getBackgrounds();
var dest = aRange.offset(0, data[0].length, 1, 1)
var index = getmaxIndices(data);
var max = data[index.i][index.j], color = colors[index.i][index.j];
dest.setValue(max).setBackground(color);
}
}
function getmaxIndices(data) {
var max = 0, indexI = 0, indexJ = 0;
for(var i=0, iLen=data.length; i<iLen; i++) {
for(var j=0, jLen=data[0].length; j<jLen; j++) {
if(data[i][j] > max) {
max = data[i][j], indexI = i, indexJ = j;
}
}
}
return {"i":indexI, "j":indexJ};
}
Explained
The onOpen() adds a menu item. Select the range you want to evaluate and select the menu option Max & Color. The values and backgrounds are collected as is an offset range. Then the indices (2d-array) of the max value are obtained, through the getmaxIndices(). Afterwards the corresponding max value and color is set.
The onEdit() does almost the same, but it has a fixed range. It will only fire if a cell is edited and if the edited cell belongs to the A1:F7
range.
Note
The reason for getting the error message, can be read in this answer: custom function
Example
I've added the sample code into your example file.
Best Answer
for a number between
1.9
-8.1
custom formula applied for cell B1 or range B1:B