Difference between revisions of "ZH/Documentation/BASIC Guide/Editing Spreadsheet Documents"
Line 132: | Line 132: | ||
{{PDL1}} | {{PDL1}} | ||
− | + | ||
+ | |||
{{InterWiki_Languages_BasicGuide|articletitle=Documentation/BASIC Guide/Editing Spreadsheet Documents}} | {{InterWiki_Languages_BasicGuide|articletitle=Documentation/BASIC Guide/Editing Spreadsheet Documents}} |
Latest revision as of 22:27, 16 December 2008
上一节介绍了电子表格文档的主要结构,本节将介绍一些服务,利用这些服务可以轻松地访问各个单元格或单元格区域。
单元格区域
除了用于单个单元格的对象(com.sun.star.table.Cell 服务)以外,Apache OpenOffice 还提供了表示单元格区域的对象。这些 CellRange 对象是使用电子表格对象的 getCellRangeByName 调用创建的:
Dim Doc As Object Dim Sheet As Object Dim CellRange As Object Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets.getByName("Sheet 1") CellRange = Sheet.getCellRangeByName("A1:C15")
在电子表格文档中使用冒号 (:) 来指定单元格区域。例如,A1:C15 表示 A、B 和 C 列中 1 至 15 行的所有单元格。
可以使用 getCellByPosition 方法确定单元格区域中的各个单元格位置,其中单元格区域左上角的单元格坐标为 (0, 0)。以下示例使用此方法创建单元格 C3 的对象。
Dim Doc As Object Dim Sheet As Object Dim CellRange As Object Dim Cell As Object Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets.getByName("Sheet 1") CellRange = Sheet.getCellRangeByName("B2:D4") Cell = CellRange.GetCellByPosition(1, 1)
设置单元格区域格式
与单个单元格一样,可以使用 com.sun.star.table.CellProperties 服务将格式应用于单元格区域。有关此服务的详细信息和示例,请参见设置电子表格文档格式。
使用单元格区域进行计算
可以使用 computeFunction 方法对单元格区域执行数学运算。computeFunction 要求将常量作为参数,以说明要使用的数学函数。关联的常量是在 com.sun.star.sheet.GeneralFunction 枚举中定义的。可以使用以下值:
- SUM
- 所有数字值的总和
- COUNT
- 所有值的总数(包括非数字值)
- COUNTNUMS
- 所有数字值的总数
- AVERAGE
- 所有数字值的平均值
- MAX
- 最大数字值
- MIN
- 最小数字值
- PRODUCT
- 所有数字值的乘积
- STDEV
- 标准偏差
- VAR
- 方差
- STDEVP
- 基于总体的标准偏差
- VARP
- 基于总体的方差
以下示例计算 A1:C3 区域的平均值,并在消息框中显示结果:
Dim Doc As Object Dim Sheet As Object Dim CellRange As Object Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets.getByName("Sheet 1") CellRange = Sheet.getCellRangeByName("A1:C3") MsgBox CellRange.computeFunction(com.sun.star.sheet.GeneralFunction.AVERAGE)
删除单元格内容
clearContents 方法简化了删除单元格内容和单元格区域的过程,因为它从单元格区域中删除一种特定类型的内容。
以下示例从 B2:C3 区域中删除所有字符串和直接格式信息。
Dim Doc As Object Dim Sheet As Object Dim CellRange As Object Dim Flags As Long Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets(0) CellRange = Sheet.getCellRangeByName("B2:C3") Flags = com.sun.star.sheet.CellFlags.STRING + _ com.sun.star.sheet.CellFlags.HARDATTR CellRange.clearContents(Flags)
clearContents 中指定的标记来自 com.sun.star.sheet.CellFlags 常量列表。此列表提供了以下元素:
- VALUE
- 未设置为日期或时间格式的数字值
- DATETIME
- 设置为日期或时间格式的数字值
- STRING
- 字符串
- ANNOTATION
- 链接到单元格的注释
- FORMULA
- 公式
- HARDATTR
- 单元格的直接格式
- STYLES
- 间接格式
- OBJECTS
- 连接到单元格的绘图对象
- EDITATTR
- 仅适用于部分单元格的字符格式
也可以将常量相加,以使用 clearContents 调用删除不同的信息。
搜索和替换单元格内容
与文本文档一样,电子表格文档也提供了搜索和替换功能。
在电子表格文档中,用于搜索和替换的描述符对象不是直接通过文档对象创建的,而是通过 Sheets 列表创建的。下面是一个搜索和替换过程的示例:
Dim Doc As Object Dim Sheet As Object Dim ReplaceDescriptor As Object Dim I As Integer Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets(0) ReplaceDescriptor = Sheet.createReplaceDescriptor() ReplaceDescriptor.SearchString = "is" ReplaceDescriptor.ReplaceString = "was" For I = 0 to Doc.Sheets.Count - 1 Sheet = Doc.Sheets(I) Sheet.ReplaceAll(ReplaceDescriptor) Next I
此示例使用文档的第一页创建一个 ReplaceDescriptor,然后通过循环将其应用于所有页面。
Content on this page is licensed under the Public Documentation License (PDL). |