我正在尝试为我的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)的文本文件,然后再上传一个几个字节的文件,甚至根本不提交任何文件,但遇到同样的错误。我在两个单独的Google帐户的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脚本时才出现此问题。我可以在HTML的google.script.run.withSuccessHandler(updateUrl).digestXml(objForm);
行之前向控制台写入内容,但在Google脚本中却无法访问thisUI.alert("Test");
。如果从HTML的objForm
中删除参数digestXml()
,则不会发生崩溃。
看来,该问题仅出现在Google App脚本接口的最新发布版本“ V8”中。
创建脚本时,系统会提示我使用此版本的脚本界面。我信任Google来测试其功能,我没有考虑就接受了。
如果我编辑脚本的配置文件以使用STABLE而不是V8,则不会遇到此问题。如果您遇到此问题,请按以下步骤操作:
appsscript.json
。"runtimeVersion": "V8"
替换"runtimeVersion": "STABLE"
然而,这令人震惊,因为我认为当前最终稳定的版本将被V8弃用。我为此记录了一个问题:https://issuetracker.google.com/issues/149980602