Message Boards Message Boards

Paste[] without evaluating the contents of the clipboard?

Posted 5 years ago

I am using Windows. I need a way to copy from the clipboard within a Mathematica statement, without evaluating the contents of the clipboard. I want to end up with the clipboard contents as a string - e.g. this would be like doing the following interactively: " Edit-Paste"

I've been trying to use the Paste[] command. The problem is that using it not only pastes from the clipboard, but also evaluates the contents that are pasted. I've tried the various Hold functions without success. The various Hold functions allow you to Hold various arguments but I want to Hold effectively different levels of downvalues (if that is the correct term).

POSTED BY: Vilis Nams
9 Replies

I am not familiar with the Excel clipboard format, but there is a lovely suite of tools for pasting tabular data in the form of a Palette. See this page for the code. When you evaluate that block of code, you will get a Palette with three buttons for different formats. I think there is a good probability that one of the three will do what you want.

You can save the Palette to your user Mathematica\SystemFiles\FrontEnd\Palettes directory and it will appear in the Palettes menu.

Posted 5 years ago

You could potentially copy/paste the actual cells vs. their content. Here's what I mean:

CopyToClipboard[NotebookRead[Cells[NotebookObject[obj],CellStyle->"Text"]]]

Then Paste:

Paste[]

The Text cell style renders properly outside of the notebook front-end, and you'll need to convert all your Input cells to Text cells first, but that is pretty easy to do: you can alt+click on an input cell in the notebook and it selects all of the cells of that style. Then, you can do ctrl+7 to turn them all into text cells.

Hope this helps!

POSTED BY: Jesse Dohmann
Posted 5 years ago

Can you clarify? What does "obj" refer to? I am trying to paste content that I copied to the clipboard from outside of Mathematica (specifically, from a spreadsheet). Thanks

POSTED BY: Vilis Nams
Posted 5 years ago

obj is just the name of the notebook file being represented as a NotebookObject. It is assuming you are trying to extract the unevaluated code from another notebook.

But I guess you're copying content from outside of Mathematica into a Mathematica notebook? I tried to recreate your issue by copying a column from a Google sheet, and using Paste[] gave me each spreadsheet cell as an Input style cell in Mathematica, without them being added together—I can't seem to recreate your issue.

Can you post a picture or step-by-step process of what you are doing?

POSTED BY: Jesse Dohmann
Posted 5 years ago

Hi Jesse,

I have different problem when copying a column of numbers from Excel 2003: they are just concatenated together instead of being copied as a string with newlines. I described steps necessary to reproduce the issue in another post in this thread: https://community.wolfram.com/groups/-/m/t/1792960.

POSTED BY: Alexey Popkov

I find this very useful to copy data from Excel to Mathematica:

https://mathematica.stackexchange.com/a/14659/251

It is a VBA script for Excel that does the conversion automatically when you copy with a modifier key (CTRL+SHIFT+C instead of CTRL-C)

POSTED BY: Gustavo Delfino

On a Macintosh if I copy, for example, 10! and then evaluate Paste[], I get 10!, not 3628800. To do it programmatically we can add Defer:

CopyToClipboard[Defer[10!]];
Paste[]

10!
POSTED BY: Gianluca Gorni
Posted 5 years ago

This works if you copy into the clipboard using Mathematica. The issue comes when you copy outside of Mathematica. For example, if you copy a column of numbers from a spreadsheet and then evaluate Paste[], then Mathematica multiplies those numbers together, rather than just displaying them as a string with tabs.

POSTED BY: Vilis Nams
Posted 5 years ago

The issue comes when you copy outside of Mathematica. For example, if you copy a column of numbers from a spreadsheet and then evaluate Paste[], then Mathematica multiplies those numbers together, rather than just displaying them as a string with tabs.

What spreadsheet software do you use? When I copy a column of numbers from Excel 2003 and after that evaluate Paste[], it inserts numbers concatenated together with all tabs and empty cells removed:

1) What I copy from Excel 2003:

screenshot 1

2) What appears in Mathematica after evaluating Paste[]:

screenshot 2

When copying from Notepad, I get no such issue.

POSTED BY: Alexey Popkov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract