Thanks for your answer, @user2759865.
The following is an adaptation for anyone who wants a function that takes a range of cells, iterates over each of them, and sets the background color of each based on the text value of the cell.
/**
* https://stackoverflow.com/a/78535683/470749 based on https://stackoverflow.com/a/38894241/470749
*/
function setCellBackgroundColorBasedOnValue() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange('colors');
const values = range.getValues();
const missingColorCodes = [];
for (let r = 0; r < values.length; r += 1) {
for (let c = 0; c < values[r].length; c += 1) {
const desiredBackgroundColorCode = values[r][c];
const cell = range.getCell(r + 1, c + 1);
cell.setBackground(desiredBackgroundColorCode);
const actualBackgroundColorCode = cell.getBackground();
if (actualBackgroundColorCode === '#ffffff' && desiredBackgroundColorCode !== 'white') {
// https://developer.mozilla.org/en-US/docs/Web/CSS/named-color
missingColorCodes.push(desiredBackgroundColorCode);
}
}
}
if (missingColorCodes.length > 0) {
Logger.log(`Missing color codes:\n${missingColorCodes.join('\n')}`);
}
}