如何在评估时删除输入单元格?
我想完成以下任务:在评估输入单元格时,它应该自毁(即删除自身)。我尝试与 SelectionMove
和 NotebookDelete
一起破解一些东西,但没有完全得到我想要的东西。
以下是潜在的用例:
该命令可能是将动态生成并插入笔记本中的一系列其他命令的简写
该命令可能仅用于副作用(例如设置笔记本选项或打开新笔记本);评估后将命令留在笔记本中没有任何作用,并且会造成混乱
编辑:根据向导先生,答案是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要影响所有输入单元格,请评估笔记本:
如果您只想影响一个单元格,请选择该单元格并使用选项检查器按上述方式设置
CellEvaluationFunction
。To affect all Input cells, evaluate this is the notebook:
If you only want to affect one cell, then select the cell and use the Options Inspector to set
CellEvaluationFunction
as above.或者,以先生为基础。向导的解决方案,您可以创建一个函数
SelfDestruct
,如果您只想偶尔这样做,它将删除输入单元格:然后评估
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:Then evaluating
2+3//SelfDestruct
outputs5
and deletes the input cell. This usage scenario seems more appealing to me.