Reputation: 271
I have create a simply formel to check the backroundcolor from a Cell:
function myTest(row, col) {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(row, col).getBackgrounds();
}
This script works fine when I change the backroundcolor for the first time. After that I must delete the formel from the cell an add it again.
What do I have to do if all formulas in the sheet are to be constantly updated?
Here is my fileexample: https://docs.google.com/spreadsheets/d/1CqZWoLy-y7bui8UnnV4B3n-vRHZ_usUyw_A74SYxtNE/edit?usp=sharing
Who has a tipp for me?
Thanks
Upvotes: 0
Views: 53
Reputation: 1533
Custom functions wont automatically recompute unless a change to one of their input values occurs.
Your function is written as =mytest(ROW(A1);COLUMN(A1))
, so it wont recompute unless the value of ROW(A1)
or COLUMN(A1)
changes -- which will never happen, given A1's row is always 1, and its column is always 1.
In general, I don't think there's a way to hook into a formatting change event in Google Sheets at this time. However, you do have some options for workarounds that can get the color codes to update after a small delay, including:
My sense is (A) or (B) is the simplest option to wire up given what you have.
A:
function refreshBackgroundColorCodes(event) {
var sheet = (event ? event.source.getSheetByName("Tabellenblatt1") : null) || SpreadsheetApp.getActiveSheet();
sheet.getRange("B:B").setValues(sheet.getRange("A:A").getBackgrounds());
}
B:
function refreshBackgroundColorCodes(event) {
var sheet = (event ? event.source.getSheetByName("Tabellenblatt1") : null) || SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B:B");
var formulas = range.getFormulas();
range.clearContent();
SpreadsheetApp.flush();
range.setFormulas(formulas);
}
You can also use either on-demand via a custom menu option by adding the following code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Background Colors').addItem('Refresh color codes', 'refreshBackgroundColorCodes').addToUi();
}
Upvotes: 1