Google 表格导入 CSV 弹出窗口

发布于 2025-01-10 14:34:14 字数 1562 浏览 1 评论 0原文

目标:您想要通过从 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 信息已成功上传。我只想让弹出窗口显示导入进度的更多信息 随着当我单击所需的自定义菜单项时,会出现示例弹出窗口,我可以选择该文件,但在选择它后,我使用系统文件浏览器,只看到以下屏幕,并且无法知道是否上传工作或正在进度,但最终如果您等待足够长的时间,弹出窗口会自行关闭,并且 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
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾`听者〃 2025-01-17 14:34:14

作为一个简单的示例,根据您的情况,进行以下修改如何?

修改后的脚本:

请按如下方式修改您的 HTML。

<form>
  <input type="file" name="file" onchange="importCsv(this.parentNode)" accept=".csv,text/csv">
  <div id="progress">Waiting</div>
</form>
<script>
function importCsv(e) {
  const div = document.getElementById("progress");
  div.innerHTML = "Uploading...";
  const file = e.file.files[0];
  const f = new FileReader();
  f.onload = d => google.script.run.withSuccessHandler(_ => {
    div.innerHTML = "Done";
    setTimeout(google.script.host.close, 1000);
  }).importCsv([[...new Int8Array(d.target.result)], file.type, file.name]);
  f.readAsArrayBuffer(file);
}
</script>
  • 在此修改中,当选择文件时,Waiting 的值更改为Uploading...。并且,当文件上传完成后,会显示 Done 并关闭对话框。

  • 请根据您的情况修改文本。

As a simple sample, in your situation, how about the following modification?

Modified script:

Please modify your HTML as follows.

<form>
  <input type="file" name="file" onchange="importCsv(this.parentNode)" accept=".csv,text/csv">
  <div id="progress">Waiting</div>
</form>
<script>
function importCsv(e) {
  const div = document.getElementById("progress");
  div.innerHTML = "Uploading...";
  const file = e.file.files[0];
  const f = new FileReader();
  f.onload = d => google.script.run.withSuccessHandler(_ => {
    div.innerHTML = "Done";
    setTimeout(google.script.host.close, 1000);
  }).importCsv([[...new Int8Array(d.target.result)], file.type, file.name]);
  f.readAsArrayBuffer(file);
}
</script>
  • In this modification, when the file is selected, the value of Waiting is changed to Uploading.... And, when the file upload is done, Done is shown and the dialog is closed.

  • Please modify the texts for your situation.

红墙和绿瓦 2025-01-17 14:34:14

当尝试加载更大的 CSV 数据集(3k+ 行)时,我注意到方法“.getDataAsString());” +“.setValues(csv);”执行几分钟后就会失败。

相反,我使用“.batchUpdate()”方法以最短的运行时间一次上传 15k+ 行。

下面是一个示例:

var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getSheetByName('Dataset'),
file = Utilities.newBlob(...e);

var string = file.getDataAsString();
var csvRowsNo = string.match(/(?:"(?:[^"]|"")*"|[^,\n]*)(?:,(?:"(?:[^"]|"")*"|[^,\n]*))*\n/g).length;

sheet.insertRowsAfter(3, csvRowsNo);

var resource = {
  requests: [{pasteData: { data: string, coordinate: {sheetId: sheet.getSheetId(), rowIndex: 2, columnIndex: 3}, delimiter: "," }}]
};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());

您只需确保在批量更新之前在工作表中添加足够的行数。

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:

var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getSheetByName('Dataset'),
file = Utilities.newBlob(...e);

var string = file.getDataAsString();
var csvRowsNo = string.match(/(?:"(?:[^"]|"")*"|[^,\n]*)(?:,(?:"(?:[^"]|"")*"|[^,\n]*))*\n/g).length;

sheet.insertRowsAfter(3, csvRowsNo);

var resource = {
  requests: [{pasteData: { data: string, coordinate: {sheetId: sheet.getSheetId(), rowIndex: 2, columnIndex: 3}, delimiter: "," }}]
};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());

You just have to make sure to add the adequate number of rows in the sheet prior to batch update.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文