8

是否可以将单元格的内容放在公式中。公式是指数学公式编辑器(插入->对象->公式)。

4

1 回答 1

3

据我所知,无法从公式中引用单元格。数学公式编辑器不了解 OO Calc。但是,您可以在需要时使用宏创建新公式。

请按照以下步骤使其工作:

  1. 将要插入的数学公式放入单元格中。例如,将一些数字放入单元格 A1、A2、A3 并将以下内容放入单元格 C3:

    =CONCATENATE("{";A1;"}";"over {";A2;" `+` ";A3;"}";" `=` ";A4).
    

    {1} over {2 `+` 3} `=这将在 C3 中生成类似的东西

  2. 从下面的代码创建一个宏。在 OO 计算中,选择

    Tools > Macros > Organize Macros > OpenOffice.org Basic > My Macros > Standard
    

    创建一个新宏并粘贴下面的代码。

  3. 现在您可以使用Tools > Macros > Run Macro. 运行insertFormula插入从单元格 C3 生成的数学公式,或者addFormulaListener将注册一个侦听器并在 C3 的内容更改时为您重新生成公式。

这是代码。它包含常量formulaCellFromformulaCellTo,它们指定哪个单元格具有数学公式源,以及哪个单元格是生成的公式对象应放置的目标单元格。请注意,目标单元格必须足够大,才能容纳生成的公式,否则宏在重新生成公式时不会删除单元格的旧内容

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

代码改编自这个问题。显然,必须在其中创建宏My Macros并且在嵌入电子表格时不起作用(安全措施?它对我不起作用)。源单元格和目标单元格是硬编码的,但您可以修改宏以满足您的需要。我不熟悉 Visual Basic,但这样的修改应该很容易。

于 2013-05-23T16:50:55.180 回答