How to get format values of a cell's borders and its comment

Cell B2 was made by this code:

REM  *****  BASIC  *****
Option Explicit
Sub MakeCellContentAndFormat

	Dim oDoc 		As Object : oDoc = ThisComponent
	Dim oSheet 		As object : oSheet = oDoc.CurrentController.ActiveSheet
	Dim oCell 		As Object
	Dim oRange		As Object
	Dim aThinBorder As New com.sun.star.table.BorderLine2

	Dim oAnnotation As Object : oAnnotation	= oSheet.GetAnnotations()
	Dim oCellAddress As Object

	Dim oColumns	As object : oColumns = oSheet.getColumns() 'All columns of this sheet
	oColumns.removeByIndex(0, 99)

	MsgBox "Let's go !",,"Please press Enter"

	oCell = oSheet.GetCellByPosition(1, 1)
	oRange = oSheet.GetCellRangeByPosition(1, 1, 1, 1)

	With oCell
		.SetString("Cell B2")
		oCellAddress 			= .CellAddress
		oAnnotation.InsertNew(oCellAddress, "This is cell B2.")
		.Annotation.IsVisible 	= True 'True = Show comment /False = No show
	End With
	
	With oRange
		aThinBorder 				= .TopBorder2
		aThinBorder.Color 			= RGB(255, 0, 0) 'Red
		aThinBorder.InnerLineWidth 	= 0
		aThinBorder.OuterLineWidth 	= 30
		aThinBorder.LineDistance   	= 0
		aThinBorder.LineStyle      	= 0
		aThinBorder.LineWidth      	= 30
		.LeftBorder2 				= aThinBorder
	End With

End Sub

Screenshot from 2022-08-22 17-33-54

If cell B2 was made manually, how can I check cell B2’s borders and get the border properties using macros?
And how can I check the comment of cell B2 using macros?
0031HowToGetCellFormatAndComment.ods (16.0 KB)

LibreOffice:
Version: 7.3.5.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.5-0ubuntu0.22.04.1
Calc: threaded
OS:
Ubuntu 22.04 LTS

Comments in Calc

I’d like to make a function for making another cell on a file that will look the same as cell B2:

MakeAcell1(0, 8, pLeftBorderColor, pLeftborderWidth)

MakeAcell2(0, 8, pComment)

The object TableBorder2 is responsible for the cell borders. You can copy it:

' ...
oCell2.TableBorder2=oCell.TableBorder2

For copying comments to cells, there is, for example, the corresponding Paste special option.

1 Like

Another way to create a “simple” comment with the same text:

'  ...
oSheet.Annotations.InsertNew oCell2.CellAddress, oCell.Annotation.String

A cell has 4 borders around, each one described by a structure containing 6 values. Four of them are (small) measures of dimension Length (one redundant probably) given in units of 10µm. The additional two are a color (RGB-coded) and a rather cryptic LineStyle designator (given as an enumeration index). . That makes 24 values for your cell.
Since a SheetCell is treated as a SheetCellRange at the same time, there is an additional property TableBorder consisting of 25 “simple” values.

An Annotation (called “comment” in the UI) also has lots of properties of which some may be used somewhere else, while others are context-dependent.

Assuming somebody gives you code to find out all the (about) 70 simple values together with their names and with information about the ways to access them from the structures they may be contained in: How would you use that information?

What do you mean by “on a file”?
Why can’t you use a Copy/Paste process manually?
Well, the API has services and interfaces for data transfer designed and coded by IT people who knew a lot about their objects and their properties. Do you think you can do better with a bit of Basic code?

To CopyPaste from a sheet in one document to a sheet in a different documenT:
===:
Open the source document. Basic can identify it as ThisComponent when you start a Sub.
Open the target document and get access to it with the help of StarDesktop.loadComponentFromURL(...)
Select the source cell (or a range) in the source document and send whatever it contains to an intermediary memory object, say intermed with the help of the getTransferable() method of the source controller. Then use your access object to the target doument, select the target position, and use the second controller witth insertTransferable(intermed).
Done. No need to know 70 values and a lot of structure information.

1 Like