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;
If[Head[data] === String,
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;
If[Head[data] === String,
NotebookWrite[InputNotebook[],
ToBoxes@Map[strip, ImportString[data, "CSV"], {2}]]]],
ImageSize -> 85],
Button["Table",
Module[{data},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
If[Head[data] === String,
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:-).
Answer
For Windows users something could perhaps be created using NETLink. For a basic demonstration:
Needs["NETLink`"];
InstallNET[];
LoadNETType["System.Windows.Forms.Clipboard"];
Now copy the data in Excel and evaluate:
ImportString[Clipboard`GetText[], "Table"]
(* {{1}, {2}, {3}, {4}, {5}} *)
Comments
Post a Comment