I’m not sure what I’m doing wrong but my best guess is that I’m handling the attachment wrong (although it only comes through as an ID and I append that too https://drive.google.com/open?id=ID_HERE). I have a Google Form where the only question is an attachment. Everything works fine when an attachment is added but when it’s not I get the following error:
ReferenceError: attachment is not defined
at eval([unknown file]:8:60)
at eval([unknown file]:14:3)
at myFunction(Code:21:22)
I’ve tried declaring attachment independently and get the same error
Here is my gs and hmtl files
Code.gs
function myFunction(e) {
var formResponses = e.response,
itemResponses = formResponses.getItemResponses();
var attachment = "",
template = HtmlService.createTemplateFromFile("index"),
message = "";
for (x = 0; x < itemResponses.length; x++) {
if (itemResponses[x].getItem().getTitle() === "Attachment") {
if (itemResponses[x].getResponse() === undefined) {
attachment = "No file was attached";
template.attachment = attachment;
}
else {
Logger.log("Made it here");
attachment = "https://drive.google.com/open?id=" + itemResponses[x].getResponse();
template.attachment = attachment;
}
}
}
message = template.evaluate().getContent();
Logger.log(message);
}
index.html
<p>Hello,</p> <strong>Attachment: </strong><p><?= attachment ?></p>
Thank you for your time and any assistance in advance!