Replace a null value for one of the questions in a Google Form with AppsScript

31 views
Skip to first unread message

Radha Subramanian

unread,
Feb 22, 2024, 3:31:17 PMFeb 22
to Google Apps Script Community

I have a requirement where I need to get the answer for 2 form questions in a Google Form and send the details in email.

Var Question1 - is a mandatory field and it is a text field so there is no problem.

Var Question 2 - is an optional field (not mandatory) where they can upload documents like picture,pdf,word doc or excel. There is no text.

When answering the form, if I upload a document then I get the email with the document uploaded as a link to the drive. But if I don't upload any document my below code is failing with the below error: when no document uploaded how do I replace that doc with some message like "no document uploaded"

TypeError: Cannot read properties of undefined (reading 'length')

Here is my current code:


function EmailForm() {
var allQuestions,
i,
itemType,
L,
thisAnswer,
Question1,
Question2,
itemType2,
thisAnswer2,
number_of_submissions;

number_of_submissions = FormApp.getActiveForm().getResponses().length;

allQuestions = FormApp.getActiveForm().getResponses()[number_of_submissions - 1].getItemResponses();

L = allQuestions.length;
thisSubmissionsAnswers = [];
Question1 = allQuestions[0];
itemType = Question1.getItem().getType();

if (itemType === FormApp.ItemType.PAGE_BREAK) {

};

thisAnswer = Question1.getResponse().toString();//Get the answer
Logger.log(thisAnswer);


Question2 = allQuestions[2];//Get this question

if (Question2 === "") {

Question2 = "None"
}
else {
itemType2 = Question2.getItem().getType();

if (itemType2 === FormApp.ItemType.PAGE_BREAK) {

};

thisAnswer2 = Question2.getResponse().toString();//Get the answer
Logger.log(thisAnswer2);

let htmlMsg = thisAnswer + "https://drive.google.com/file/d/" + thisAnswer2

if (thisAnswer === 'Yes') {
GmailApp.sendEmail('a...@gmail.com', "Incident Reported", "", { htmlBody: htmlMsg });
}

};`
Reply all
Reply to author
Forward
0 new messages