Question

Example:

https://spreadsheets.google.com/ccc?key=0Am8IA9FchrLbdHlkbWFMOWtZa1Y0RWpMNUV5Q3RMaVE&hl=en&authkey=CPXlr5UL

When you install an on-edit trigger, your function gets passed the start and end rows and columns. But not which sheet was edited.

Naturally, my first thought was to grab SpreadsheetApp().getActiveSheet() but that doesn't work.

Edit any cell in the example spreadsheet. The installed on-edit trigger will fill in values to Sheet1 telling you what you edited. Now switch to sheet 2 or sheet 3 and edit something. It always reports that Sheet1 was edited.

The trigger's code:

function my_on_edit(e) {

  //the object passed in, e, doesn't know what sheet was edited. Really!

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetname = ss.getName();

  var thesheet = ss.getActiveSheet();

  var sheetname = thesheet.getName();

  //Tell us what was edited, please.
  ss.getSheetByName("Sheet1").getRange("A2:E2").setValues([[ spreadsheetname, sheetname, e.range.rowStart, e.range.columnStart, Utilities.jsonStringify(e)]]);

}

How can I get the sheet edited? Is this my mistake or is this a bug? Or a deficiency in how they're doing the event handling?

Was it helpful?

Solution 2

I crossposted this question to the google apps script help forum:

http://www.google.com/support/forum/p/apps-script/thread?tid=535e1d8ff33a731f&hl=en

Henrique Abreu replied with an answer; that this is a deficiency with how Google Apps Script does installed onedit triggers. But he also gave an undocumented workaround:

There's two undocumented things I use to find out which sheet was edited. First, by inspecting the parameter passed to the installable edit function, I found out a "gridId" parameter, which is the ID of the sheet (same number we see on the url ...#gid=). And the sheet.getSheetId(), which shows up in the code editor but not in the docs. Here is an example:

function my_on_edit(e) {
  var s = findSheetById_(e.gridId);
  var r = e.range;
  s.getRange(r.rowStart, r.columnEnd+1).setValue( s.getName() );
}

function findSheetById_(id) {
  var sheets = SpreadsheetApp.getActive().getSheets();
  for( var i in sheets )
    if( sheets[i].getSheetId() == id )
      return sheets[i];
  throw 'Unable to find sheet with id: '+id;
}

OTHER TIPS

I've found this workaround on many forums but when I tried to use it I kept getting e.gridId = undefined. After banging my head and pulling my hair out I ended up trying some alternatives and found the solution. So I thought I'd share my little discovery:

e.range.getGridId() now returns the same value. So in the script above change the second line to:

var s = findSheetById_(e.range.getGridId());

to make it work if you're getting the same undefined issue I did.

The code does not work because the e.range and e.value objects do not give any objects on an installable trigger(undefined)! This has been brought to the attention of Google, but there is no action after the message of this in Google-script-issues!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top