Hello, my workbook contain several sheets. I want to open my workbook on a specific sheet, for example, the sheet named “draft”. With Basic in Calc, how to set a specific sheet, to be display on start on my workbook? I developed well in VBA under excel, but I’m a total noob in Basic of Calc and I need help to code this nut command.
Try:
Sub open_in_sheet()
NAME_SHEET = "draft"
doc = ThisComponent
sheet = doc.Sheets.getByName(NAME_SHEET)
doc.CurrentController.setActiveSheet(sheet)
End Sub
And set in event Open Document , see attach example.
open_in_sheet.ods (8.4 KB)
If the name of the sheet to start with is stored as a user defined property of the document, you can read it from there, and leave the activation to an onViewCreated handler without a need to change code if the specific sheetname is changed, or a different spreadsheet document needs to start wih a different sheet.
Or you can use CodeName, user can’t change this value.
but there is no …getByCodeName so you need to loop over the sheets until its …CodeName compares!
To make clear what I meant by my comment above, I unveil one of my secrets by attaching an example file also containing the code running for me ‘onViewCreated’. Parts concerning operations changing the URL are included. Sorry, the code is not just five lines in this case. And, sorry again, it’s in Basic.
startsheetName.ods (55.5 KB)