[英]How to download My Html table data into googlesheets or csv using javascript
Earlier My code that I got from google search was working fine.早些时候我从谷歌搜索得到的代码运行良好。 But suddenly it has stopped working.
但突然它停止工作了。
My simple objective is to download my HTML table into csv or Googlesheets file.我的简单目标是将我的 HTML 表下载到 csv 或 Googlesheets 文件中。
How can I achieve this?我怎样才能做到这一点?
Here is my html code这是我的 html 代码
<div class="container">
<div style="width: auto; height: 600px; overflow:scroll;">
<table class="containers" width="100" id="myTable">
<thead id="thead1">
</thead>
<tbody id="perf">
</tbody>
</table>
</div>
<div class="row">
<button id="btn" onclick="exportTableToCSV('allowance.csv')" class="waves-effect waves-light btn light-blue"><i class="material-icons left">file_download</i>Download File to csv</button>
</div>
</div>
Please note My table is generated dynamically and its working fine.请注意我的表是动态生成的,并且工作正常。 but my code that fires the download option does not work.
但是我触发下载选项的代码不起作用。
here is my JavaScript that I got it from web search:-这是我从 web 搜索得到的 JavaScript:-
function downloadCSV(csv, filename) {
var csvFile;
var downloadLink;
// CSV file
csvFile = new Blob([csv], {type: "text/csv"});
// Download link
downloadLink = document.createElement("a");
// File name
downloadLink.download = filename;
// Create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);
// Hide download link
downloadLink.style.display = "none";
// Add the link to DOM
document.body.appendChild(downloadLink);
// Click download link
downloadLink.click();
}
function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
// Download CSV file
downloadCSV(csv.join("\n"), filename);
}
There is nothing in your code that uses Google Apps Script.您的代码中没有任何内容使用 Google Apps 脚本。 Its plain vanilla JavaScript.
它的普通香草 JavaScript。 However if you wanted to add the content to a Google Sheet, then you could use Apps Script, and theSpreadsheets Service or the good old JavaScript client for the Sheets API to insert the values into a sheet.
但是,如果您想将内容添加到 Google 表格,那么您可以使用 Apps 脚本、电子表格服务或用于表格 API的旧JavaScript 客户端将值插入表格。
As you can see from the following snippet, the code is working.正如您从以下代码段中看到的那样,代码正在运行。 I removed styling and added dummy content to the table, but in essence it is the same code you posted.
我删除了样式并向表格添加了虚拟内容,但本质上它与您发布的代码相同。 And it works.
它有效。
Try it:试试看:
function exportTableToCSV(filename) { var csv = []; var rows = document.querySelectorAll("table tr"); for (var i = 0; i < rows.length; i++) { var row = [], cols = rows[i].querySelectorAll("td, th"); for (var j = 0; j < cols.length; j++) row.push(cols[j].innerText); csv.push(row.join(",")); } // Download CSV file downloadCSV(csv.join("\n"), filename); } function downloadCSV(csv, filename) { var csvFile; var downloadLink; // CSV file csvFile = new Blob([csv], { type: "text/csv" }); // Download link downloadLink = document.createElement("a"); // File name downloadLink.download = filename; // Create a link to the file downloadLink.href = window.URL.createObjectURL(csvFile); // Hide download link downloadLink.style.display = "none"; // Add the link to DOM document.body.appendChild(downloadLink); // Click download link downloadLink.click(); }
<div> <table id="myTable"> <thead id="thead1"> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> </thead> <tbody id="perf"> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> </tbody> </table> <br> <button id="btn" onclick="exportTableToCSV('allowance.csv')">Download CSV File</button> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.