title: How to deal with if statement when one Form Response has no response in Google Script?
body: <p>I'm trying to create a script that sends an email when someone submits a google form. The form includes an optional file upload that the script will then attach to the email as a pdf.</p>
  <p>The issue I'm facing is how to ignore the process that creates the attachment if the response is empty.</p>
  <p>Sample code below</p>
  <pre><code>function getIdFrom(url) {
    var id = '';
    var parts = url.split(
      /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/
    );
    if (url.indexOf('?id=') &gt;= 0) {
      id = parts[6].split('=')[1].replace('&amp;usp', '');
      return id;
    } else {
      id = parts[5].split('/');
      var sortArr = id.sort(function (a, b) {
        return b.length - a.length;
      });
      id = sortArr[0];
      return id; //returns google doc id.
    }
  }
  
  function onFormSubmit(response) {
    var link = response.namedValues['Upload file'];
  
    if (typeof link !== &quot;undefined&quot; &amp;&amp; link.length &gt; 0) { // I think it's here that's the issue
      var uploadFileId = getIdFrom(link[0]);
      var uploadFile = DriveApp.getFileById(uploadFileId);
      var uploadFileType = (function () {
        if (uploadFile.getMimeType().includes('image')) {
          return uploadFile.getMimeType();
        } else {
          return 'application/pdf';
        }
       };
      var attachArr = [uploadFile.getAs(uploadFileType)];
    } 
    // etc etc send email.
  }
  </code></pre>
  <p>Works fine if the user submits a form with an uploaded file.</p>
  <p>However if the form is submitted without entering anything in the &quot;Upload File&quot; question, I'm getting a &quot;TypeError: Cannot read property 'split' of undefined&quot; at the <code>getIdFrom(url)</code> function I assume because it's still trying to pass <code>link</code> through <code>getIdFrom()</code> even though it shouldn't because it's undefined.</p>
  <p>Weirdly it works perfectly fine when I use the two test inputs I have, one of which 'Upload File' exists but is empty and the other it doesn't exist at all.</p>
  <p>I'm not sure what I'm missing here.</p>
  <p>Also I have no doubt it's a messy way to do things but I'm getting there.</p>