Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return the range of an edited cell using an onEdit event?

This seems like it should be easy as pie, but I can't seem to get any of my little test implementations to work. The ultimate goal is to create a function that runs only when cells in a certain range are edited. To accomplish this, I am creating a function that runs on every edit but starts by checking to see if the range of the edited cell is within a certain range, and only executing the rest of the code if this is true, and otherwise, it just ends immediately.

The first step in doing this, it seems to me, is to find out how to return the range of the edited cell. I've tried various tests.

Here is the current version, which I test by editing the spreadsheet:

function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var eRange = e.range;
var message = eRange.getA1Notation();
ss.toast(message);
}

(output: toast does not appear/nothing happens)

I had about 10 other versions posted here, but I realized it was not helpful.

I've managed to return [object Object] and [undefined] in my toast message, so I know I'm not making some silly error with my toast function.

Doesn't e.range return a range? Is that where I go wrong? If it returns a range, shouldn't I be able to use any of the methods in range, just as if I'd used something like "ss.getRange("A1").someMethod()"? Or is there some difference that I don't understand?

Okay, I realize I've asked a couple of different questions here-- I'm just trying to give a clearer picture of my thought process in case it helps you to correct me where I'm wrong. The simple question is this: what sort of command or method can I use to return the range of an edited cell?

I'm so sorry to ask such a basic question. I'm really afraid that someone will enter the right combination of search terms and the answer will be right on the top of the results... but I just can't seem to find it on my own, so here I am. I've tried lots of snippets of code from both StackOverflow and the old Google Groups support forum, as well as lots of my own attempts. I see people have asked lots of similar questions regarding my ultimate goal here-- only running the onEdit function if cells in a certain range are edited-- but the answers are all more complex and seem tailored to each individual situation, and they don't fit mine. I'm certain the answer is out there, but I've been searching for three straight evenings now and found many similar questions asked, and just can't seem to get my own implementation of those suggestions to work. Thanks for being patient with me, and thank you so much for any answers you can offer!

like image 527
Dr.Queso Avatar asked Oct 21 '12 05:10

Dr.Queso


1 Answers

here is the code taken (and slightly modified) from the doc :

function onEdit(event){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  r.setComment("Last modified: " + (new Date())+' by '+Session.getActiveUser());
  ss.toast('Last cell modified = '+r.getA1Notation()+' by '+Session.getActiveUser());
}
like image 192
Serge insas Avatar answered Oct 01 '22 18:10

Serge insas