# Assign the contents of a “Copy” operation programmatically to a variable?

While an earlier question Cut and paste data from a spreadsheet nominally addresses the issue, a year has past since its OP asked it and I hope that others have come across simpler or more direct ways to do this. I think a new solution might interest the community.

I often want to get values (numbers, dates, ...) from a spreadsheet someone has sent me into Mathematica.

Import[...] works fine, but seems a bit cumbersome, when I just want to grab something like a single column or row of values and work with them immediately.

"Copy/Paste" can give one odd results. Set up a short list of numbers in a spreadsheet ...

and copy them into Mathematica:

This seems a bit inconsistent and neither of these gives one a list of the data elements like Import[...].

Note: I use Mac OS X if it makes any difference.

Mr. Wizard supplies a pretty good solution in the original question, which references code for a palette that Szabolcs posted at see page, see Pasting tabular data into notebooks.

I've added a title and minor formatting to the palette:

CreatePalette[
Column[{Button["TSV",
Module[{data, strip},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
strip[s_String] :=
StringReplace[s, RegularExpression["^\\s*(.*?)\\s*$"] -> "$1"];
strip[e_] := e;
NotebookWrite[InputNotebook[],
ToBoxes@Map[strip, ImportString[data, "TSV"], {2}]]]],
ImageSize -> 85],
Button["CSV",
Module[{data, strip},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
strip[s_String] :=
StringReplace[s, RegularExpression["^\\s*(.*?)\\s*$"] -> "$1"];
strip[e_] := e;
NotebookWrite[InputNotebook[],
ToBoxes@Map[strip, ImportString[data, "CSV"], {2}]]]],
ImageSize -> 85],
Button["Table",
Module[{data},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
NotebookWrite[InputNotebook[],
ToBoxes@ImportString[data, "Table"]]]], ImageSize -> 85]},
Alignment -> {Center, Center}], WindowTitle -> "Paste Data",
WindowSize -> {85, Fit}]


One can install this by choosing Palettes > Install Palette (from the file menu) then provide the source file, palette name, and installation directory to the Install Mathematica Item dialog, and click Finish.

This works, but it does not have the immediacy that one gets used to when working with Mathematica, so...

Does a simple way exist to assign the contents of a "Copy" operation (from a spread sheet or csv file) programmatically to a variable as a list of elements, which one could immediately press into service?

Given that a clipboard appears to hold copies of content in different formats, an elegant solution might involve a right click to select how one wanted it pasted. Maybe a feature request to Wolfram would do it (eventually:-).

-
Oops; it's a duplicate. Well, I think it is. Please review the marked question, especially my answer there, and let me know if you feel it is not. –  Mr.Wizard Jan 28 at 15:55
@Mr.Wizard - Duplicate? Kind of, but the answers, excepting yours, don't provide solutions for what I'd like to see. While yours does (I just implemented it;-), having done so makes me think we can make it better by: naming the palette & supplying steps to install it using Palette -> Install palette...?. Also, even your answer has intermediate steps. A year since the original, might not others have come across simpler or more direct ways to do this? I think a new solution might interest the community, but maybe we could spark new contributions to the original. Thoughts? –  Jagra Jan 28 at 17:31
If you edit the question to: (1)reference the earlier one, (2)state that you are using Szabolcs's palette and explain how it basically works, and (3)describe how you would like to improve upon it if possible, I will happily reopen it. –  Mr.Wizard Jan 28 at 23:46

For Windows users something could perhaps be created using NETLink. For a basic demonstration:

Needs["NETLink"];
InstallNET[];

ImportString[ClipboardGetText[], "Table"]