我正在使用带有伪随机数的OpenOffice Calc电子表格公式来生成算术问题的数组,我可以轻松更新以创建新的工作表(我是老师)
问题以字符串形式输出为公式标记。 OOo Math公式使用键入编辑器的这些字符串命令来显示格式良好的数学表达式。
我可以手动执行下一步操作:
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
结果:给定算术问题的数学表达式很好。
我需要能够在各种纸张上对整个源单元格列执行此操作。 ......甚至更好,然后添加一个监听器,以便在更新源时动态更新。
我在这里找到了代码:Cell content inside formula为一对固定的单元格实现了这个目标,但尽管我付出了最大的努力,但我不得不承认失败 - 一般来说这个代码完全超出了我的专业知识!
绝对理想是一个宏函数,我可以称之为电子表格函数;使用输入参数(sourceCell,targetCell,listenerON / OFF)可以运行上述算法并在需要时动态更新。
任何人都可以帮助我吗?像这样的解决方案或任何类型的解决方案都会非常有用。
更新2016/10/27
谢谢Jim K,确实有效,但使用调度员带来了一系列我没有预料到的困难。 我刚刚在OpenOffice论坛中找到Charlie Young's post,它使用了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
下一步更新
我现在很快就能解决自己的问题......
我决定使用sub而不是函数,所以我可以访问工作表来删除现有对象。附加了代码 - 源单元格位于列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
答案 0 :(得分:0)
从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")
现在,当值更新时,它会创建另一个公式。
注意:我无法使Mifeet代码的.uno:Delete
部分工作,可能是因为函数不应该访问其他单元格。这可能需要在创建新公式之前手动删除公式。
答案 1 :(得分:0)
(代表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