0

I have a simple JavaScript code in HTML page which gets pasted content from excel, splits it by "\t" and puts it in a HTML table. Somethin like this - http://jsfiddle.net/duwood/sTX7y/

When in Excel I have cell with value 1.2562 and this cell is number formatted with 2 decimal places, Excel displays 1.26. When I copy/paste this cell to the HTML page, only 1.26 is copied. When I paste to another Excel document the actual value is pasted, but only if the source Excel file is still open.

Is there any way to get the actual cell value from the clipboard using JavaScript?

Todor
  • 43
  • 1
  • 8
  • please read stackoverflow.com/help/how-to-ask You will find people would be more receptive to help if you edited your post to be more inline with the guidelines – alowflyingpig Nov 11 '18 at 23:31
  • This: _but only if the source Excel file is still open_ -- and if it is not open? what happens? – Randy Casburn Nov 11 '18 at 23:31
  • I'd be using `.value` Would be nice to see the code though... – alowflyingpig Nov 11 '18 at 23:38
  • I added jsfiddle link, not mine, but the code is similar. – Todor Nov 11 '18 at 23:45
  • 2
    Last paragraph on this page is relevant - http://www.mschaef.com/what_is_in_your_clipboard – Tim Williams Nov 11 '18 at 23:46
  • If I understand it right, the content copied from Excel, pasted to Excel is in completely different format than the same content pasted to HTML. This raises the question, is it possible to request the content in another format, using JavaScript in an HTML page. – Todor Nov 11 '18 at 23:59
  • I don't think there's any way to do that without some type of browser plug-in / add-on – Tim Williams Nov 12 '18 at 01:21
  • @TimWilliams - Very nice read, I would say that the whole page is relevant. I wish I would have seen your comment before answering! I updated my answer to include the link. – David Nov 12 '18 at 04:25

1 Answers1

2

Clipboard formats

When you copy data from an application, it sends different pieces of data, on different formats, to the clipboard. As a result, you can do things as copying a file and then paste the actual file on a directory or just its path on a text document.

Likewise, an application is ready to understand a certain set of formats.

Some of these formats are standard. Others, are custom formats unique to an application. Maybe you want to read about clipboard formats on Windows and, mainly, the article provided by @Tim Williams.

You need to find a format that both Excel and your supported browsers can understand.

This other answer list some formats used by Excel. The Clipboard API and events W3C Working Draft enforces some mandatory data types.

In a slightly different scenario, I copied a range of cells from LibreOffice Calc to Firefox on my Linux machine. I can get a list of valid data formats for the current selection by using xclip:

xclip -selection clipboard -o -t TARGETS

As per @Tim Williams' link you can use a tool called cbdump on Windows. It seems that it is not available any longer; the OP found a similar one called clipview.

It returns around 20 formats, half of them exclusive to OpenOffice (application/x-openoffice-...). Just two were supported by my browser (the rest return an empty string): text/plain and text/html.

Under text/plain format, the browser pastes the text inside the cells as you see it (1.26, not the real value 1.2562) and that makes sense. Under text/html it returns a very verbose output, including the cell value:

<!-- more html -->
<td height="34" align="right" sdval="1.2562">1.26</td>
<!-- more html -->

Set clipboard format in JavaScript

Once you know which is your target format, you can use ClipboardEvent.clipboardData:

  • to obtain the data to be pasted from the paste event handler, typically with a getData(format) call.

format is a DOMString representing the type of data to retrieve.

var textArea = document.querySelector('textarea');

textArea.addEventListener('paste', function(e) {
  var format = document.querySelector('input[name="format"]:checked').value;
  e.preventDefault();
  var excel_data = (e.originalEvent || e).clipboardData.getData(format);
  textArea.value = excel_data;
});
<input id="plainText" type="radio" name="format" value="text/plain" checked>
<label for="plainText">plain text</label>
<input id="HTML" type="radio" name="format" value="text/html">
<label for="html">HTML</label>
<p>Paste excel data here:</p>
<textarea></textarea>
David
  • 6,695
  • 3
  • 29
  • 46
  • I couldn't find `cbdump` anywhere on the web, but I found a similar tool `clipview` that gets the job done. http://www.peterbuettner.de/develop/tools/clipview/ – Todor Nov 22 '18 at 00:14
  • @Todor - Thanks for sharing. I've updated my answer. – David Nov 22 '18 at 09:43
  • My excel sheet contains decimal values when I paste I am not getting values(not even text/html) after decimal. In above mentioned clipView I can see decimal values in xml Spreadsheet format – Abhi Apr 27 '21 at 08:58