I'm working on a script that interacts with Google Form' response sheet.
FormApp.getActiveForm().getDestinationId()
give me the spreadsheet id, but I don't find a way to get the sheet itself. User can change its name and position, so I need to get its id, like in
Sheet.getSheetId()
I also have to determine the number of columns the responses uses. It's not equal to the number of questions in the form. I can count the number of items in the form:
Form.getItems().length
and then search for gridItems, add the number of rows in each and add them minus one:
+ gridItem.getRows().length - 1
Finally, I think there's no way to relate each question with each column in the sheet, but by comparing somehow columns names with items title.
Thank you
There is now a way to verify which sheet in a Google Sheets file with multiple linked forms corresponds to the current Form - through the use of
Sheet#getFormUrl()
, which was added to theSheet
class in 2017.If you have unlinked the Form and the destination spreadsheet, then obviously you won't be able to use
getDestinationId
orgetFormUrl
.@tehhowch came very close to the correct answer, but there is a problem with the code: there is no guarantee that
form.getPublishedUrl()
andsheet.getFormUrl()
will return exactly the same string. In my case,form.getPublishedUrl()
returned a URL formed ashttps://docs.google.com/forms/d/e/{id}/viewform
andsheet.getFormUrl()
returnedhttps://docs.google.com/forms/d/{id}/viewform
. Since the form id is part of the URL, a more robust implementation would be:To get the spreadsheet, once you have the DestinationID, use SpreadsheetApp.openById(). Once you have that, you can retrieve an array of sheets, and get the response sheet by index, regardless of its name.
From this point, you can manipulate the data in the spreadsheet using other Spreadsheet Service methods.
You're right - the number of current items does not equal the number of columns in the spreadsheet. The number of columns each response takes up in the destination sheet includes any questions that have been deleted from the form, and excludes items that are not questions. Also, the order of the columns in the spreadsheet is the order that questions were created in - as you re-arrange your form or insert new questions, the spreadsheet column order does not reflect the new order.
Assuming that the only columns in the spreadsheet are from forms, here's how you could make use of them:
And your last point is correct, you need to correlate names.
I needed this also, and remarkably there is still no apps script method that facilitates it. In the end I set about finding a reliable way to determine the sheet id, and this is what I ended up with by way of programmatic workaround:
I'm sure some won't like this approach because it modifies the form and spreadsheet, but it does work well.
With the necessary wait times included it takes about 12 seconds to perform all the look up / clean up operations.
Here's my code for this method in case anyone else might like to use it.