1
votes

I'm running into two issues when working with upload fields in google script. I will try to describe my issue as best as I can.

I have a form, with a trigger set for when its sent. This trigger calls a main function that in return calls some other functions.

One of the subfunctions creates a doc, then sets its link on a specific sheet.

The main function gets the item responses object in the following line:

var responses = e.response.getItemResponses();

Then, this object is passed to the Doc function as an argument:

generateBriefingPDF(classID, briefingSheet, responses, timestamp);

ClassID is a simple integer property, briefingSheet is the destination sheet and timestamp I get from:

var timestamp = e.response.getTimestamp();

Inside the generateBriefingPDF script, I get the responses and assign them to easier-to-read variables like so:

var name = responses[0].getResponse();
var subject = responses[1].getResponse();
var content = responses[2].getResponse();
var level = responses[3].getResponse();
var uploads = responses[4].getResponse();

Then I replace text 'variables' in a new doc copied with a template with the variables.

I also set them in the sheet.

The "uploads" var is the id of the file, so I convert it to the link using:

uploads = DriveApp.getFileById(uploads).getUrl();

This works fine. Except for the "uploads" variable, from the response[4], which is an upload field. The following occurs:

  • When nothing is uploaded: The variable is undefined, so it cannot run the property "getResponse". I get the error TypeError: Cannot read property 'getResponse' of undefined on the Stackdriver log. I have tried to check for undefined with the following lines, none worked:

    if(uploads !== undefined) if(typeof uploads !== "undefined")

  • One file is uploaded: Everything works.

  • More than one file is uploaded: I get an error when trying to get the file by the ID (of course): Exception: Unexpected error while getting the method or property getFileById on object DriveApp. When I didn't have the line to convert the ID to the link, the IDs were separated by a comma in the doc file, but only the first one appeared in the sheet.

What can I do to solve those two issues?

1
I believe you are using Google Forms with onFormSubmit Trigger. Is that correct? - Cooper
@Cooper Yes. That is correct. - Set

1 Answers

1
votes

I believe your goal as follows.

  • You want to modify your script for the cases that a file and/or multiple files are uploaded using Google Form.

Modification points:

From your script, I thought that your script might be the container-bound script of Google Form. And from More than one file is uploaded, I think that uploads of var uploads = responses[4].getResponse() is an array including the file IDs of uploaded files. In this case, responses[4] for each situation becomes as follows.

  • When the file is not uploaded, responses[4] is undefined.
    • This has already been mentioned in your question.
  • When a file and/or multiple files are uploaded, responses[4] is the object of ItemResponse and responses[4].getResponse() is an array including the file IDs of uploaded files.
    • When one file is uploaded, var uploads = responses[4].getResponse() is like ["###fileId###"]. In this case, DriveApp.getFileById(uploads) works. I think that uploads.toString() might be used for this situation. But, when multiple files are uploaded, the file ID is not existing. By this, the error occurs.
    • About the issue in More than one file is uploaded, I thought that this is the reason of your issue.

I think that in order to achieve your goal, above situation can be used. When your script is modified, it becomes as follows.

Modified script:

var uploads = responses[4].getResponse();
var uploads = "";
if (responses[4]) {  // or if(uploads !== undefined) if(typeof uploads !== "undefined")
  uploads = responses[4].getResponse().map(id => DriveApp.getFileById(id).getUrl()).join(",");
}

Note:

  • In this modification, when one file is uploaded, uploads is one URL. For example, when 2 files are uploaded, uploads is a string value including 2 URLs like URL1,URL2. If you want to use it as an array, please modify to uploads = responses[4].getResponse().map(id => DriveApp.getFileById(id).getUrl()).
  • When no files are uploaded, uploads is "".

References: