0

I have been calling in the active things either by event.source.getActive... or by SpreadsheetApp.getActive... . Which is more proper? I have pulled these from multiple sources, so sometimes in my script I use one over the other. I feel like I have tried to use them interchangeably, but things went wrong, (this was early on so I gave up). But I am wondering which way should be best, or do I really need them both?

var ss = SpreadsheetApp.getActiveSpreadsheet();//ss is active spreadhseet
var s = event.source.getActiveSheet();//s is active sheet withing spreadsheet
var r = event.source.getActiveRange();//r is active cell


var sheetAppActive = SpreadsheetApp.getActiveSheet();//sheetAppActive is active sheet within spreadsheet
var rangeAppActive = sheetAppActive.getActiveCell(); //rangeAppActive is the active cell
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dain Luka
  • 3
  • 5

2 Answers2

1

Both are appropriate and feasible, but you can use it as a requirement.

If your script require to use only current script then you may go with getActiveSheet(), here you cannot use another sheet's data.

And if you use getActiveSpreadsheet(), it allow you to use current as well as another available sheet's data.

Depends on current requirement and future possible requirement.

YNK
  • 129
  • 2
  • 15
0

Recently, I had problems using typeof event.value on onEdit simple trigger as it always returned string. The problem was solved by using typeof SpreadsheetApp.getActiveRange().getValue(), so I think that the best way will depend on the event property that you will use.

By the other hand you should try to minimize the calls to the Google Apps Services classes as they in order to keep at minimum the execution time.

References

my answer to Script to automatically capitalize contents of a cell in Google Sheets?

Community
  • 1
  • 1
Rubén
  • 34,714
  • 9
  • 70
  • 166