GOOGLE SHEETS: Replacing cell references with Named Ranges in script - FAILURE.

ddub25

Well-known Member
Joined
Jan 11, 2007
Messages
625
Office Version
  1. 2019
Platform
  1. Windows
The code below works fine - each cell is being referenced directly. When a value is entered into any cell, 'in.' is appended as expected.

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.");
  }
}
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top