Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When trying to answer this question, I ran into the following problem.

In the help documentation of EvaluationElements, it is claimed that we can use EvaluateNotebook to evaluate only cells that have a certain tag using EvaluationElements, which is an Option. However, I could not get it to work. I have made a piece of code to show you what I have tried. Because the problem was that NotebookEvaluate would evaluate an entire notebook, instead of just a few (or no) cells, there can be a problem with infinite loops. The piece of code I made creates a new notebook with which to manipulate the original notebook. I have taken the opportunity explain things further in a generated text cell. Maybe all this was not the most efficient way to go about asking this question, so you may not want to bother if you don't like fun. Otherwise, please run the code in a new notebook.

nb = EvaluationNotebook[];
If[
  TrueQ[defaultFalseBool]
  ,
  Null
  ,
  defaultFalseBool = True;
  nb2 = NotebookPut@
    Notebook[
     {
      Cell[
       "The cell below has been evaluated for your convenience! But \
as you can see, even though clearly no cells matched the specified \
tag, the entire other notebook got evaluated. To prove it, a message \
was generated in the other notebook (which was printed there because \
of the option InsertResults-> True). Placing curly brackets {} around \
\"blabla\" wont help. Use the cell containing the Clear below if you \
want to try again!", "Text"]
      ,
      Cell[BoxData[{
          RowBox[{
             RowBox[{"NotebookEvaluate", "[", 
                RowBox[{"nb", ",", 
                   RowBox[{"InsertResults", "\[Rule]", "True"}], ",", 
               " ", 
                   RowBox[{"EvaluationElements", "\[Rule]", 

                 RowBox[{"\"\<Tags\>\"", "\[Rule]", " ", 
                   "\"\<blabla\>\""}]}]}], "]"}], 
             ";"}], "\[IndentingNewLine]", 
          RowBox[{"CellPrint", "[", 
             RowBox[{"Cell", "[", 
                RowBox[{
                   RowBox[{"BoxData", "[", "\n", " ", 
                      RowBox[{"RowBox", "[", 
                         RowBox[{"{", 
                           RowBox[{
                           "\"\<Clear\>\"", ",", " ", "\"\<[\>\"", 
                    ",", " ", "\"\<defaultFalseBool\>\"", ",", " ", 
                            "\"\<]\>\""}], "}"}], "]"}], "]"}], ",", 
               " ", "\"\<Input\>\""}], "]"}], 
             "]"}]}], "Input"]
      }
     ];
  CellPrint[
    Cell[BoxData[
      RowBox[{"Print", "[", 
        "\"\<this cell should not have been evaluated, as it lacks \
the specified tag\>\"", "]"}]], "Input"]]

   NotebookEvaluate[nb2, InsertResults -> True];

  ];

The question is: How can we evaluate only specific cells using the option EvaluationElements in NotebookEvaluate?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Evaluating the following will not produce the print statement:

NotebookEvaluate[nb, InsertResults -> True, 
  EvaluationElements -> {"Tags" -> {"blabla"}}];

And will only evaluate cells with tag "blabla".

Another example:

nb = CreateDocument[{ExpressionCell[Defer[Print["Text 1"]], "Input", 
    CellTags -> {"Print"}], 
   ExpressionCell[Defer[Print["Text 2"]], "Input", 
    CellTags -> {"NoPrint"}]}]

NotebookEvaluate[nb, EvaluationElements -> {"Tags" -> {"Print"}}]

Will only print "Text 1".

share|improve this answer
    
Thanks! Very concise too! –  Jacob Akkerboom Apr 11 '13 at 21:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.