在Google Apps中使用HtmlService表单上传文件时,始终会导致“服务器错误".和堆栈跟踪 [英] Uploading a file using an HtmlService form in Google Apps always causes "server error" and a stack trace

查看:63
本文介绍了在Google Apps中使用HtmlService表单上传文件时,始终会导致“服务器错误".和堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Google电子表格创建一个脚本,在该脚本中我上载XML文件并处理其数据.我能够创建一个表单,并在模式对话框中显示它,但是当我尝试使用文件提交表单时出现一个奇怪的错误:Stackdriver Error Reporting中未记录任何错误.但是,Web浏览器控制台记录以下错误消息:

I am trying to make a script for my Google spreadsheet in which I upload an XML file and process its data. I am able to create a form, display it in a modal dialog, but I get a strange error when I attempt to submit a form with a file: Nothing is logged for the error in Stackdriver Error Reporting. However, the web browser console logs the following error message:

Error: We're sorry, a server error occurred. Please wait a bit and try again. 

该错误消息带有堆栈跟踪:

The error message comes with a stack trace:

Zd https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:56
    bf https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:71
    G https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:15
    J https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:99
    Id https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:47
    Ed https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:48
    b https://n-z7hx4jjtvixobmaqkddve7tkcdyndjsnh3plmfq-0lu-script.googleusercontent.com/static/macros/client/js/2745927008-mae_html_user_bin_i18n_mae_html_user__en_gb.js:44

当然,堆栈跟踪在这里无济于事,因为它指向Google服务器上的一个精简的JavaScript文件.

Of course, the stack trace doesn't help here, as it points to a huge minified JavaScript file on Google's servers.

我尝试复制当前Google Apps文档中的示例以及我可以在StackOverflow上找到的一些旧的和最近的示例,但问题始终是相同的:提交表单数据的时间到了,脚本崩溃.

I have tried replicating the examples in the current Google Apps documentation as well as a few old and recent examples I could find on StackOverflow, and the issue is always the same: When comes the time to submit the form data, the script crashes.

我知道这是由文件输入字段引起的.如果删除它,则可以提交表单并处理其数据.如果添加文件输入字段,则在提交表单后立即收到错误消息.

I know that this is specifically caused by the file input field. If I remove it, I'm able to submit the form and process its data. If I add the file input field, I get the error as soon as I submit the form.

我可以说问题出在文件之外.我尝试过先上传一个大(125 kb)的文本文件,然后再上传一个几个字节的文件,甚至根本不提交任何文件,但遇到同样的错误.我在两个单独的Google帐户上的Chrome和Firefox上都遇到了此问题.

I can tell the issue is not the file. I have tried uploading a big (125 kb) text file at first, followed by one a few bytes in size, and even not submitting any file at all, and I get the same error. I'm encountering this issue on both Chrome and Firefox, on two separate Google accounts.

这是我的Google脚本.当单击我放在吊架中的图形对象时,会调用updateTracker方法.

Here is my Google script. The updateTracker method is called when clicking on a drawing object that I placed in the spreadhseet.

function updateTracker()
{  
  var thisUI = SpreadsheetApp.getUi();
  var htmlUpdatePage = HtmlService.createHtmlOutputFromFile('myPage');
  var updatePrompt = thisUI.showModalDialog(htmlUpdatePage, 'Update');
}

function digestXml(theForm) {
  //var fileBlob = theForm.xmlFile;
  var thisUI = SpreadsheetApp.getUi();
  thisUI.alert("Test");
}

这是我的HTML文件"myPage":

Here is my HTML file, "myPage":

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      // Prevent forms from submitting.
      function preventFormSubmit() {
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
          forms[i].addEventListener('submit', function(event) {
            event.preventDefault();
          });
        }
      }
      window.addEventListener('load', preventFormSubmit);     

      function submitXml(objForm)
      {
        google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);
      }
      function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = 'Got it!';
      }
    </script>

  </head>
  <body>
    <form id="xmlForm" onsubmit="submitXml(this)">    
      <input type="file" value="Browse" name="xmlFile" />
      <input type="submit" value="Digest" />
    </form>
    <div id="output"></div>
  </body>
</html>

我可以说,正是在尝试将objForm从HTML传递到Google脚本时才出现此问题.我可以在HTML的google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);行之前向控制台写入内容,但是在Google脚本中却无法访问thisUI.alert("Test");.如果我从HTML的digestXml()中删除参数objForm,则不会发生崩溃.

I can tell that the issue occurs precisely when trying to pass objForm from the HTML to the Google Script. I'm able to write to the console right before the line google.script.run.withSuccessHandler(updateUrl).digestXml(objForm); in HTML, but I don't get to thisUI.alert("Test"); in the Google Script. If I remove the parameter objForm from digestXml() in the HTML, the crash does not occur.

推荐答案

看来,该问题仅出现在Google App脚本接口"V8"的最新版本中.

It appears that the issue only occurs in a recently released version of Google App's scripting interface, "V8".

创建脚本时,系统会提示我使用此版本的脚本界面.我信任Google来测试其功能,因此我不加思索地接受了.

When I create a script, I am prompted to use this version of their scripting interface. Trusting Google to test their functionalities, I accepted without thinking.

如果我编辑脚本的配置文件以使用STABLE而不是V8,则不会遇到此问题.如果您遇到此问题,请按以下步骤操作:

If I edit my script's configuration file to use STABLE instead of V8, I do not encounter the issue. If you're having this issue, here's how to do that:

  1. 打开脚本编辑器.
  2. 在顶部菜单中,选择查看">显示清单文件".
  3. 在文件列表中,打开appsscript.json.
  4. "runtimeVersion": "V8"替换为"runtimeVersion": "STABLE"
  5. 保存.
  1. Open the Script Editor.
  2. In the top menu, select View > Show manifest file.
  3. In the files list, open appsscript.json.
  4. Replace "runtimeVersion": "V8" with "runtimeVersion": "STABLE"
  5. Save.

但是这令人震惊,因为我认为当前最终稳定的版本将被V8弃用.我为此记录了一个问题: https://issuetracker.google.com/issues/149980602

This is however alarming as I presume the current stable version will be deprecated eventually in favor of V8. I logged an issue for this: https://issuetracker.google.com/issues/149980602

这篇关于在Google Apps中使用HtmlService表单上传文件时,始终会导致“服务器错误".和堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