据我所知,无法从公式中引用单元格。数学公式编辑器不了解 OO Calc。但是,您可以在需要时使用宏创建新公式。
请按照以下步骤使其工作:
将要插入的数学公式放入单元格中。例如,将一些数字放入单元格 A1、A2、A3,并将以下内容放入单元格 C3:
=CONCATENATE("{";A1;"}";"over {";A2;" `+` ";A3;"}";" `=` ";A4).
Run Code Online (Sandbox Code Playgroud)
这将生成类似于{1} over {2 `+` 3} `=C3 的内容
从下面的代码创建一个宏。在 OO Calc 中,选择
Tools > Macros > Organize Macros > OpenOffice.org Basic > My Macros > Standard
Run Code Online (Sandbox Code Playgroud)
创建一个新宏并粘贴以下代码。
现在您可以使用运行宏Tools > Macros > Run Macro。运行insertFormula插入从单元格 C3 生成的数学公式,或者addFormulaListener运行它将注册一个侦听器并在 C3 的内容发生更改时为您重新生成公式。
这是代码。它包含常量formulaCellFrom和formulaCellTo,指定哪个单元格具有数学公式源以及哪个单元格是应放置生成的公式对象的目标单元格。请注意,目标单元格必须足够大以容纳生成的公式,否则宏在重新生成公式时不会删除单元格的旧内容。
const formulaCellFrom As String = "$C$1"
const formulaCellTo As String = "$C$10"
rem ----------------------------------------------------------------------
rem Adds listener for changes of the math formula
sub addFormulaListener
dim oSheet as Object
dim oCell as Object
rem go to cell containing markup
oSheet = ThisComponent.CurrentController.ActiveSheet
oCell = oSheet.getCellRangeByName(formulaCellFrom)
rem add listener
oListener = CreateUnoListener( "formulaListener_", "com.sun.star.chart.XChartDataChangeEventListener" )
oCell.addChartDataChangeEventListener(oListener)
end sub
rem ----------------------------------------------------------------------
rem Listener for cell changes
sub formulaListener_chartDataChanged
dim oCell as Object
rem remember current cursor position
oCell = ThisComponent.CurrentSelection
rem call insertFormula
call insertFormula
rem restore cursor position
ThisComponent.CurrentController.select(oCell)
end sub
rem ----------------------------------------------------------------------
rem Creates a math formula from text in cell C1 and inserts it into cell C10
sub insertFormula
dim document as object
dim dispatcher as object
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem go to cell containing markup and copy it
dim fromCellArgs(0) as new com.sun.star.beans.PropertyValue
fromCellArgs(0).Name = "ToPoint"
fromCellArgs(0).Value = formulaCellFrom
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, fromCellArgs())
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem go to cell where I want the formula displayed
dim toCellArgs(0) as new com.sun.star.beans.PropertyValue
toCellArgs(0).Name = "ToPoint"
toCellArgs(0).Value = formulaCellTo
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, toCellArgs())
rem delete previous content
dim deleteArgs(0) as new com.sun.star.beans.PropertyValue
deleteArgs(0).Name = "Flags"
rem flags: A = All, S = String, V = Value, D = DateTeim, F = Formula, ...
rem ... N = Notes, T = Formats, O = Objects
deleteArgs(0).Value = "AO"
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, deleteArgs())
rem open Star.Math
oDesk = createUnoService ("com.sun.star.frame.Desktop")
dispatcher.executeDispatch(document, ".uno:InsertObjectStarMath", "", 0, Array())
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem paste clipboard using Array() as place-holder for variable name
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
rem exit Star.Math
dispatcher.executeDispatch(document, ".uno:TerminateInplaceActivation", "", 0, Array())
end sub
Run Code Online (Sandbox Code Playgroud)
该代码是根据这个问题改编的。显然,宏必须在电子表格中创建My Macros,并且在嵌入电子表格时不起作用(安全措施?它对我不起作用)。源单元格和目标单元格是硬编码的,但您可以修改宏以满足您的需要。我不熟悉 Visual Basic,但这样的修改应该很容易。
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |