谷歌应用程序脚本服务器端无法访问HTML服务表单中提交的文件



我正在开发一个CSV导入器,以接受三个提供商中任何一个提供的CSV文件,并将其转换为另一个软件(CoinTracker(所需的格式。这些文件是使用我在谷歌应用程序脚本中创建的表单从我的本地驱动器上传的。上传命令可以作为附加组件从Google Sheets中访问。

这个问题的答案必须非常接近谷歌应用程序脚本HTML表单获胜';t提交,因为那里的OP似乎也试图遵循这篇文章。

其中一个供应商是Swissborg,这是我首先开发的一个。我面临的问题是,在我选择要上传的文件并单击"导入"后,什么都不会发生。我已经尝试使用Logger来获取任何发生的迹象(正如您在我的代码中看到的那样(。Executions报告没有为processForm生成一行。控制台错误似乎并不相关,它们是:传输或处理过程中出错…此请求。错误代码=10,路径=/wardenit获取chrome-extension://invalid/net::ERR_FAILED

如果能给我一个好的答案或故障排除建议,我将不胜感激!

编辑:在底部提供了一些新信息

代码.gs

* @OnlyCurrentDoc
*/
function onOpen(e) {
const ui = SpreadsheetApp.getUi();
ui.createMenu("📄 Import transactions")
.addItem("Import Swissborg transactions", "importSwissborgCsv")
.addToUi();
}
function importSwissborgCsv() {
displayImportDialog();
}
function displayImportDialog() {
const ui = SpreadsheetApp.getUi();
let htmlOutput = HtmlService
.createHtmlOutputFromFile('importDialog.html')
.setWidth(480)
.setHeight(236);
ui.showModalDialog(htmlOutput, "Import transactions");
}
function processForm(formObject) {
Logger.log('Processing the form...');
Logger.log('The object is: ', formObject);
let formBlob = formObject.file;
let tempSheet = SpreadsheetApp.create();
let csv = formBlob.getAs("text/csv");
}

importDialog.html

<html>
<script>
function preventFormSubmit() {
let forms = document.querySelectorAll('importForm');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener('importForm', preventFormSubmit);
function handleFormSubmit() {
const form = document.getElementById("importForm");
google.script.run.withFailureHandler(onFailure).processForm(form);
}
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.message;
}
</script>
<head>
<!-- <link rel="stylesheet" href="xxx"> -->
</head>
<body>
<p>File must be in CSV format</p>
<div id="output"></div>
<form id="importForm">
<input type="file" name="file" id="file" />
<input type="submit" value="Import" />
<input type="button" value="Close" onclick="google.script.host.close();" />
</form>
</body>
</html>

链接到谷歌工作表我正在

示例CSV我正在尝试导入

问题很简单,因为我没有使用onsubmit属性指定表单在单击提交按钮时应该采取的操作。

因此,表单的HTML现在是:

<form id="importForm" onsubmit="handleFormSubmit()">
<input type="file" name="file" id="file" />
<input type="submit" value="Import" />
<input type="button" value="Close" onclick="google.script.host.close();" />
</form>

最新更新