So i have an OnEdit function that when the user chooses a color from the list, the cells background color becomes that selected value.
This works fine, but I am trying to get the function to update the cells background color immediately when data is copied and pasted into the spreadsheet as well as when a user edits a cell. Currently when you copy and paste data into the speadsheet, the code i wrote to change the cells background colour does not trigger as nothing has been "edited". Im trying to get the code to work for OnEdit and when data is copied and pasted into the sheet. Any ideas would be greatly appreciated! Thanks!
This is what I have so far..
// Validates the colours using the above map and changes the cell background to the input colour..
function onEdit(e) {
try {
var range = e.range;
var sheet = range.getSheet();
var tabColumnData = getColumnData(sheet);
if(!tabColumnData.hasOwnProperty('Color')) return;
var col = range.getColumn();
if (col == tabColumnData['Color'] + 1) {
var color = e.value;
if(validateCellValue(color) || COLOR_TO_HEX_MAP.hasOwnProperty(color)) {
range.setBackground(color);
} else {
range.clear();
SpreadsheetApp.getUi().alert('Please select a color from the list or a hex color code.');
}
}
if (col == tabColumnData['Name'] + 1) {
setDataValidation(createColorList());
}
} catch(e) {
Logger.log(e);
}
}
// Sets the data validation when the spreadsheet is opened.
function onOpen(e){
setDataValidation(createColorList());
}
e.range
is more than one cell and then iterate through each row and column in the range. e.range.rowStart is the top row, e.range.rowEnd is the bottom row. e.range.columnStart is the left most column e.range.columnEnd is the rightmost column. It is a range so getWidth() and getHeight() methods apply. And e.value only applies to the upper left corner cell. – Cooper