TypeError: Cannot read property 'getResponse' of undefined

2,184 views
Skip to first unread message

Torgeir Helgesen Riseth

unread,
Oct 18, 2021, 11:33:18 AM10/18/21
to Google Apps Script Community
Hi!
I'm following a tutorial on how to change filenames of files upladed from a Google Form, using this guide: https://medium.com/@yashashreepatel/how-to-rename-google-forms-responses-in-the-google-drive-automatically-94f033d90b24

My script runs into an error and will not execute: 
TypeError: Cannot read property 'getResponse' of undefined
RenameResponses @ Kode.gs:19

The script looks like this (line 19 in bold): 
function RenameResponses() {
var form = FormApp.openById('RemovedMyIdButItsHere'); //DDD
var formResponses = form.getResponses();
var endString = '/view?usp=drivesdk';
  
var folder = DriveApp.getFolderById(' RemovedMyIdButItsHere  ');
var files = folder.getFiles();
 
  while (files.hasNext()) {
    var file = files.next();
    for (var i = 0; i < formResponses.length; i++) {
      var formResponse = formResponses[i];
      var itemResponses = formResponse.getItemResponses();
      var itemResponseTimestamp = itemResponses[0];
      var itemResponseName = itemResponses[1];
      var itemResponsePhoto = itemResponses[2];
      
      var photoID = itemResponsePhoto.getResponse();
      var newName = itemResponseName.getResponse() + " - " + itemResponseTimestamp.getResponse();
      var url = baseString + photoID + endString;
      var urlCheck = file.getUrl();
      if ( url == urlCheck) {
        var modName = newName + ".jpg";
        file.setName(modName);
      }
    }
  }
}

Any ideas?
Cheers!

Alan Wells

unread,
Oct 18, 2021, 11:42:09 AM10/18/21
to Google Apps Script Community
The line:
var itemResponsePhoto = itemResponses[2];
is generating a value for the variable itemResponsePhoto that is undefined.
You can change the line that's causing the error to:

var photoID = !itemResponsePhoto.getResponse() ? "No ID" : itemResponsePhoto.getResponse();
That would prevent the code from failing but it probably won't fix the real problem.

Do you know if the following line is returning Form responses?
var itemResponses = formResponse.getItemResponses();

Torgeir Helgesen Riseth

unread,
Oct 18, 2021, 11:51:18 AM10/18/21
to Google Apps Script Community
Hi! 
Thanks for reaching out.
I changed out the line with the new one you wrote me, however I got the same error.

I'm a total noob and thus I need to be spoonfed the suggestions :P 
I don't know at the moment, but is there a way for me to check if the 
var itemResponses = formResponse.getItemResponses();
is returning Form responses? 

Torgeir Helgesen Riseth

unread,
Oct 18, 2021, 11:57:14 AM10/18/21
to Google Apps Script Community
Would it be better if the script would grab the responses from the Sheet, rather than from the Form? Seeing as the script is written in the response sheet's script editor?

Alan Wells

unread,
Oct 18, 2021, 2:13:37 PM10/18/21
to Google Apps Script Community
Yes, it would be much easier to get the answers from the Sheet. The code would be different but it would still need to get all the files from the folder and loop through them. Are you familiar with Apps Script troubleshooting?

Torgeir Helgesen Riseth

unread,
Oct 18, 2021, 2:49:12 PM10/18/21
to Google Apps Script Community
I am not :/ 
I have found a workaround for all future reponses by using Zapier, however I think all past responses might be a lost cause :P

Thanks for helping :) 
Reply all
Reply to author
Forward
0 new messages