如何在评估时删除输入单元格?

发布于 2024-11-05 09:56:14 字数 1147 浏览 0 评论 0原文

我想完成以下任务:在评估输入单元格时,它应该自毁(即删除自身)。我尝试与 SelectionMoveNotebookDelete 一起破解一些东西,但没有完全得到我想要的东西。

以下是潜在的用例:

  • 该命令可能是将动态生成并插入笔记本中的一系列其他命令的简写

  • 该命令可能仅用于副作用(例如设置笔记本选项或打开新笔记本);评估后将命令留在笔记本中没有任何作用,并且会造成混乱

编辑:根据向导先生,答案是 SelectionMove[EvaluationNotebook[], Previous, Cell]; NotebookDelete[];。我不知道为什么它以前对我不起作用。下面是一些使用这个习惯用法的代码。

writeAndEval[nb_, boxExpr_] := (NotebookWrite[nb, 
    CellGroupData[{Cell[BoxData[boxExpr], "Input"]}]];
   SelectionMove[nb, Previous, Cell];
   SelectionMove[nb, Next, Cell];
   SelectionEvaluate[nb]);

addTwoAndTwo[] := Module[{boxExpr},
  boxExpr = RowBox[{"2", "+", "2"}];
  SelectionMove[EvaluationNotebook[], Previous, Cell];
  NotebookDelete[];
  writeAndEval[EvaluationNotebook[], boxExpr];
  ]

现在,运行 addTwoAndTwo[] 会删除原始输入,并使它看起来好像您已经计算了“2+2”。当然,您可以做各种各样的事情,而不必打印到笔记本上。

编辑2:Sasha 的抽象相当优雅。如果您对它的“现实世界”用法感到好奇,请查看我在“工具包中有什么”问题中发布的代码:您的 Mathematica 工具包中有什么?

I would like to accomplish the following: upon evaluation of an input cell, it should self-destruct (i.e. delete itself). I tried to hack something together with SelectionMove and NotebookDelete, but didn't quite get what I wanted.

Here are potential use cases:

  • the command might be a shorthand for a series of other commands that will be generated dynamically and inserted into the notebook

  • the command might only be used for side effects (e.g. to set a notebook option or to open a new notebook); leaving the command in the notebook after evaluation serves no purpose and creates clutter

Edit: As per Mr. Wizard, the answer is SelectionMove[EvaluationNotebook[], Previous, Cell]; NotebookDelete[];. I don't know why it wasn't working for me before. Here is some code that uses this idiom.

writeAndEval[nb_, boxExpr_] := (NotebookWrite[nb, 
    CellGroupData[{Cell[BoxData[boxExpr], "Input"]}]];
   SelectionMove[nb, Previous, Cell];
   SelectionMove[nb, Next, Cell];
   SelectionEvaluate[nb]);

addTwoAndTwo[] := Module[{boxExpr},
  boxExpr = RowBox[{"2", "+", "2"}];
  SelectionMove[EvaluationNotebook[], Previous, Cell];
  NotebookDelete[];
  writeAndEval[EvaluationNotebook[], boxExpr];
  ]

Now, running addTwoAndTwo[] deletes the original input and makes it look as if you've evaluated "2+2". Of course, you can do all sorts of things instead and not necessarily print to the notebook.

Edit 2: Sasha's abstraction is quite elegant. If you are curious about "real-world" usage of this, check out the code I posted in the "what's in your toolbag" question: What is in your Mathematica tool bag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2024-11-12 09:56:14

要影响所有输入单元格,请评估笔记本:

SetOptions[EvaluationNotebook[], CellEvaluationFunction -> 
  ( (
    SelectionMove[EvaluationNotebook[], All, EvaluationCell]; NotebookDelete[];
    ToExpression@#
  )&)
]

如果您只想影响一个单元格,请选择该单元格并使用选项检查器按上述方式设置 CellEvaluationFunction

To affect all Input cells, evaluate this is the notebook:

SetOptions[EvaluationNotebook[], CellEvaluationFunction -> 
  ( (
    SelectionMove[EvaluationNotebook[], All, EvaluationCell]; NotebookDelete[];
    ToExpression@#
  )&)
]

If you only want to affect one cell, then select the cell and use the Options Inspector to set CellEvaluationFunction as above.

枯叶蝶 2024-11-12 09:56:14

或者,以先生为基础。向导的解决方案,您可以创建一个函数SelfDestruct,如果您只想偶尔这样做,它将删除输入单元格:

SetAttributes[SelfDestruct, HoldAllComplete];
SelfDestruct[e_] := (If[$FrontEnd =!= $Failed,
   SelectionMove[EvaluationNotebook[], All, EvaluationCell]; 
   NotebookDelete[]]; e)

然后评估2+3//SelfDestruct 输出 5 并删除输入单元格。这种使用场景对我来说似乎更有吸引力。

Or, building on Mr. Wizard's solution, you can create a function SelfDestruct, which will delete the input cell, if you intend to only do this occasionally:

SetAttributes[SelfDestruct, HoldAllComplete];
SelfDestruct[e_] := (If[$FrontEnd =!= $Failed,
   SelectionMove[EvaluationNotebook[], All, EvaluationCell]; 
   NotebookDelete[]]; e)

Then evaluating 2+3//SelfDestruct outputs 5 and deletes the input cell. This usage scenario seems more appealing to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文