4
$\begingroup$

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?

$\endgroup$
5
$\begingroup$

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".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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