我正在使用一个函数,该函数检查颜色的单元格并在另一列中返回是/否。但是,在应用该功能之后,随后又更改了单元格的颜色,该功能不再适用。
例如,该函数被应用并返回“是”,那么如果我将单元格的颜色更改为白色,则不会返回“否”。是否可以具有主动检查单元格的功能,或者我只需要在每次单元格颜色更改时重新应用该功能?
function GetCellColorCode(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getRange(input);
var result = cell.getBackground();
var green = '#d9ead3';
var white = '#ffffff';
if (result == green) {
return 'Yes';
} else {
return 'No';
}
}
答案 0 :(得分:3)
=GetCellColorCode(###)
的自定义公式。为了实现您的目标,我建议使用TextFinder和OnChange事件触发器刷新公式。
请复制并粘贴以下脚本。
function onChange(e) {
if (e.changeType === "FORMAT") {
const formula = "=GetCellColorCode"; // Function name of your custom function.
const sheet = e.source.getActiveSheet();
const tempFormula = "=sampleFormula";
sheet.createTextFinder("^\\" + formula).matchFormulaText(true).useRegularExpression(true).replaceAllWith(tempFormula);
sheet.createTextFinder("^\\" + tempFormula).matchFormulaText(true).useRegularExpression(true).replaceAllWith(formula);
}
}
=GetCellColorCode
替换为其他名称,然后将其修改为=GetCellColorCode
。这样,可以刷新自定义公式。onChange
函数时,会发生错误,因为此函数使用OnChange事件触发器的事件对象。请注意这一点。 Please install the OnChange event trigger to the function of onChange
.
onChange
。完成上述流程后,请更改单元格的背景颜色。这样,将触发OnChange事件触发器并运行函数onChange
。然后,刷新GetCellColorCode
的自定义公式。
在此脚本中,即使复制并粘贴了单元格,也不会运行OnChange vente触发器。请注意这一点。在这种情况下,请修改背景颜色。
GetCellColorCode
。因此,当您更改函数名称时,也请修改上面的脚本。请注意这一点。