Recent

Author Topic: changing the position of worksheet in a workbook  (Read 773 times)

kjteng

  • Sr. Member
  • ****
  • Posts: 265
changing the position of worksheet in a workbook
« on: February 10, 2025, 03:53:46 am »
Is it ok to change the position of worksheet by changing the worksheet.index property. For example if I have a workbook (wbk) containing 3 worksheets with name 'one', 'two', 'three'. then I can move worksheet 'one' to the last postion as follows:
  sht := wbk.GetWorksheetByName('one');
  sht.Index := 3
Is this a proper way to move the worksheet?
« Last Edit: February 10, 2025, 03:47:26 pm by kjteng »

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: changing the position of worksheet in a workbook
« Reply #1 on: February 10, 2025, 08:50:39 am »
Hi
I would expect a 'Move' procedure, to be present?!?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

paweld

  • Hero Member
  • *****
  • Posts: 1327
Re: changing the position of worksheet in a workbook
« Reply #2 on: February 10, 2025, 11:37:07 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   workbook: TsWorkbook;
  3.   worksheet: TsWorksheet;
  4. begin
  5.   workbook := TsWorkbook.Create;
  6.   workbook.ReadFromFile('data.xlsx');
  7.   worksheet := workbook.GetFirstWorksheet;
  8.   worksheet.Index := 3;  //set as fourth  
Best regards / Pozdrawiam
paweld

kjteng

  • Sr. Member
  • ****
  • Posts: 265
Re: changing the position of worksheet in a workbook
« Reply #3 on: February 12, 2025, 06:17:52 am »
Thanks all for your reply.

Benny,
I dont think there is 'move' procedure for fpSpread. Though there is a movesheet method (protected method) which is called whenever we set the index property of a worksheet.

paweld,
Your code is the similar to what I did... except that it only moves the first worksheet to the end ( where mine moves any named sheet to end). I have tested this method with few small example and found it works. What I wish to confirm is whether this is the correct way (that do not have other 'side effect')  to move worksheet within a workbook. 

paweld

  • Hero Member
  • *****
  • Posts: 1327
Re: changing the position of worksheet in a workbook
« Reply #4 on: February 12, 2025, 07:25:06 am »
Quote from: kjteng
What I wish to confirm is whether this is the correct way (that do not have other 'side effect')  to move worksheet within a workbook.
Yes, this is the right way to move the sheets. If you specify an index out of range, it will be detected and corrected (set as the first or last sheet)
Best regards / Pozdrawiam
paweld

kjteng

  • Sr. Member
  • ****
  • Posts: 265
Re: changing the position of worksheet in a workbook
« Reply #5 on: February 12, 2025, 10:48:10 am »
tqvm

 

TinyPortal © 2005-2018