Skip to content
Advertisement

How to copy a JavaScript 2D array to clipboard to paste it in excel?

Example 2D array:

JavaScript

How do I copy a 2D array to the clipboard to then paste it in an excel spreadsheet? The rows and columns should be preserved as it is in the array. It should work the same way as if I would mark a range of cells in excel and then paste it back in.

Advertisement

Answer

This solution works amazing. It uses (CSV like) line breaks for a new row and tab for a new cell. Pasting text like this in an Excel, OpenOffice / LibreOffice Calc spreadsheet, it will detect it as multiple cells. It also works with Google Docs.

JavaScript

Test:

JavaScript
JavaScript

Edit:

This solution does not support cells with internal line breaks or tabs. Non-ascii unicodes can cause problems with some spreadsheet programs.

See also How do I copy to the clipboard in JavaScript?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement