Before some code runs, I want to check it is the correct sheet.
I can use the sheet name, but my concern is that if someone changes the sheet name, the code won't run. The sheet index also seems to change if the sheet is moved.
Therefore I want to use something that doesn't change.
I believe the sheet number and sheet ID never change.
So I was hoping to use one of them, but I can't see a way of doing that.
What I want in non-coding language is:
If active sheet number = 4 then run the code or If active sheet ID = 0123456789 then run the code.
Thanks to JPV's link, I have an answer and learned a few things.
- You can't use the sheet number to check if the correct sheet is active.
- You can use the getSheetId(), but the return you get is not useable.
- To make it useable, you need to add getSheetId().toString().
- To do the if statement, I needed two equal signs.
if (SpreadsheetApp.getActiveSheet().getSheetId().toString() == 0123456789) {do this when true}
- I've been trying to solve this for days, so I should have come here and asked for help earlier.
The main thing I didn't know was the "toString()" part. It always seems so easy and obvious once I know.
Thank you, JPV.