最後,我發現一個簡單的(實際上非常有效的)解決方案圍繞這個問題。
我使用ZeroClipboard將表格複製到用戶的剪貼板中,然後打開新的Google Speardsheet。用戶需要做的就是點擊Ctrl-V並粘貼表格。
下面是代碼:
HTML
<a ng-repeat="exportBtn in exportTables" class="btn btn-default" href="" clip-copy="getHtmlToCopy(exportBtn.target)" clip-copy-mime-type="text/plain">Copy {{exportBtn.name}} </a> <a class="btn btn-default" ng-href="http://spreadsheets.google.com/ccc?new&hl=he" target="_blank">open a new Google Spreadsheet</a>
JS
$rootScope.getHtmlToCopy = function (target) {
var copyConst = {rowSeperator: "\r\n", colSeperator: "\t"}
function $chk(obj) {
return !!(obj || obj === 0)
}
var TableUtil = {
nodeToString: function (table, rowSeperator, colSeperator) {
var d = "";
if (table.childNodes.length) {
if ("TD" == table.nodeName || "TH" == table.nodeName)colSeperator = rowSeperator = "";
for (table = table.firstChild; table;) {
d += TableUtil.nodeToString(table, rowSeperator, colSeperator);
if ("TR" == table.nodeName)d += rowSeperator; else if ("TD" == table.nodeName || "TH" == table.nodeName)d += colSeperator;
table = table.nextSibling
}
} else"#text" == table.nodeName && $chk(table.nodeValue) && "" !== table.nodeValue && (rowSeperator = table.nodeValue, colSeperator = RegExp("\\t", "g"), rowSeperator = rowSeperator.replace(RegExp("\\n", "g"), ""), rowSeperator = rowSeperator.replace(colSeperator, ""), d += rowSeperator.trim());
return d
}
}
var res = TableUtil.nodeToString($('table#' + target)[0], copyConst.rowSeperator, copyConst.colSeperator)
console.log('got html to copy', res);
//the following line might cause a popup blocker to stop the new tab. you can show a button after the copy is finished that will open a new tab
window.open("http://spreadsheets.google.com/ccc?new");
return (res);
}
它的醜陋,但它的工作完美!