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.

I've got something that works, but it seems like it would be "nicer" if I didn't have to use Table (or For, etc.).

I've got 3 Excel files, each with multiple sheets. Each file also has a sheet called "Paste Here" that I don't want. Each sheet contains an array. I'm reading in the file names from the folder, then from each file I read in the sheet names.

Now, I'd like to read the array from each sheet and store them all in a single (nested) list. I'm currently doing the following, which works fine:

Module[{pmlPath},
pmlPath = ParentDirectory[NotebookDirectory[]] <> pmlFolder;
fileNames = Select[FileNames["*.xlsm", pmlPath], StringCount[#, "~"] == 0 &];
sheetNames = DeleteCases[(Import[#, "Sheets"] & /@ fileNames), "Paste Here", {2}];
pmls = Table[Import[fileNames[[i]], 
       {"Sheets", sheetNames[[i, j]]}], {i,Length[fileNames]}, {j,Length[sheetNames[[i]]]}];
];

It seems like I would have to use multiple slots to accomplish that, if it's possible, and I really don't know how to do that.

share|improve this question
3  
Maybe something like Import[#, {"Sheets", #2}] & @@@ Transpose[{fileNames, sheetNames}]? –  kguler Jun 11 at 20:26
2  
Or Import[#1, {"Sheets", #2}] & ~MapThread~ {fileNames, sheetNames} –  Simon Woods Jun 11 at 20:33
1  
Okay, I'll toss in: Inner[Import[#, {"Sheets", #2}] &, fileNames, sheetNames, List] –  Mr.Wizard Jun 11 at 21:42

1 Answer 1

up vote 5 down vote accepted
fileNames = "file" <> # & /@ RandomSample[CharacterRange["A", "Z"], 4]

(* {"fileS","fileU","fileJ","fileO"} *)

sheetNames = "sheet" <> # & /@ # & /@ (RandomSample[CharacterRange["A", "Z"], 
                                            RandomInteger[{1, 3}]] & /@ Range[4])

(* {{"sheetB","sheetR","sheetK"},{"sheetA"},{"sheetC","sheetO","sheetW"}, 
    {"sheetY","sheetU"}}*)

Use Import in place of foo in the following:

foo[#, {"Sheets", #2}] & @@@ Transpose[{fileNames, sheetNames}]

(* {foo["fileS",{"Sheets",{"sheetB","sheetR","sheetK"}}],
    foo["fileU",{"Sheets",{"sheetA"}}],
    foo["fileJ",{"Sheets",{"sheetC","sheetO","sheetW"}}],
    foo["fileO",{"Sheets",{"sheetY","sheetU"}}]} *)
share|improve this answer
    
I don't understand the second "# & /@". I see that it doesn't work if I remove it. Denote "(RandomSample[CharacterRange" and everything to it's right as X. If I execute X, I get the same thing as when I execute # & /@ X. I really don't understand what's happening. –  Mitchell Kaplan Jun 12 at 18:52
1  
@MitchellKaplan, the function f="sheet"<>#& concatenates "sheet" with the input argument. (Btw you can get the same result using Map["sheet" <> # &, X, {2}] -- map f on X at level 2.) If you compare the outputs of f/@X and f/@#&/@X, you see that, when X is a list of lists, the first one applies f to the rows of X while the second applies it to the deeper elements inside each sublist. Hope this helps. –  kguler Jun 12 at 22:48

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.