示例2D数组:
var arr = [
["Mike", "Cane", 23],
["Jeff", "Meyers", 46],
["Thomas", "Bush", 67]
]
如何将一个二维数组复制到剪贴板,然后将其粘贴到Excel电子表格中? 行和列应保持与数组中的相同。 它应该像在Excel中选择一系列单元格然后将其粘贴回去一样工作。
示例2D数组:
var arr = [
["Mike", "Cane", 23],
["Jeff", "Meyers", 46],
["Thomas", "Bush", 67]
]
如何将一个二维数组复制到剪贴板,然后将其粘贴到Excel电子表格中? 行和列应保持与数组中的相同。 它应该像在Excel中选择一系列单元格然后将其粘贴回去一样工作。
\t
来分隔列,以及换行符\n
来分隔行。const excelData = arr
.map(lines => lines.join("\t"))
.join("\n");
const arr = [
["Mike", "Cane", 23],
["Jeff", "Meyers", 46],
["Thomas", "Bush", 67]
];
document.getElementById('convert').addEventListener('click', () => {
const excelData = arr
.map(lines => lines.join("\t"))
.join("\n");
document.getElementById('excel').textContent = excelData;
});
<textarea id="excel" cols="30" rows="6"></textarea><br>
<button id="convert">Make Excel Clipboard data</button>
运行代码段,点击按钮,将文本复制粘贴到Excel中进行测试。
这个解决方案非常棒。它使用(类似于逗号分隔值的)换行符表示新行,制表符表示新单元格。将这样的文本粘贴到Excel、OpenOffice / LibreOffice Calc电子表格中,它会将其识别为多个单元格。它在Google Docs上也可以使用。
function copy2DToClipboard(array) {
var csv = '', row, cell;
for (row = 0; row < array.length; row++) {
for (cell = 0; cell < array[row].length; cell++) {
csv += (array[row][cell]+'').replace(/[\n\t]+/g, ' ');
if (cell+1 < array[row].length) csv += '\t';
}
if (row+1 < array.length) csv += '\n';
}
copyTextToClipboard(csv);
}
// copied from https://dev59.com/fHRC5IYBdhLWcg3wJNcN
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
// console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
// console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
function onTest() {
const arr = [
["Mike", "Cane", 23],
["Jeff", "Meyers", 46],
["Thomas", "Bush", 67]
];
copy2DToClipboard(arr);
document.getElementById('test').innerText = 'Copied!';
}
function copy2DToClipboard(array) {
var csv = '', row, cell;
for (row = 0; row < array.length; row++) {
for (cell = 0; cell < array[row].length; cell++) {
csv += (array[row][cell]+'').replace(/[\n\t]+/g, ' ');
if (cell+1 < array[row].length) csv += '\t';
}
if (row+1 < array.length) csv += '\n';
}
copyTextToClipboard(csv);
}
// copied from https://dev59.com/fHRC5IYBdhLWcg3wJNcN
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
// console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
// console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
<button onclick="onTest()" id="test">Copy to clipboard.</button>
此解决方案不支持带有内部换行或制表符的单元格。非ASCII Unicode字符可能会导致某些电子表格程序出现问题。
.replace(/[\n\t]+/g, ' ')
。我认为这部分非常重要。 - Niklas E.我想提一下,现在有一种更简单的方法可以将内容复制到剪贴板上 - 你不再需要创建一个文本区域来复制数据。
这个例子使用了 @christos-lytra 的数组映射。
const arr = [
['Mike', 'Cane', 23],
['Jeff', 'Meyers', 46],
['Thomas', 'Bush', 67],
];
const excelData = arr.map(lines => lines.join('\t')).join('\n');
navigator.clipboard.writeText(excelData);
你需要将数组保存为JSON文件,然后才能将JSON数据导入到Excel工作表中。按照以下步骤将不同形式的数据导入到Excel工作表中。
/[\n\t]+/g
正则表达式进行清理,而是指循环。你的代码完全可接受。当然,for
循环是有效且可接受的,我只是指出在Javascript中可以用更简单的方式来实现。你不需要编辑它,没问题的。 - Christos Lytras