Google 表格导入 CSV 弹出窗口
目标:您想要通过从 Google 电子表格的自定义菜单执行脚本来将 CSV 数据上传到活动工作表。
示例脚本: Google Apps 脚本端:Code.gs 请将以下脚本作为脚本复制并粘贴到脚本编辑器中。
function onOpen() {
SpreadsheetApp.getUi().createMenu("sample").addItem("import CSV", "importCsv").addToUi();
}
function importCsv(e){
if (!e) {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("index"), "sample");
return;
}
const csv = Utilities.parseCsv(Utilities.newBlob(...e).getDataAsString());
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
}
HTML 和 Javascript 端:index.html 请将以下脚本以 HTML 格式复制并粘贴到脚本编辑器中。
<form><input type="file" name="file" onchange="importCsv(this.parentNode)" accept=".csv,text/csv"></form>
<script>
function importCsv(e) {
const file = e.file.files[0];
const f = new FileReader();
f.onload = d => google.script.run.withSuccessHandler(google.script.host.close).importCsv([[...new Int8Array(d.target.result)], file.type, file.name]);
f.readAsArrayBuffer(file);
}
</script>
使用脚本,当我单击所需的自定义菜单项时,会出现示例弹出窗口,我可以选择该文件,但在选择它后,我使用系统文件浏览器,我只会看到以下屏幕,并且无法知道是否上传成功或正在进行中,但最终,如果您等待足够长的时间,弹出窗口会自行关闭,并且 csv 信息已成功上传。我只想让弹出窗口显示导入进度的更多信息 。
Goal: You want to upload a CSV data to the active sheet by executing the script from the custom menu of Google Spreadsheet.
Sample script:
Google Apps Script side: Code.gs
Please copy and paste the following script to the script editor as a script.
function onOpen() {
SpreadsheetApp.getUi().createMenu("sample").addItem("import CSV", "importCsv").addToUi();
}
function importCsv(e){
if (!e) {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("index"), "sample");
return;
}
const csv = Utilities.parseCsv(Utilities.newBlob(...e).getDataAsString());
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);
}
HTML and Javascript side: index.html
Please copy and paste the following script to the script editor as a HTML.
<form><input type="file" name="file" onchange="importCsv(this.parentNode)" accept=".csv,text/csv"></form>
<script>
function importCsv(e) {
const file = e.file.files[0];
const f = new FileReader();
f.onload = d => google.script.run.withSuccessHandler(google.script.host.close).importCsv([[...new Int8Array(d.target.result)], file.type, file.name]);
f.readAsArrayBuffer(file);
}
</script>
With the script as it is the sample popup windows appears when I click on the desired custom menu item, i can choose the file but after selecting it I with the system file browser I just get the following screen and there is no way of knowing if the upload worked or is in progress, but eventually if you wait long enough the popup window closes itself and the csv information is successfully uploaded. I just want for the popup window to show more info of the importing progress
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为一个简单的示例,根据您的情况,进行以下修改如何?
修改后的脚本:
请按如下方式修改您的 HTML。
在此修改中,当选择文件时,
Waiting
的值更改为Uploading...
。并且,当文件上传完成后,会显示Done
并关闭对话框。请根据您的情况修改文本。
As a simple sample, in your situation, how about the following modification?
Modified script:
Please modify your HTML as follows.
In this modification, when the file is selected, the value of
Waiting
is changed toUploading...
. And, when the file upload is done,Done
is shown and the dialog is closed.Please modify the texts for your situation.
当尝试加载更大的 CSV 数据集(3k+ 行)时,我注意到方法“.getDataAsString());” +“.setValues(csv);”执行几分钟后就会失败。
相反,我使用“.batchUpdate()”方法以最短的运行时间一次上传 15k+ 行。
下面是一个示例:
您只需确保在批量更新之前在工作表中添加足够的行数。
When trying to load larger CSV datasets (3k+ rows), I noticed the method ".getDataAsString());" + ".setValues(csv);" will simply fail after a few minutes of execution.
Instead, I used the ".batchUpdate()" method to upload 15k+ rows at once with minimal runtime.
Below is an example:
You just have to make sure to add the adequate number of rows in the sheet prior to batch update.