2013-03-12 98 views
9

是否可以在公式中放置單元格的內容。通過公式我是指數學公式編輯器(insert-> object-> formula)。公式內部的單元格內容

+0

還沒有反應,這將是一個很好的功能! – 2013-03-17 20:30:14

回答

4

據我所知,沒有辦法從公式中引用單元格。數學公式編輯器不瞭解OO Calc。但是,您可以根據需要使用宏創建新的公式。

按照thesse步驟,使其工作:

  1. 將要插入到單元格中的數學公式。例如,把一些數字單元格A1,A2,A3,把下面的單元格C3:

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

    這將在C3

  2. 產生類似{1} over {2 `+` 3} `=創建從下面的代碼的宏。在OO Calc中,選擇

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

    創建一個新的宏並粘貼下面的代碼。

  3. 現在您可以使用Tools > Macros > Run Macro運行宏。運行插入從單元格C3生成的數學公式的insertFormulaaddFormulaListener,它將註冊偵聽器,並在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 

該代碼改編自this question。顯然,宏必須在My Macros中創建,並且在嵌入到電子表格中時不起作用(安全措施?它僅適用於我)。源和目標單元格是硬編碼的,但您可以修改宏以滿足您的需求。我不擅長Visual Basic,但這種修改應該很容易。

+0

要執行這些步驟,我需要將'formulaCellFrom'更改爲「$ C $ 3」。 – 2016-10-26 14:26:52

相關問題