Google应用程式指令码:从html服务上载csv档案 [英] Google app script: upload a csv file from html service
问题描述
我正在尝试从Google表格中的html服务中上传一个csv文件,经过一番研究后,我发现一些当时似乎可以正常工作的代码:
I'm trying to upload a csv file from an html service in google sheets, and after some research I found some code which seemed to be working at the time:
html服务呼叫:
function importAnalysis() {
var html = HtmlService.createHtmlOutputFromFile('Import')
.setWidth(1524)
.setHeight(800);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Import d\'analyse');
}
html模板:
<!DOCTYPE html>
<html>
<body>
<form>
<input type="file" name="analysisCsv" accept=".csv">
<input type="button" onclick="google.script.run.processForm(this.parentNode);">
</form>
</body>
</html>
gs文件(我评论了进一步的代码以找出问题的根源):
gs file (I commented further code to isolate the source of the problem):
function processForm(form) {
let fileBlob=form.analysisCsv;
// sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Table_Analysis");
// let lastRow = sheet.getLastRow();
// let values = []
// let rows = fileBlob.contents.split('\n');
// for(let r=0; r<rows.length; ++r){
// values.push( rows[r].split(';') );
// }
// for (var i = 0; i < values.length; i++) {
// sheet.getRange(lastRow+i+1, 1, 1, values[i].length).setValues(new Array(values[i]));
// }
}
问题是我在processForm函数上收到错误400: 无法加载资源:服务器响应状态为400()"
Problem is I get an error 400 on processForm function: "Failed to load resource: the server responded with a status of 400 ()"
您知道我的代码有什么问题,还是在工作表中上传csv内容的另一种方法? 预先感谢.
Do you know what's wrong with my code, or another way to upload csv content in my sheet? Thanks in advance.
推荐答案
总而言之,我不得不强制执行Legacy运行时: 选择运行">禁用由V8驱动的新Apps脚本运行时". 然后编辑所有V8语法:主要用"var"替换"let". 现在一切正常,感谢所有人.
So to sum it up, I had to force Legacy runtime: Select Run > Disable new Apps Script runtime powered by V8. Then edit all the V8 syntax:replace "let" by "var" mainly. Now it's working, thanks to all.
请参阅 https://developers.google.com/apps-script/guides/v8运行时/迁移 https://developers.google.com/apps-script/guides/v8-runtime#enabling_the_v8_runtime
这篇关于Google应用程式指令码:从html服务上载csv档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!