2

这是我第一次进入Google脚本,我有一个表单调用两个不同的Google应用程序脚本(都在.gs文件中)。一个上传文件,另一个将表单数据保存到谷歌电子表格。由于某种原因,在调用文件上载脚本时出现错误调用脚本时Google App脚本关闭错误

(Uncaught TypeError: Cannot read property 'closure_lm_407634' of null)

尽管上载数据的脚本正常工作。 保存表单数据到电子表格(工作):

google.script.run.withUserObject(data).saveToSheet(data); 

- 这就要求:

function saveToSheet(data) { 
    var date = new Date(); 
    var sheet = SpreadsheetApp.openById(submissionSSKey); 
    sheet 
    .appendRow([date, data.name, data.email, data.headline, 
     data.location, data.newsContent, data.websiteContent, data.expiry, data.fileUrl]); 
} 

上传文件(不工作):

google.script.run 
    .withUserObject(theForm) 
    .withSuccessHandler(processForm) 
    .uploadFile(theForm); 

- 这来电:

function uploadFile(form) { 
    var folder = DriveApp.getFolderById(folderId), doc = '', file = form.uploadedFile; 
    if (file.getName()) { doc = folder.createFile(file); } 
    return doc; 
} 

我不能为了我的生活找出为什么一个电话工作而另一个电话不工作。我尽我所能地想方法来调用上传脚本,但没有任何效果。我试过删除用户对象和成功处理程序。

HTML:

<?!= include('styles'); ?> 
<div id="container" class="col-lg-12 col-md-12 col-sm-12"> 
    <header class="col-lg-offset-3 col-md-offset-3"></header> 
    <div class="col-lg-offset-3 col-lg-6 col-md-6 col-sm-12" id="form"> 
     <h1 class="text-center"> 
      SAS News Submission 
     </h1> 
     <span id="required-content"> 
      <sup>*</sup> 
      Required 
     </span> 
     <br> 
     <br> 
     <form name="sas-form"> 
      <label for="name" class="required">Contact Person/ Source of News</label> 
      <input type="text" name="name" value="test" class="form-control" id="name" required="required"> 
      <br> 
      <label for="email" class="required">Contact Person's email (in case we have questions regarding your News content)</label> 
      <input type="email" name="email" value="[email protected]" id="email" class="form-control" required="required"> 
      <br> 

      <label for="headline" class="required">Headline (try to keep below 10 words if possible) </label> 
      <input type="text" name="headline" value="headline" id="headline" class="form-control" required="required"> 
      <br> 

      <label for="newsContent" class="required">News Content *Note all content must be fully edited to ensure posting</label> 
       <textarea rows="5" cols="0" name="newsContent" class="form-control" id="newsContent" required="required"> 
         Content 
       </textarea> 
      <br> 

      <label class="required">Where would you like the News to be shared? (You may choose more than one)</label> 
      <ul id="social-list"> 
       <li> 
        <input type="checkbox" name="newsletter" id="newsletter" value="newsletter"> 
        <label for="newsletter"> Newsletter</label> 
       </li> 
       <li> 
        <input type="checkbox" name="social" id="social" value="social"> 
        <label for="social"> Social Media (Facebook, LinkedIn, Twitter)</label> 
       </li> 
       <li> 
        <input type="checkbox" name="website" id="website" value="website" checked> 
        <label for="website"> Website </label> 
       </li> 
      </ul> 
      <br> 

      <label for="websiteContent">If you chose the website, please provide specific instructions on where you would like the content to be posted.</label> 
      <br> 
      <small>News and Events Page, Volunteer Page, Student Page, etc. Ex: Please post in the News and Events Page and send the link and headline out on social media.</small> 
      <textarea rows="5" cols="0" name="websiteContent" id="websiteContent" class="form-control">website content</textarea> 
      <br> 
      <label for="expiry">If your content has an expiration date, please share that date below.</label> 
      <input type="date" name="expiry" id="expiry" class="form-control"> 
      <br> 
      <label>If you have files that need to be attached, pick them below.</label> 
      <input type="file" name="uploadedFile" id="file"> 
      <br> 
      <div id="not-valid"><span></span></div> 
      <div id="error"><span> 
       An error occurred, please try submitting again. 
      </span></div> 
      <div id="success"><span> 
      Form submission was successful! Thank you! 
      </span></div> 
      <input type="button" value="Submit" class="btn btn-primary" id="submit" 
        onclick="validateForm(this.parentNode)"> 
     </form> 
    </div> 
