Home > Mobile >  Modifying e.range.getA1Notation() to accept wider range of inputs in Google Apps Script
Modifying e.range.getA1Notation() to accept wider range of inputs in Google Apps Script

Time:05-25

I'm using a Google Apps script to update the active sheet's name with a cell (C6) value when another cell (I16) is updated. Currently, the script works fine when I16 is updated and the sheet is renamed as intended.

However, I would like the range of inputs to be much wider, like an A:J kind of range, or at least A1:J104. How can I achieve this?

I have also read this thread but I'm unsure of how to extract that info specific to this scenario.

The script I'm using is as follows:

function onEdit(e) {

if(e.range.getA1Notation() !== 'I16') return;

var s = e.source.getActiveSheet();

s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().setName(s.getRange('C6').getValue());

}

Any assistance will be much appreciated. Thank you in advance!

EDIT: With the script, the following happens:

  1. Upon editing cell I16, the contents of cell C6 changes via a formula. For example, when I enter "002" into cell I16, cell C6 updates to "Report-002", and so on.
  2. At the same time, the script extracts the string in cell C6 and updates the name of the current sheet accordingly (i.e. the sheet is now named "Report-002".

The script works well for this specific scenario. However, I would like to trigger the script when I update a wider range of cells, and not just cell I16. Ideally, I'd like the script to run if I updated any cell in the sheet.

Does that help to clarify what I'm trying to achieve?

CodePudding user response:

Try

function onEdit(e) {
  e.source.getActiveSheet().setName(e.source.getActiveSheet().getRange('C6').getValue());
}

CodePudding user response:

Change the if condition to account for bounds:

const [/*actual row*/ar, ac] = ['rangeStart','columnStart'].map(s => e.range[s])
const [/*row start*/rs, re, cs, ce] = [1,2,3,4];//C1:D2
if(ar >= rs && ar <= re && ac >= cs && ac <= ce){
// do stuff
}
  • Related