3
$\begingroup$

I have a series of ~500 notebook files (created by a colleague), which somehow have had their cell groups modified from the default (perhaps accidentally by said colleague). In order to extract the data I need, it would be most convenient if the cells were grouped in the default automatic fashion, by section, subsection, etc. How can I programmatically accomplish this? Obviously, there is the FE menu item Cell > Grouping > Ungroup Cells, but I don't want to have to go through and do this by hand. I have tried executing the FE tokens "CellUngroup" and "UngroupCells" (each of which I've seen referenced in different places), both with the NB option "CellGrouping" -> Manual, and with "CellGrouping" -> Automatic. Here's code that steps through every combination I've tried, none of which work:

(* open file and select all cells *)
fileName = FileNameJoin[nbDirectory, "myFile.nb"];
nb = NotebookOpen[fileName];
FrontEndExecute[FrontEndToken[nb, "SelectAll"]];

(* try with CellGrouping -> Manual *)
SetOptions[nb, CellGrouping -> Manual];
FrontEndExecute[FrontEndToken[nb, "CellUngroup"]];
FrontEndExecute[FrontEndToken[nb, "UngroupCells"]];

(* try with CellGrouping -> Automatic *)
SetOptions[nb, CellGrouping -> Automatic];
FrontEndExecute[FrontEndToken[nb, "CellUngroup"]];
FrontEndExecute[FrontEndToken[nb, "UngroupCells"]];

Does anyone know of a way that I can get this to work?

$\endgroup$
4
  • 1
    $\begingroup$ Please prepare small example of cells with non default grouping that you can't convert with the last code. It works for me. $\endgroup$
    – Kuba
    May 6, 2015 at 21:52
  • $\begingroup$ Good idea. I will prepare a simplified example when I get a chance (writing this comment on my mobile). Thanks all for the comments so far. $\endgroup$
    – Paco Jain
    May 7, 2015 at 3:16
  • $\begingroup$ I put this on hold until you can add an example. Your edit should automatically put this in the reopen review queue but you can also flag this post for moderator attention if you want, after your update. $\endgroup$
    – Mr.Wizard
    May 7, 2015 at 5:16
  • 1
    $\begingroup$ Update: In trying to create a simplified example, I accidentally deleted some problematic cells. When I got the files again from our central repository, the problem went away (I swear, it WAS there to begin with). Perhaps my copies were corrupted in some subtle way that MMA didn't detect/report, but which manifested itself in what I was seeing. At any rate, I'll consider the issue closed for now unless and until it comes up again (but please leave this question on hold for the time being in case I do need to re-open it). Thanks all for your input, and sorry for the noise. $\endgroup$
    – Paco Jain
    May 8, 2015 at 20:45

0

Browse other questions tagged or ask your own question.