我正在使用OpenOffice Calc电子表格公式和伪随机数来生成算术问题数组,我可以轻松地更新以创建新的工作表(我是一名教师)
问题以字符串形式输出公式标记。OOo数学公式使用在编辑器中键入的这些字符串命令来显示格式良好的数学表达式。
我可以手动执行下一步:
1) go to source cell and copy string mark-up to clipboard
2) select target cell and clear existing contents and objects
3) create new Math object anchored to target cell
4) open Math editor window and paste in mark-up string
5) exit Math editor window and return cursor to source cell
结果:给定算术问题的一个很好的数学表达式。
我需要能够在各种工作表上的源单元格的整个列做到这一点。...更好的方法是添加一个侦听器,以便在源更新时动态更新。
我在这里找到了代码:单元格内容内的公式,实现这对固定的单元格,但尽管我尽了最大的努力,我不得不承认失败-概括这段代码简直超出了我的专业知识!
绝对理想的是一个宏函数,我可以像电子表格函数一样调用它;输入参数(sourceCell, targetCell, listenerON/OFF)可以运行上述算法,并在需要时动态更新。
有人能帮我吗?像这样的解决方案,或者任何一种变通方法都会非常有帮助。更新2016/10/27
谢谢你,吉姆·K,这确实有效,但是使用调度器带来了一大堆我没有预见到的困难。我刚刚在OpenOffice论坛上发现了Charlie Young的帖子,其中使用了这个API。我在下面附上了我对他的代码的改编。
谁能帮我把它集成成一个函数在一个类似的方式,我已经描述了?我不知道如何解决Math对象在目标单元格中的位置。
API代码很棒,因为每次更新代码时它都会创建一个新的Math对象。但是需要删除现有的。
我认为不能从函数中删除现有对象的限制将继续存在。即使由函数调用的子程序完成,情况会是这样吗?
function InsertFormula(paraFromCell, paraToCell)
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(0)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = paraFromCell
oShape.setSize(oShape.OriginalSize)
end function
下个更新
我现在已经能很快地解决自己的问题了…
我决定使用子,而不是函数,所以我可以访问表来删除现有对象。代码附加-源单元格在C列和目标单元格在A列匹配行。到目前为止,我只能将对象发送到$A$1。
如何将每个新对象锚定到特定的单元格?
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 1 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(0, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
Next i
End Sub
从Mifeet的示例开始,将此添加到My Macros
:
rem ----------------------------------------------------------------------
rem Creates a math formula from text
Function InsertFormulaFromCell(paramCellFrom, paramCellTo)
dim document as object
dim dispatcher as object
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 = paramCellFrom
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 = paramCellTo
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, toCellArgs())
rem open Star.Math
oDesk = createUnoService ("com.sun.star.frame.Desktop")
dispatcher.executeDispatch(document, ".uno:InsertObjectStarMath", "", 0, Array())
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())
InsertFormulaFromCell = "Math Formula updated " & Now()
End Function
要运行它,将此公式放入单元格C5:
=INSERTFORMULAFROMCELL("$C$3","$C$20")
现在当值更新时,它创建另一个公式。
注意:我不能得到.uno:Delete
部分的Mifeet的代码工作,也许是因为函数不应该访问其他单元格。这可能需要在创建新公式之前手动删除公式。
(代表OP发布解决方案)。
这个问题现在解决了。经过一番搜索,我找到了我需要的东西!简单的很。未来的改进可能是适当地调整单元格的大小。现在很开心。感谢Jim K和Stack Overflow社区的其他成员!
下面的完整宏:
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 6 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(3, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
oShape.Anchor = targetCell
oShape.MoveProtect = True
Next i
End Sub