GetActiveSheet doesn't work on onChange trigger

504 views
Skip to first unread message

Jenny

unread,
Oct 23, 2021, 12:40:11 PM10/23/21
to Google Apps Script Community
Hello Community, I want to get a name of Changed Sheet.
But GetActiveSheet( ) on onChange( ) always returns first sheet.

Code is just like .. 
function onChange(e){
Logger.log(e.source.getActiveSheet().getName())
}

There have been several bug reports about this in 2014 and I still have this problem.

Is there any other way to get name of changed sheet?
Or is there any other way to request Google for fix this?

Alan Wells

unread,
Oct 23, 2021, 1:00:32 PM10/23/21
to Google Apps Script Community
The Sheets "on change" event object doesn't have a "source" property according to the documentation.
https://developers.google.com/apps-script/guides/triggers/events#change
So, "e.source" should be undefined unless it's an undocumented capability.
Maybe try:
// The code below logs the name of the active sheet.
Logger.log(SpreadsheetApp.getActiveSheet().getName());

Let's us know what happens.

Jenny

unread,
Oct 24, 2021, 12:14:09 AM10/24/21
to Google Apps Script Community
Oh thank you for simple clear answer!

Although there is no "source" property in the official document,
I thought it was possible because some of them (like the below) worked. 
e.source.getName()
e.source.getSheets()

Maybe it's deprecated.

Then, Is always guaranteed that 'changed sheet' on onChange event is always 'active sheet'??

Jenny

unread,
Oct 24, 2021, 12:58:49 AM10/24/21
to Google Apps Script Community
As you told me, I tried SpreadsheetApp.getActiveSheet().getName() on onChange event.
But it doesn't work in some cases just like when i used e.source on onChange event.

When I edited inside of sheet,
e.changeType = INSERT_COLUMN | INSERT_ROW | FORMAT | EDIT
it returns right.

But in other cases,
e.changeType = OTHER | REMOVE_GRID | INSERT_GRID
it always returns FIRST SHEET!

Alan Wells

unread,
Oct 24, 2021, 9:48:33 AM10/24/21
to Google Apps Script Community
Thanks for the detailed feedback. You could create a new issue in the Issue Tracker but I have no confidence in Google that it would ever get fixed. There are bugs in the Issue Tracer that have been there for years. There is at least one case I know of that took 10 years to fix a Sheets issue. Unfortunately, Apps Script is both really great, and sometimes horrible all at the same time. The Apps Script engineers have a long history of not being able to adequately respond to bugs. And internal inquiries from product advocates seems to have no affect at all. I'd think it would be frustrating for Google product managers or advocates to realize that they are in a futile situation with no hope at all for any change. They'd probably quit and go somewhere else to work.
Reply all
Reply to author
Forward
0 new messages