在 Google 应用程序中使用 HtmlService 表单上传文件总是会导致 "server error" 和堆栈跟踪
Uploading a file using an HtmlService form in Google Apps always causes "server error" and a stack trace
我正在尝试为我的 Google 电子表格制作一个脚本,我在其中上传一个 XML 文件并处理其数据。我能够创建一个表单,在模式对话框中显示它,但是当我尝试提交一个带有文件的表单时出现一个奇怪的错误:Stackdriver 错误报告中没有记录任何错误。但是,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 文档中的示例以及我可以在 Whosebug 上找到的一些旧的和最近的示例,问题总是一样的:什么时候提交表单数据,脚本崩溃。
我知道这是专门由文件输入字段引起的。如果删除它,我就可以提交表单并处理其数据。如果我添加文件输入字段,我会在提交表单时立即收到错误。
我可以看出问题不是文件。我一开始尝试上传一个大的 (125 kb) 文本文件,然后是一个几个字节大小的文件,甚至根本没有提交任何文件,我得到了同样的错误。我在 Chrome 和 Firefox 上都遇到了这个问题,在两个不同的 Google 帐户上。
这是我的 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 脚本时,问题恰恰发生了。我可以在 HTML 中的 google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);
行之前写入控制台,但我没有到达 Google 脚本中的 thisUI.alert("Test");
。如果我从 HTML 中的 digestXml()
中删除参数 objForm
,则不会发生崩溃。
该问题似乎只出现在最近发布的 Google 应用脚本接口 "V8" 版本中。
当我创建脚本时,系统提示我使用他们的这个版本的脚本界面。信任 Google 来测试它们的功能,我想都没想就接受了。
如果我编辑脚本的配置文件以使用 STABLE 而不是 V8,我就不会遇到这个问题。如果您遇到此问题,请按以下步骤操作:
- 打开脚本编辑器。
- 在顶部菜单中,select 查看 > 显示清单文件。
- 在文件列表中,打开
appsscript.json
。
- 将
"runtimeVersion": "V8"
替换为"runtimeVersion": "STABLE"
- 保存。
然而,这令人担忧,因为我认为当前的稳定版本最终将被弃用以支持 V8。我为此记录了一个问题:https://issuetracker.google.com/issues/149980602
我正在尝试为我的 Google 电子表格制作一个脚本,我在其中上传一个 XML 文件并处理其数据。我能够创建一个表单,在模式对话框中显示它,但是当我尝试提交一个带有文件的表单时出现一个奇怪的错误:Stackdriver 错误报告中没有记录任何错误。但是,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 文档中的示例以及我可以在 Whosebug 上找到的一些旧的和最近的示例,问题总是一样的:什么时候提交表单数据,脚本崩溃。
我知道这是专门由文件输入字段引起的。如果删除它,我就可以提交表单并处理其数据。如果我添加文件输入字段,我会在提交表单时立即收到错误。
我可以看出问题不是文件。我一开始尝试上传一个大的 (125 kb) 文本文件,然后是一个几个字节大小的文件,甚至根本没有提交任何文件,我得到了同样的错误。我在 Chrome 和 Firefox 上都遇到了这个问题,在两个不同的 Google 帐户上。
这是我的 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 脚本时,问题恰恰发生了。我可以在 HTML 中的 google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);
行之前写入控制台,但我没有到达 Google 脚本中的 thisUI.alert("Test");
。如果我从 HTML 中的 digestXml()
中删除参数 objForm
,则不会发生崩溃。
该问题似乎只出现在最近发布的 Google 应用脚本接口 "V8" 版本中。
当我创建脚本时,系统提示我使用他们的这个版本的脚本界面。信任 Google 来测试它们的功能,我想都没想就接受了。
如果我编辑脚本的配置文件以使用 STABLE 而不是 V8,我就不会遇到这个问题。如果您遇到此问题,请按以下步骤操作:
- 打开脚本编辑器。
- 在顶部菜单中,select 查看 > 显示清单文件。
- 在文件列表中,打开
appsscript.json
。 - 将
"runtimeVersion": "V8"
替换为"runtimeVersion": "STABLE"
- 保存。
然而,这令人担忧,因为我认为当前的稳定版本最终将被弃用以支持 V8。我为此记录了一个问题:https://issuetracker.google.com/issues/149980602