Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ...

enter image description here

and copy them into Mathematica:

enter image description here

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:-).

share|improve this question
    
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

1 Answer 1

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}} *)
share|improve this answer
    
While I can't use this solution on OS X, +1 for helping that other part of the universe. –  Jagra Jan 30 at 18:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.