javascript - 在 Google Apps 中使用 HtmlService 表单上传文件总是会导致“服务器错误”和堆栈跟踪
问题描述
我正在尝试为我的 Google 电子表格制作一个脚本,在其中上传 XML 文件并处理其数据。我可以创建一个表单,在模式对话框中显示它,但是当我尝试提交带有文件的表单时出现一个奇怪的错误:Stackdriver Error Reporting 中的错误没有记录。但是,Web 浏览器控制台会记录以下错误消息:
Error: We're sorry, a server error occurred. Please wait a bit and try again.
错误消息带有堆栈跟踪:
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 文件。
我已经尝试复制当前 Google Apps 文档中的示例以及我可以在 StackOverflow 上找到的一些旧的和最近的示例,但问题总是相同的:当提交表单数据的时候,脚本崩溃了。
我知道这是专门由文件输入字段引起的。如果我删除它,我就可以提交表单并处理其数据。如果我添加文件输入字段,我会在提交表单后立即收到错误消息。
我可以告诉问题不是文件。我首先尝试上传一个大的(125 kb)文本文件,然后是几个字节大小的文件,甚至根本没有提交任何文件,我得到了同样的错误。我在 Chrome 和 Firefox 上都遇到了这个问题,在两个单独的 Google 帐户上。
这是我的谷歌脚本。updateTracker
当单击我放置在电子表格中的绘图对象时调用该方法。
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”:
<!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 脚本时,问题就发生了。我可以google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);
在 HTML 中的行之前写入控制台,但我无法thisUI.alert("Test");
使用 Google 脚本。如果我objForm
从digestXml()
HTML 中删除参数,则不会发生崩溃。
解决方案
该问题似乎仅出现在最近发布的 Google App 脚本界面“V8”版本中。
当我创建脚本时,系统会提示我使用他们的脚本界面的这个版本。相信谷歌会测试他们的功能,我不假思索地接受了。
如果我编辑脚本的配置文件以使用 STABLE 而不是 V8,则不会遇到此问题。如果您遇到此问题,请按以下步骤操作:
- 打开脚本编辑器。
- 在顶部菜单中,选择查看 > 显示清单文件。
- 在文件列表中,打开
appsscript.json
. - 替换
"runtimeVersion": "V8"
为"runtimeVersion": "STABLE"
- 节省。
然而,这令人担忧,因为我认为当前的稳定版本最终将被弃用,取而代之的是 V8。我为此记录了一个问题:https ://issuetracker.google.com/issues/149980602
推荐阅读
- java - 在 application.yml 中使用 URI Documentdb java spring boot
- scala - 使用 Spark 中的地图将日期转换为字符串不起作用?
- xcode - 不是在正确的时间执行函数,而是在完成块之后执行
- javascript - 如何更改对象中javascript函数的值
- nestjs - 即使端点不存在,NestJS 也总是返回 200
- python - 在对函数结果使用类型提示时,我应该使用“预期”类型吗?
- c# - TensorFloat .netcore (CustomVision) 的问题
- font-awesome - 使字体真棒图标具有相同的宽度
- c# - System.Timers.Timer 有时会停止,然后在一段时间后恢复
- amazon-web-services - AWS Lambda:在 VPC 之外调用另一个 REST API