在 LibreOffice Calc 中自动化图表的标题

Posted

技术标签:

【中文标题】在 LibreOffice Calc 中自动化图表的标题【英文标题】:Automating the Title of a chart in LibreOffice Calc 【发布时间】:2021-08-28 14:32:43 【问题描述】:

我找到了以下代码(根据我的需要更改)here

我尝试使用 libre calc 7.3.0 中的宏自动更改许多图表的标题。

我知道哪些单元格包含标题,我希望他们将它们添加到图表中。 我怎样才能使这个 VBA vcode 工作?

Const SCells = "L8, T8, AA8"

' Set the title of the first Chart to the contents of C1
Sub SetTitle
    ' Get active sheet
    oSheet = ThisComponent.CurrentController.ActiveSheet
    
    aCells = Split(SCells,",")
    
    for i = uBound(aCells) to 0 step -1
        
    ' Get the cell containing the chart title, in this case C1
    oCell = oSheet.getCellRangeByName(aCells(i))
   
    oCharts = oSheet.getCharts() 
    ' Get the chart with index 0, which is the first chart created
    ' to get the second you would use 1, the third 2 and so on...
    oChart = oCharts.getByIndex(i)

    oChartDoc = oChart.getEmbeddedObject()

    'Change title
    oChartDoc.getTitle().String = oCell.getString() 

    next i
    
    
    
End Sub
        
        
    

【问题讨论】:

【参考方案1】:

我做错了什么?我在 Const SCells 内容中有空格: "L11, T11, E11" 错了。

我使用xraytool检查了aCells的内容,发现内容被解析为1)“L11”,2)“T11”和3)“E11”,带有空格。

因此,您可以使用该 vba 代码自动更改 librecalc 表中图表的标题:

' NOTE: NO SPACES OR OTHER CHARACTERS
Const SCells = "L11,T11,E11"


Sub SetTitle

    ' Get active sheet
    oSheet = ThisComponent.CurrentController.ActiveSheet
    
    ' Split SCells content by "," 
    ' see Print aCells for corrent content
    aCells = Split(SCells,",")
       
               
        'enumarate by "1" point increment
    for i  = uBound(aCells) to 0 step -1
    
      ' using aCells Content
      oCell  = oSheet.getCellRangeByName(aCells(i))

      oCharts = oSheet.getCharts() 
      ' Get the chart with index 0, which is the first chart created
      ' to get the second you would use 1, the third 2 and so on...
      oChart = oCharts.getByIndex(i)

      oChartDoc = oChart.getEmbeddedObject()

      'Change title
      oChartDoc.getTitle().String = oCell.getString() 

      ' xray oChart.Name

    next i
    
 End Sub

【讨论】:

以上是关于在 LibreOffice Calc 中自动化图表的标题的主要内容,如果未能解决你的问题,请参考以下文章

从 libreoffice 计算图表中提取数据

Libreoffice Calc:自定义 x 轴标签 [关闭]

从 python 执行 LibreOffice Calc Basic 宏

如何在 LibreOffice Calc 中使用 PyUNO 更改单元格边框的 LineWidth?

在 OpenOffice Calc 中自动化图表的标题

LibreOffice (Calc) VBA 单元格总和(按索引)