Message Boards Message Boards

0
|
7955 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Open and close cells programatically in a notebook

Posted 11 years ago
How can I programatically open and close cells in a notebook? There are so many commands I am not sure how to go about this.

Suppose I have 3 cells with  text and a fourth cell that contains a button that will close (say) the second cell and another button to reopen it.
I can figure out the rest given this clue.
POSTED BY: William Stewart
3 Replies
Thanks for implementing exactly what I asked for!
POSTED BY: William Stewart
Take a look at how the following code works to get some insight into how to handle a problem like this using CellTags.  I am interpreting you question about opening and closeing a cell as wanting to hide the cell rather than in terms of opening and closing CellGroups.  But the latter can be handled along similar lines.
 CreateWindow[
  DocumentNotebook[{
    TextCell["Cell one", "Text", CellTags -> {"cell1"}],
    TextCell["Cell two", "Text", CellTags -> {"cell2"}],
    TextCell["Cell three", "Text", CellTags -> {"cell3"}],
    TextCell[
     Row[{
       Button["Open",
        Module[{nb},
        nb = EvaluationNotebook[];
        NotebookFind[nb, "cell2", All, CellTags,
         AutoScroll -> False];
        SetOptions[NotebookSelection[nb], CellOpen -> True]
        ]],
      Button["Close",
       Module[{nb},
        nb = EvaluationNotebook[];
        NotebookFind[nb, "cell2", All, CellTags,
         AutoScroll -> False];
        SetOptions[NotebookSelection[nb], CellOpen -> False]
        ]]
      }
     ],
    "Text"]
   }
  ]
]


For a lot more detail on working with various sorts of tags take a look at the Mathematica conference talk I gave several years ago either at 

http://scientificarts.com/worklife/conference2008.html

or at

http://library.wolfram.com/infocenter/Conferences/7246/

Hope this helps...
POSTED BY: David Reiss
You may take a look at this discussion on Stack Exchange
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract