LibreOffice Calc:从单元名称获取行和列索引

时间:2015-11-27 10:09:34

标签: vba openoffice-calc libreoffice-calc

我在变量中有一个单元格名称作为值。 示例:((LinearLayout) addView.getParent()).removeView(addView1);

我需要单独变量中该单元格的行和列索引。 示例:myCellName = "C9" columnIndex = rowIndex = myCellName .getRow() '3

1 个答案:

答案 0 :(得分:2)

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

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

要从单元名称中获取列号和行号,可以使用getcellRangeByName,查看https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Cells_and_Ranges,然后使用此CellAddress对象中的Range getCellAddress,请参阅https://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XCellAddressable.html

示例:

首先,我们使用现有的Calc文档和现有的工作表。

Sub Test1

 myCellName = "C9"

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

 oCellAddress = oRange.getCellAddress()

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

End Sub

也许我们没有现有的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
相关问题