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 undefinedon 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?