Open and close cells programatically in a notebook

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.

You may take a look at this discussion on Stack Exchange.

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…

Thanks for implementing exactly what I asked for!