The code below works fine - each cell is being referenced directly. When a value is entered into any cell, 'in.' is appended as expected.
In the code below I have tried to replace the direct cell references with Named Ranges, but it does not seem to work. Whenever I enter a value into any of the named ranges, 'in.' is not appended. Can anyone help?
JavaScript:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
var editedCell = range.getA1Notation();
// Check if the edited cell is C5, C6, C9, C10, C11, or C14
if ((editedCell === "C5" || editedCell === "C6" || editedCell === "C9" ||
editedCell === "C10" || editedCell === "C11" || editedCell === "C14")) {
// Check if the edited cell is now empty
if (range.isBlank()) {
// If it's empty, clear the cell
sheet.getRange(editedCell).setValue("");
} else {
// Append ' in.' to the entered value
sheet.getRange(editedCell).setValue(range.getValue() + " in.");
}
}
}
In the code below I have tried to replace the direct cell references with Named Ranges, but it does not seem to work. Whenever I enter a value into any of the named ranges, 'in.' is not appended. Can anyone help?
JavaScript:
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
var editedCell = range.getA1Notation();
// Define named ranges
var namedRanges = {
"NAME_A": sheet.getRange("NAME_A"),
"NAME_B": sheet.getRange("NAME_B"),
"NAME_C": sheet.getRange("NAME_C"),
"NAME_D": sheet.getRange("NAME_D"),
"NAME_E": sheet.getRange("NAME_E"),
"NAME_F": sheet.getRange("NAME_F")
};
// Check if the edited cell is one of the named ranges and it is not empty
if (Object.values(namedRanges).includes(range) && e.value !== "") {
// Append ' in.' to the entered value
range.setValue(e.value + " in.");
}
}