Macros/Basic/Calc/Sheets

From The Document Foundation Wiki
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:

Return to Index

Sheets

All sheets

1 doc = ThisComponent
2 
3 For Each sheet In doc.Sheets
4     MsgBox sheet.Name
5 Next

All names

1 sheets = ThisComponent.Sheets
2 MsgBox Join(sheets.ElementNames, Chr(13))

Get by name

1 sheets = ThisComponent.Sheets
2 sheet = sheets.getByName("Sheet1")

Get by index

1 sheets = ThisComponent.Sheets
2 sheet = sheets.getByIndex(1)

Verify if exists

1 sheets = ThisComponent.Sheets
2 exists = sheets.hasByName("Sheet1")

Count

1 sheets = ThisComponent.Sheets
2 Msgbox sheets.Count

Get active sheet

1 doc = ThisComponent
2 sheet = doc.CurrentController.ActiveSheet

Set active sheet

1 doc = ThisComponent
2 sheet = doc.Sheets.getByIndex(1)
3 doc.CurrentController.setActiveSheet(sheet)

Insert by name

1 sheets = ThisComponent.Sheets
2 position = 0
3 sheets.insertNewByName("New_Sheet", position)

Insert like object

1 doc = ThisComponent
2 sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
3 sheets.insertByName("New_Sheet_Obj", sheet)

Delete

1 sheets = ThisComponent.Sheets
2 sheets.removeByName("Sheet1")

Move

1 sheets = ThisComponent.Sheets
2 position = 0
3 sheets.moveByName("Sheet1", position)

Copy in the same file

1 sheets = ThisComponent.Sheets
2 position = 0
3 sheets.copyByName("Sheet1", "Copy_Sheet", position)

Copy from other file

1 sheets = ThisComponent.Sheets
2 path = ConvertToUrl("/home/USER/Source_File.ods")
3 doc_source = StarDesktop.loadComponentFromURL(path, "default", 0, Array())
4 position = 0
5 
6 sheets.importSheet(doc_source, "Sheet1", position)

Replace

CAUTION: Replace sheet NAME with a clean sheet.

1 doc = ThisComponent
2 sheets = doc.Sheets
3 
4 sheet = doc.createInstance("com.sun.star.sheet.Spreadsheet")
5 sheets.replaceByName("Sheet1", sheet)

Rename

1 sheet = sheets.getByName("Sheet1")
2 sheet.Name = "New_Name"

Visibility

1 sheet = sheets.getByName("Sheet1")
2 sheet.IsVisible = False
3 
4 'Or
5 
6 sheet.IsVisible = True

Set password

1 sheet = sheets.getByName("Sheet1")
2 sheet.Protect("letmein")

Remove password

1 sheet = sheets.getByName("Sheet1")
2 sheet.unProtect("letmein")

Color

1 sheet = sheets.getByName("New_Name")
2 sheet.TabColor = RGB(255, 0, 0)