</div> 
<footer> 
<?!= include('javascript'); ?> 
</footer> 
<script> 
    var validateForm = function(theForm) 
    { 
     var valid = true; 
     $('#not-valid span').empty(); 
     $('input').removeClass('warning'); 

     if($('#name').val() == '') 
     { 
      $('#name').addClass('warning'); 
      $('#not-valid span').append('Please enter a name <br>'); 
      valid = false; 
     } 

     if($('#email').val() == '') 
     { 
      $('#email').addClass('warning'); 
      $('#not-valid span').append('Please enter an email <br>'); 
       valid = false; 
     } 

     if($('#headline').val() == '') 
     { 
      $('#headline').addClass('warning'); 
      $('#not-valid span').append('Please enter a headline <br>'); 
      valid = false; 
     } 

     if($('#newsContent').val() == '') 
     { 
      $('#newsContent').addClass('warning'); 
      $('#not-valid span').append('Please enter news content <br>'); 
      valid = false; 
     } 

     if(!$('#social').is(':checked') && !$('#website').is(':checked') && !$('#newsletter').is(':checked')) 
     { 
      $('#not-valid span').append('Please choose where you would like the news to be shared. <br>'); 
      $('#social-list').addClass('warning'); 
      valid = false; 
     } 


     if(valid) 
     { 
      google.script.run.withSuccessHandler(processForm).uploadFile(theForm) 

     } 
    }; 

    function processForm(file) 
    { 
     var fileUrl = file ? file.getUrl() : 'No file uploaded', 
     location = []; 

     if($('#social').is(':checked')) 
     { 
      location.push('social'); 
     } 

     if($('#newsletter').is(':checked')) 
     { 
      location.push('newletter'); 
     } 

     if($('#website').is(':checked')) 
     { 
      location.push('website'); 
     } 

     var data = { 
     name:   $('#name').val(), 
     email:   $('#email').val(), 
     headline:  $('#headline').val(), 
     location:  location.toString(), 
     newsContent: $('#newsContent').val(), 
     websiteContent: $('#websiteContent').val(), 
     expiry:   $('#expiry').val() ? $('#expiry').val() : 'No expiration date selected', 
     fileUrl:  fileUrl 
     }; 

     google.script.run.saveToSheet(data);   

     clearForm(); 
     success(); 
    }; 

    var clearForm = function() 
    { 
     $("input[type=text], input[type=date], textarea, input[type=email], input[type=file]").val(""); 
     $("input[type=checkbox]").attr('checked', false); 
     enableSubmit(); 
    }; 

    var success = function() 
    { 
     $('#success').show() 
    }; 

    var enableSubmit = function() 
    { 
     $("#submit").prop('disabled', false); 
    }; 
</script> 
+0

DOM可能尚未设置(请参阅:http://stackoverflow.com/questions/20613181/cannot-read-property-of-null)。将javascript部分的方式放在底部或在DOM加载后使用'DOMContentLoaded'技术加载JS并再次尝试。 –

+0

请发表格式HTML,以及如何获取'data'和'theForm'。 –

+0

Logger.log(“file Name:”+ file.getName()) Logger.log(“file type:”+ file)您可以通过在服务器代码中放入一些日志语句来测试文件是否正在传递。 .getContentType()) 记录器。log('file:'+ file);'如果文件真的被传递了,那么应该有一个名字和内容类型。如果没有收到文件,那么客户端有问题。 –

回答

1

我能够重现你的错误。我不知道为什么会出现这种错误,但我找到了一种方法来使其工作。

以下是你需要做的:

把一个id属性到上表单标签:

<form id="myForm"> 
  • 使用输入标签删除按钮。
  • 添加一个<button>标签外部的形式。必须在表单之外。并获得形式document.getElementById('myForm')

    <form id="myForm"> 
        <input type="file" name="uploadedFile"> 
    </form> 
    
    <button onclick="validateForm(document.getElementById('myForm'))">submit</button> 
    

我测试过这一点。它获得了该文件,并将其发送到表单元素的服务器中。

可以在服务器代码中使用Logger.log()而不使用调试器。

function uploadFile(form) { 
    Logger.log('form: ' + form); 
    Logger.log('form.uploadedFile: ' + form.uploadedFile); 
    Logger.log('form.uploadedFile: ' + form.uploadedFile.getName()); 
+0

非常感谢,这是一个很大的帮助! –

+0

谢谢你让我知道,并投票。 –