I have been using the code below to insert a timestamp in column L
when a cell fill color in column C
is changed to white.
The code is working well, but while it works when I choose the white color with the Fill Color paint bucket tool, it does not work when I use the Paint Format paint roller tool.
How do I make the code work with both of these tools?
/**
* Inserts a timestamp when a cell is formatted by choosing a fill color.
* Runs on an installable 'on change' trigger.
* https://developers.google.com/apps-script/guides/triggers/installable
*
* @param {Object} e The 'on change' event object.
*/
function insertTimestampOnFormatChange(e) {
// version 1.0, written by --Hyde, 28 October 2021
const settings = {
sheetsToWatch: /^(Sheet1|Sheet2|Sheet3)$/i,
colorsToWatch: /^(#ffffff)$/i,
columnsToWatch: /^(3)$/i, // column C
timestampColumn: 12, // column L
timestampFormat: 'MM-dd-yyyy HH:mm:ss',
};
if (!e) {
throw new Error('Please do not run the script in the script editor window. '
+ 'It runs automatically when you hand edit the spreadsheet.');
}
if (e.changeType !== 'FORMAT') {
return;
}
const sheet = SpreadsheetApp.getActiveSheet();
if (!sheet.getName().match(settings.sheetsToWatch)) {
return;
}
const range = sheet.getActiveRange();
if (!String(range.getColumn()).match(settings.columnsToWatch)) {
return;
}
const now = new Date();
const rowStart = range.getRow();
const colors = range.offset(0, 0, range.getHeight(), 1).getBackgrounds().flat();
colors.forEach((color, rowIndex) => {
if (color.match(settings.colorsToWatch)) {
sheet.getRange(rowStart + rowIndex, settings.timestampColumn)
.setValue(now)
.setNumberFormat(settings.timestampFormat);
}
});
}