undefined想要使用谷歌表单脚本显示空白

时间:2013-11-27 22:17:52

标签: google-apps-script google-form

我正在使用脚本,当google form response =“”时,如果没有任何内容,我希望它显示空白而不是未定义。这是下面的脚本

    function onSubmit(e) {
  var myemail = "user@example.com"  // please, replace myemail by your email 
    try{
      var formResponse = e.response;//get the form response
      var editResponseUrl = formResponse.getEditResponseUrl(); //get the individual form url just in case the respondent needs to edit its anwers later
      var itemResponses = formResponse.getItemResponses();// get all item questions and anwers
      var itemResponse
      var title
      var response
      for (var i = 0; i < itemResponses.length; i++) {    // iterate for all form items
        itemResponse = itemResponses[i];
        title = itemResponse.getItem().getTitle().toUpperCase(); // get the question title. In our form can be EMAIL or NAME
        response =  itemResponse.getResponse().toString();  // get the corresponding answer
        if(title.indexOf("AUDITOR", 0) != -1){   // if the title is "AUDITOR NAME"
          var auditor = response.toUpperCase();  // get it
        }
        else if (title.indexOf("JCI", 0) != -1){  // if the title is "STATION NAME"
          var work = response.toUpperCase(); // get it
        }
        else if (title.indexOf("RELATED", 0) != -1){  // if the title is "ISSUES FOUND"
          var found = response.toUpperCase(); // get it
        }
        else if (title.indexOf("DEPARTMENT", 0) != -1){  // if the title is "DEPARTMENT RESPONSIBLE"
          var departments = response.toUpperCase(); // get it
        }
        else if (title.indexOf("RESULTS", 0) != -1){  // if the title is "NAME"
          var results = response.toUpperCase(); // get it
        }
      }
      var subject = results + ",\n\n"; // body of the message you're assembling
      subject += departments + ",\n\n"; // body of the message you're assembling
      var message = auditor + ",\n\n"; // body of the message you're assembling
      message += "Just Completed Station Audit \n\n"; // body of the message you're assembling
      message += work + ",\n\n"; // body of the message you're assembling
      message += "FAILED AUDIT ISSUES FOUND:\n\n"; // body of the message you're assembling
      message += found + ",\n\n"; // body of the message you're assembling
      message += "Please Assign A Champion:\n\n"; // body of the message you're assembling
      message += "Click On The Link Below To Assign Ownership Responsibilites:\n\n"; // body of the message you're assembling
      message += editResponseUrl + "\n\n"; // body of the message you're assembling
      MailApp.sendEmail("miltondatabase@gmail.com", subject, message); // send the message
  }
  catch(e) {             // if something wrong happens 
      MailApp.sendEmail(myemail, "Error in Auto replying to contact form submission.", e.message); // tell me
  }
}

1 个答案:

答案 0 :(得分:0)

您可以验证内容并指定新值。

尝试类似:

...
response = itemResponse.getResponse().toString();  // get the corresponding answer
if (typeof response === 'undefined') response = '';
...