Skip to main content

notebooks - Overwrite contents of a cell


I am trying to figure out a way to change the contents of a cell that has previously been evaluated in my notebook. To do this, I'd like to overwrite the cell.


I've seen questions like How to pipe a stream to another notebook? and How do I direct output to a specific notebook or cell? and so far they have not helped me accomplish this.


NotebookApply[cell, data] 


The above is supposed to write data to the specified cell, but I haven't had any luck with this, I'm most likely incorrectly identifying the cell.


The most promising example I've seen so far has been this from the CellObject page:


  Do[CellPrint[
Cell["1", "Output", ShowCellTags -> True,
CellTags -> "overwrite" <> ToString[i]]], {i, 1, 3}];
obj = Cells[CellTags -> "overwrite2"][[1]];
NotebookWrite[obj, Cell["2", "Output"]];

But I don't really understand what it's doing, and I don't understand how to adapt it to suit my needs.


If I have thisCell identifying the CellObject I'd like to change...



NotebookWrite[InputNotebook[], 
thisCell = Cell[BoxData[RowBox[{"mysteryVar = 0"}]], "Input"]]

How can I overwrite the contents of this cell? (I'm running ver 9). Thanks!



Answer



Too long for a comment, please tell me if this is what you need.


Evaluate it in a cell you want:


SetOptions[EvaluationCell[], CellTags -> "target"]

and then later you can do:



NotebookWrite[
Cells[CellTags -> "target"][[ 1]],
Cell[BoxData[RowBox[{"mysteryVar = 0"}]], "Input", CellTags -> "target"]
]

so that first cell was replaced. I've added the CellTags again so you can repeat it.


Comments