1

我正在使用带有伪随机数的 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

谢谢 Jim K,这确实奏效了,但是使用调度程序会带来很多我没有预见到的困难。我刚刚在使用 API 的 OpenOffice 论坛上找到了Charlie Young 的帖子。我在下面包含了我对他的代码的改编。

任何人都可以帮助我以与我描述的类似的方式将它集成到一个函数中吗?我不知道如何解决将 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
4

2 回答 2

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
于 2016-10-31T19:05:37.003 回答
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")

现在,当值更新时,它会创建另一个公式。

注意:我无法让.uno:DeleteMifeet 的代码部分工作,可能是因为函数不应该访问其他单元格。这可能需要在创建新公式之前手动删除公式。

于 2016-10-26T14:49:18.743 回答