Skip to main content

Clipboard Data

Spreadsheet software like Excel typically support copying and pasting cells and data. This is implemented through the Clipboard ("Pasteboard" in MacOS).

When copying a selection of cells, Excel for Windows stores a screenshot of the selected cells as an image. It also creates and stores a number of strings and files for the various formats, including TSV, CSV, HTML, RTF, SYLK, DIF, XLSB, XLS (both '97-2004 and '95), and SpreadsheetML 2003.

Not all Clipboard APIs offer access to all clipboard types.

Tested Deployments

Each browser demo was tested in the following environments:

BrowserDate
Chrome 1192023-11-30
Safari 16.62023-09-01
Brave 1.572023-09-01

Browser Reading (paste)

Clipboard data can be read from a paste event.

The event clipboardData property has a getData method which returns a string compatible with the "string" type in the SheetJS read method1.

The following example reads from the HTML clipboard and generates a XLSX file using the SheetJS writeFile method2:

document.onpaste = function(e) {
/* get HTML */
var str = e.clipboardData.getData('text/html');
/* parse */
var wb = XLSX.read(str, {type: "string"});
/* DO SOMETHING WITH wb HERE */
};

getData accepts one argument: the desired MIME type. Tested browsers support:

MIME typeData format
text/plainTSV (tab separated values)
text/htmlHTML
text/rtfRTF (rich text format)

Live Demo

Open a file in Excel, copy some cells, then come back to this window. Click on "RESULT" below and paste (Control+V for Windows, Command+V for Mac).

Result
Loading...
Live Editor

Reading Files

Modern browsers support reading files that users have copied into the clipboard.

Due to browser API limitations, the system file browser should be used to select and copy spreadsheets into the clipboard.

The event clipboardData.files property, if it is set, is a list of files.

Result
Loading...
Live Editor

Browser Writing (copy)

Clipboard data can be written from a copy event.

The event clipboardData property has a setData method which accepts a string that can be generated using type: "string" in the SheetJS write method3.

The following example generates a HTML string from the first sheet of a workbook object and loads the string into the HTML clipboard:

document.oncopy = function(e) {
/* get HTML of first worksheet in workbook */
var str = XLSX.write(wb, {type: "string", bookType: "html"});
/* set HTML clipboard data */
e.clipboardData.setData('text/html', str);

/* prevent the browser from copying the normal data */
e.preventDefault();
};

setData accepts two arguments: MIME type and new data.

The following table lists the supported MIME types and the bookType4 value that must be passed to the SheetJS write method:

MIME typeData formatbookType
text/plainTSV (tab separated values)txt
text/htmlHTMLhtml

Browsers do not currently support assigning to the text/rtf clipboard type.

Live Demo

This demo creates a simple workbook from the following HTML table:

SheetJSClipboardDemo
bookTypeRTF
sourceHTML Table

Create a new file in Excel then come back to this window. Select the text below and copy (Control+C for Windows, Command+C for Mac). Go back to the Excel file, select cell A1, and paste.

Result
Loading...
Live Editor

Electron

Electron Clipboard API supports HTML and RTF clipboards.

There are special methods for specific clipboard types:

File TypeRead Clipboard DataWrite Clipboard Data
TSVclipboard.readTextclipboard.writeText
HTMLclipboard.readHTMLclipboard.writeHTML
RTFclipboard.readRTFclipboard.writeRTF

Each method operates on JS strings.

clipboard.write can assign to multiple clipboard types:

const { clipboard } = require('electron');
const XLSX = require('xlsx');

function copy_first_sheet_to_clipboard(workbook) {
clipboard.write({
text: XLSX.write(wb, {type: "string", bookType: "txt"}),
rtf: XLSX.write(wb, {type: "string", bookType: "rtf"}),
html: XLSX.write(wb, {type: "string", bookType: "html"})
});
}