Reputation: 525
I am using this functions to write values in cell.
For example if cell has specific color then write value to cell and this function is slow. Is there any fast function to write values in cell if it has specific color.
There is one more problem that is these formatting changes according to Conditional Formatting Rules but when it changes then formula does not change the Value accordingly. It should be dynamic it cell color changes then formula must observe.
I have tried with below function but it is slow.
=IF($B2<>"",IF(GETBACKGROUNDCOLOR(cell("address", Data!P3)) = "#f4cccc", "MISSING",IF(Data!P3="P","Pending","")),"")
function GETBACKGROUNDCOLOR(cell){
return SpreadsheetApp.getActiveSheet().getRange(cell).getBackground();
}
Upvotes: 1
Views: 119
Reputation: 5325
You can change your function a bit (notice the s
at the end) to work with ranges instead of a single cell:
function GETBACKGROUNDCOLORS(range){
return SpreadsheetApp.getActiveSheet().getRange(range).getBackgrounds();
}
And then use ARRAYFORMULA
with it:
=ARRAYFORMULA(
IF(
B2:B27 = "",,
IFS(
GETBACKGROUNDCOLORS("Data!P3:AM28") = "#f4cccc",
"MISSING",
Data!P3:AM28 = "P",
"Pending",
True,
)
)
)
Both those steps will increase performance significantly.
See the array solution sheet in your sample spreadsheet.
Upvotes: 1
Reputation: 201398
I believe your goal is as follows.
=IF($B2<>"",IF(GETBACKGROUNDCOLOR(cell("address", Data!P3)) = "#f4cccc", "MISSING",IF(Data!P3="P","Pending","")),"")
.Data
sheet are changed.In this case, how about the following sample script?
Please copy and paste the following script to the script editor and save it. And, when your sample Spreadsheet is used, please put a custom function of =SAMPLE(Data!P3:AM,"Data!P3:AM",B2:B)
to the cell "G2" of "Formula" sheet. By this, the values are put.
In this sample script, 3 arguments of Data!P3:AM,"Data!P3:AM",B2:B
are used. Those are used as the cell values of "Data", the range for retrieving the background colors, and the cell values of "Formula". Data!P3:AM
and B2:B
are used. By this, when those cells are changed, the custom function is recalculated. I thought that this might be the result you expect.
function SAMPLE(srcValues, srcRange, dstValues) {
const srcBackgrounds = SpreadsheetApp.getActiveSpreadsheet().getRange(srcRange).getBackgrounds();
return srcValues.map((r, i) => r.map((c, j) => {
if (dstValues[i][0] != "") {
if (srcBackgrounds[i][j] == "#f4cccc") {
return "MISSING";
}
return c == "P" ? "Pending" : "";
}
return "";
}));
}
When this script is used for your sample Spreadsheet, the following result is obtained.
=SAMPLE(Data!P3:AM,"Data!P3:AM",B2:B)
. And, the arguments of Data!P3:AM,"Data!P3:AM",B2:B
. The 2nd argument is the string type. Please be careful about this.Upvotes: 1