调用脚本时出现Google App Script Closure错误

时间:2016-01-28 23:35:36

标签: javascript google-apps-script google-sheets google-apps

这是我第一次涉足Google脚本,我有一个表单可以调用两个不同的Google应用脚本(两者都在.gs文件中)。 One上传文件,而另一个文件将表单数据保存到Google电子表格。出于某种原因,我在调用文件上传脚本时遇到错误

  

(未捕获的TypeError:无法读取属性' closure_lm_407634'为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="me@me.com" 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>

1 个答案:

答案 0 :(得分:1)

我能够重现你的错误。我不知道为什么会出现这种错误,但我找到了一种方法让它发挥作用。

以下是您需要做的事情:

将id属性放入上部表单标记:

<form id="myForm">
  • 使用输入标记删除按钮。
  • 在表单中添加<{1}}标记 。必须在表格之外。并使用<button>

    获取表单
    document.getElementById('myForm')

我已经测试了这个。它获取了文件,并将其发送到表单元素内的服务器。

可以在服务器代码中使用<form id="myForm"> <input type="file" name="uploadedFile"> </form> <button onclick="validateForm(document.getElementById('myForm'))">submit</button> 而无需使用调试器。

Logger.log()
相关问题