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?