2013-03-12 32 views
9

Có thể đặt nội dung của ô trong công thức hay không. Theo công thức, tôi có nghĩa là trình soạn thảo công thức toán học (insert-> object-> formula).Nội dung ô bên trong công thức

+0

Không có phản ứng nào, và đây sẽ là một tính năng tuyệt vời! –

Trả lời

4

Theo hiểu biết tốt nhất của tôi, không có cách nào để tham chiếu ô từ công thức. Trình soạn thảo công thức toán học không có kiến ​​thức về OO Calc. Tuy nhiên, bạn có thể tạo công thức mới bất cứ khi nào cần sử dụng macro.

theo thesse bước để làm cho nó hoạt:

  1. Đặt công thức toán mà bạn muốn chèn vào một tế bào. Ví dụ, đặt một số con số để các tế bào A1, A2, A3 và đặt sau để ô C3:

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

    này sẽ tạo ra một cái gì đó giống như {1} over {2 `+` 3} `= trong C3

  2. Tạo một macro từ mã dưới đây. Trong OO Calc, chọn

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

    Tạo macro mới và dán mã bên dưới.

  3. Bây giờ bạn có thể chạy macro bằng cách sử dụng Tools > Macros > Run Macro. Chạy hoặc insertFormula chèn công thức toán học được tạo từ ô C3 hoặc addFormulaListener sẽ đăng ký trình nghe và tạo lại công thức cho bạn bất cứ khi nào nội dung của thay đổi C3.

Đây là mã. Nó chứa các hằng số formulaCellFromformulaCellTo, trong đó xác định ô nào có nguồn công thức toán và đó là ô đích nơi đối tượng công thức được tạo sẽ được đặt. Lưu ý rằng ô đích phải đủ lớn cho công thức được tạo, nếu không macro sẽ không xóa nội dung cũ của ô khi tái tạo công thức.

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 

Mã được điều chỉnh từ this question. Rõ ràng, macro phải được tạo trong My Macros và không hoạt động khi được nhúng trong bảng tính (biện pháp bảo mật? Nó chỉ không hoạt động đối với tôi). Các ô nguồn và đích được mã hóa cứng nhưng bạn có thể sửa đổi macro để phù hợp với nhu cầu của mình. Tôi không có kỹ năng trong Visual Basic nhưng những sửa đổi như vậy phải dễ dàng.

+0

Để làm theo các bước này, tôi cần thay đổi 'formulaCellFrom' thành" $ C $ 3 ". –

Các vấn đề liên quan