LibreOffice Basic - Objecten: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 201: Regel 201:
 
end function
 
end function
 
</pre>
 
</pre>
 +
 +
== insertByName ==
 +
 +
inserts the given element at the specified name.
 +
 +
''' Syntaxis '''
 +
 +
<pre>
 +
void insertByName
 +
(
 +
  string aName,
 +
  any    aElement
 +
)
 +
</pre>
 +
 +
Lijkt te werken met allerlei soorten objecten. <code>aElement</code> is waarschijnlijk afhankelijk van het soort object.
 +
 +
''' Voorbeeld '''
 +
 +
<pre>
 +
function insert_sheet_aggregate()
 +
'
 +
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 +
' Insert sheet 'Aggregate' (if not exists)
 +
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 +
'
 +
dim doc as object
 +
dim sheet as object
 +
 +
doc = ThisComponent
 +
 +
if ThisComponent.sheets.hasByName("Aggregate") then
 +
 +
' Do nothing: Sheet "Aggregate" bestaat al
 +
'
 +
' MsgBox "Sheet 'Aggregate' already exists"
 +
 +
else
 +
 +
sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
 +
doc.sheets.insertByName("Aggregate",sheet)
 +
 +
end if
 +
 +
end function
 +
</pre>
 +
 +
 +
''' Bronnen '''
 +
 +
* https://www.openoffice.org/api/docs/common/ref/com/sun/star/container/XNameContainer.html

Versie van 4 nov 2019 14:32

Dit artikel is vooral bedoeld als referentie. Daarom bronnen gelijk in de afzonderlijke hoofdstukken opnemen.

copyByName()

copyByName is een method van sheets.

Syntaxis

void copyByName
(
   [in] string aName,
   [in] string aCopy,
   [in] short  nDestination
)

* aName:        Name of the sheet to be copied
* aCopy:        Name of the copied sheet
* nDestination: Numerical index of the new sheet

Voorbeeld

Sub CopySheet
    Dim oCurrentController As Object
    Dim oActiveSheet As Object
    Dim oSheets As Object
    oCurrentController = ThisComponent.getCurrentController()
    oActiveSheet = oCurrentController.getActiveSheet()
    oSheets = ThisComponent.getSheets()
    If oSheets.hasByName( oActiveSheet.Name & "Copy" ) Then
        MsgBox "Sheet name " & oActiveSheet.Name & "Copy, already exists"
    Else
        oSheets.copyByName(oActiveSheet.Name, oActiveSheet.Name & "Copy", oActiveSheet.RangeAddress.Sheet + 1)
    End If
End Sub

Bronnen

getCellRangeByPosition()

Kopiëer cellen aan de hand van hun coördinaten - Method van sheets

Syntaxis

data_array = sheet.getCellRangeByPosition
(
   x_start,
   y_start,
   x_end,
   y_end
)

Voorbeeld

function copy_paste_test()
	'
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	' Copy & paste een paar cellen binnen de eerste sheet
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'
	cell_org=ThisComponent.sheets(0).getCellRangeByPosition(0,0,0,2)
	cell_dest=ThisComponent.sheets(0).getCellByPosition(3,3)

	ThisComponent.sheets(0).copyRange _
	( _
		cell_dest.CellAddress, _
		cell_org.RangeAddress _
	)	
	
end function

getDataArray()

Get an array from the contents of the cell range.

Syntaxis

sequence< sequence< any > > getDataArray()

Bronnen

gotoEndOfUsedArea()

Ga met de cursor naar de laatste gebruikte cel in betreffende sheet.

Syntaxis

gotoEndOfUserArea
(
   boolean bExpand
)

bExpand: 

* true: Expands the current cursor range
* false: Sets size of the cursor to a single cell

Voorbeeld

function get_used_area_coordinates()
   '
   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   ' Get used area coordinates
   ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   '
   ' Create a cursor on the first sheet
   ''''''''''''''''''''''''''''''''''''
   '    
   o_cursor = ThisComponent.Sheets(0).createCursor()

   ' Goto end of used are
   ''''''''''''''''''''''''''''''''''''
   '
   o_cursor.gotoEndOfUsedArea(false)

   ' Retrieve number of rows & columns
   ''''''''''''''''''''''''''''''''''''
   '
   i_rows = o_cursor.getRangeAddress().EndRow+1
   i_columns = o_cursor.getRangeAddress().EndColumn+1

   ' Display to user
   ''''''''''''''''''''''''''''''''''''
   '
   msgBox "Rijen: " & i_rows
   msgBox "Kolommen: " & i_columns

end function

hasByName()

true if an element with this name is in the container, false otherwise.

Voorbeeld

function insert_sheet()
	'
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	' Insert sheet 'Aggregate' (if not exists)
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'
	dim doc as object
	dim sheet as object
	
	doc = ThisComponent
	
	if ThisComponent.sheets.hasByName("Aggregate") then
	
		' Do nothing: Sheet "Aggregate" bestaat al
		'
		MsgBox "Sheet 'Aggregate' already exists"
		
	else
	
		sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
		doc.sheets.insertByName("Aggregate",sheet)	
	
	end if

end function

Opmerkingen

Als je gewoon wilt testen wat de naam van een gegeven sheet is, krijg je een foutmelding. Wat dan bv. wel werkt:

function test_detect_sheet_by_name()
	'
	' Loop through all sheets
	'''''''''''''''''''''''''''''''''''''
	'
	for sheet_counter = 0 to ThisComponent.sheets.count-1

		if ThisComponent.sheets(sheet_counter).name="Joepie" then
		
			msgbox "Sheet 'Joepie' gevonden bij index " & sheet_counter

		end if

	next
	
end function					

insertByName

inserts the given element at the specified name.

Syntaxis

void insertByName
(
   string aName,
   any    aElement
)

Lijkt te werken met allerlei soorten objecten. aElement is waarschijnlijk afhankelijk van het soort object.

Voorbeeld

function insert_sheet_aggregate()
	'
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	' Insert sheet 'Aggregate' (if not exists)
	''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'
	dim doc as object
	dim sheet as object
	
	doc = ThisComponent
	
	if ThisComponent.sheets.hasByName("Aggregate") then
	
		' Do nothing: Sheet "Aggregate" bestaat al
		'
		' MsgBox "Sheet 'Aggregate' already exists"
		
	else
	
		sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
		doc.sheets.insertByName("Aggregate",sheet)	
	
	end if

end function


Bronnen