# How can I get the style of a CellObject?

I want to work on a notebook programmatically with CellObjects.

lets create a notebook for example:

nb = CreateDocument[{TextCell["a", "Title"], Cell["b", "Subtitle"],
Cell["c", "Text"]}];


this gives the CellObjects of the notebook:

cells = Cells[nb]


Now i want to have a function like:

 getCellStyle/@cells


Out:

 {"Title", "Subtitle", "Text"}


I know that i could work with selections as suggested in this question: How can I get the style of selected cells?

But its more practical in my case to work directly with the CellObjects. I think there must be a way to get the style, since the CellObjects look in the FrontEnd like CellObject["style"]

I just want to extract that "style".

• related: 32957 – Kuba Apr 2 '15 at 8:02

Something more documented:

NotebookRead[ PreviousCell[] ][[2]]

"Section"


It seems there is something exactly what you need, quite not ready probably since in Experimental context:

ExperimentalCellStyleNames[ PreviousCell[] ]

"Output"


and ExperimentalCellStyleNames[] gives you styles of all cells in notebook!, quite useful sometimes. Keep in mind that it is undocumented.

p.s. closer look reveals that it is nothing more than CellInformation approach:

ClearAttributes[ExperimentalCellStyleNames, ReadProtected];

Block[{$ContextPath}, AppendTo[$ContextPath, "Experimental"];

AppendTo[\$ContextPath, "NotebookToolsUtilitiesDump"];

?? ExperimentalCellStyleNames
]


take a closer look at the very first definition, now you have new way to set cell style:

(ExperimentalCellStyleNames[#] = "Title") & @ PreviousCell[]


• Nice discovery. To clarify: ExperimentalCellStyleNames@Cells[nb] works, i.e., directly on the CellObjects. So getCellStyles = ExperimentalCellStyleNames – Rolf Mertig Apr 1 '15 at 8:38
• not exactly ... – Rolf Mertig Apr 1 '15 at 8:41
• @RolfMertig Agree, but PreviousCell is a cell object. – Kuba Apr 1 '15 at 8:42
• sure. How do I escape a quote in comments? – Rolf Mertig Apr 1 '15 at 8:45
• @RolfMertig You have to wrap with one more that you want to display inside, and if here is one at the end you have to add whitespace. – Kuba Apr 1 '15 at 8:45
Last /@ FilterRules[DeveloperCellInformation[nb], "Style"]
(* {"Title", "Subtitle", "Text"} *)


Or

SelectionMove[nb, All, Notebook]; "Style" /.  DeveloperCellInformation[nb]
(* {"Title", "Subtitle", "Text"} *)


Working with CellObjects:

RawBoxes[ToBoxes[#][[1, -1]]] & /@ Cells[nb]
(* {"Title", "Subtitle", "Text"}  *)

• thank you for your solution, it works for the most cases, but you did not used the CellObjects. I specify my question, why i need to work with CellObjects... – sacratus Apr 1 '15 at 0:28
• @sacratus, just added a method that works with CellObjects. Not sure how robust it is though. – kglr Apr 1 '15 at 0:31
• Nice this is what i wanted, but instead of RawBoxes one should use ToExpression so that the function returns a String instead of RawBoxes. Thank You! – sacratus Apr 1 '15 at 0:43
• @sacratus it works with cell objects: "Style" /. DeveloperCellInformation@PreviousCell[] – Kuba Apr 1 '15 at 7:07
• The last one is RawBoxes["\"Input\""] for example. So it looks ok but it isn't. – Kuba Nov 15 '15 at 9:27