0

Totally new to javascript, but I'm able to hack together existing pieces. I'm a bit stuck now.

I have a script that joins together a bunch of Q + As from a Google Form. A lot of the questions are optional, but I'm not sure how to skip those lines, so it still prints the questions and line breaks.

var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();

// ---------

// create the notes portion of the form
var notes = "XYZ Submission \n\n";

for (var j = 0; j < itemResponses.length; j++) {
  var itemResponse = itemResponses[j];

// add each Q & A to the notes and then add 2 endlines if the response isn't blank
  if (itemResponse.getResponse() != null) {
notes = notes + itemResponse.getItem().getTitle() + "\n" + itemResponse.getResponse() + "\n\n";
} 
}

What am I doing wrong here?

jessica
  • 25
  • 2

2 Answers2

0

The responses to the unanswered optional questions are not null; they are blank.

So try replacing this:

if (itemResponse.getResponse() != null) {

with this:

if (itemResponse.getResponse() != '') {
ADW
  • 4,177
  • 1
  • 14
  • 22
0

Ok I think I found the answer! I actually didn't find what kind of value is in the variable, but I arealized I could just check if it had a truthy value or not.

So this worked

var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();

// ---------

// create the notes portion of the form
var notes = "XYZ Submission \n\n";

// cycle through all form responses and add them all to the notes variable
for (var j = 0; j < itemResponses.length; j++) {
  var itemResponse = itemResponses[j];
  if (itemResponse.getResponse()) {
    console.log(itemResponse.getResponse());
notes = notes + itemResponse.getItem().getTitle() + "\n" + itemResponse.getResponse() + "\n\n";
} 
}
jessica
  • 25
  • 2