从 LibreOffice Calc 中的单元格名称获取行和列索引

Obtain row and column index from cell name in LibreOffice Calc

我有一个单元格名称作为变量中的值。 示例:myCellName = "C9"

我需要单独变量中该单元格的行索引和列索引。 示例:

首先:在你的问题中,你弄乱了行和列。 C9 是第 C 列,即第三列和行 9,即第九行。

第二个:对于 Liberoffice 和 Openoffice,列号和行号是基于 0 的。所以第三列是第2列,第九行是第8行。

要从单元格名称中获取列号和行号,您可以使用 getcellRangeByName,请参阅 https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Cells_and_Ranges, and then get the CellAddress from this Range object using getCellAddress, see https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XCellAddressable.html

示例:

首先我们使用现有的 Calc 文档和现有的 sheet.

Sub Test1

 myCellName = "C9"

 oRange = ThisComponent.Sheets(0).getCellRangeByName(myCellName)

 oCellAddress = oRange.getCellAddress()

 msgbox oCellAddress.Row '8
 msgbox oCellAddress.Column '2

End Sub

也许我们还没有一个存在 sheet 的 Calc 文档,那么我们可以先创建一些:

Sub Test2

 myCellName = "C9"

 oDoc = CreateUnoService("com.sun.star.sheet.SpreadsheetDocument")
 oSheet = oDoc.createInstance("com.sun.star.sheet.Spreadsheet")
 oDoc.Sheets.insertByName("MySheet", oSheet)

 oRange = oSheet.getCellRangeByName(myCellName)

 oCellAddress = oRange.getCellAddress()

 msgbox oCellAddress.Row '8
 msgbox oCellAddress.Column '2

 oCellAddress = Nothing
 oRange = Nothing
 oSheet = Nothing
 oDoc = Nothing

End Sub