How to get the ID of the Form Responses sheet inside the destination spreadsheet without using getSheetByName?

390 views
Skip to first unread message

Hoang Trinh

unread,
Oct 3, 2019, 3:17:18 AM10/3/19
to Google Apps Script Community

Hi everyone,

I have a Forms addon, and Google will automatically put all responses inside Form Responses 1 sheet, and my addon also updates something in there.

But there is a case when my users change the sheet name (accidentally or not), for example they change it to Form Responses 2

In that case, I will need to find the correct sheet in order to update anything.

But I can't find any Apps Script function to do that. There is even a feature request that is still open: https://issuetracker.google.com/issues/37175450 since 2017

Is there any workaround for this problem?

Note: Somehow Google can update the correct sheet, even if I changed from Form Responses 1 to ABCXYZ, so I thought logically they must have the sheet ID.

Thank you.

New Addons

unread,
Oct 3, 2019, 5:07:15 AM10/3/19
to Google Apps Script Community
Hello,

you can have the edit-Url of the form: 
getEditUrl()

you also have the id of the spreadsheet stores form responses:
.getDestinationId()

so, you can get all the sheets/worksheets => get all form URL for each sheet:
getFormUrl()

So, you will find the sheet/worksheet stores form responses by comparing the URLs. Note: remove the /edit and /viewform at the end of each url

 =>When you have the sheet/worksheet, you can get the id of this sheet. 


Alan Wells

unread,
Oct 3, 2019, 9:09:11 AM10/3/19
to Google Apps Script Community
When either the user or the add-on code saves a setting for the target sheet tab, you need to write the code so that the sheet tab setting is stored as the sheet tab ID, and not the sheet tab name.  And because Apps Script can't directly get a sheet tab by ID, your code needs to get all sheet tabs, and loop through them until you find the correct sheet tab ID number.  As far as I know, that's the only way to do it.
Note that the first default sheet tab always has an ID number of zero, and zero evaluates as falsy in JavaScript, so don't test for falsy sheet tab ID numbers.

Hoang Trinh

unread,
Oct 3, 2019, 10:26:00 PM10/3/19
to Google Apps Script Community
Thank you very much.
Reply all
Reply to author
Forward
0 new messages