Recent

Author Topic: create a new worksheet (file) based on a template worksheet  (Read 896 times)

talitaedwiges

  • Newbie
  • Posts: 5
create a new worksheet (file) based on a template worksheet
« on: November 10, 2022, 02:05:09 pm »
Hello! I need to create a new worksheet (file) based on a template worksheet. I'm using CopyWorksheetFrom it worked, but the cell values ​​with formulas and links are not copied.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: create a new worksheet (file) based on a template worksheet
« Reply #1 on: November 10, 2022, 04:16:52 pm »
Please be more specific. The following code is working:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   fpspreadsheet, xlsxooxml;
  5.  
  6. var
  7.   workbook: TsWorkbook;
  8.   sheet1, sheet2: TsWorksheet;
  9. begin
  10.   workbook := TsWorkBook.Create;
  11.   try
  12.     sheet1 := workbook.AddWorksheet('Original');
  13.     sheet1.WriteNumber(0, 0, 1);
  14.     sheet1.WriteFormula(0, 1, 'A1');
  15.     sheet1.WriteText(5, 0, 'Jump to cell B1');
  16.     sheet1.WriteHyperlink(5, 0, '#B1');
  17.  
  18.     sheet2 := workbook.CopyWorksheetFrom(sheet1, true);
  19.     sheet2.Name := 'Copy';
  20.  
  21.     workbook.WriteToFile('test.xlsx', true);
  22.   finally
  23.     workbook.Free;
  24.   end;
  25. end.

And, please, don't double-post.

talitaedwiges

  • Newbie
  • Posts: 5
Re: create a new worksheet (file) based on a template worksheet
« Reply #2 on: November 10, 2022, 06:02:35 pm »
Copying occurs correctly but as cells containing formulas do not come.

 MyWorkbook:= TsWorkbook.Create;
  MyWorkbook2:= TsWorkbook.Create;
  TRY
    MyWorkbook.ReadFromFile('...\Modelo\Modelo Fluxo de Caixa.xlsx');

    for i:=0 to MyWorkbook.GetWorksheetCount-1 do
    begin
     MyWorksheet:= MyWorkbook.GetWorksheetByIndex(i);
     MyWorkbook2.CopyWorksheetFrom(MyWorksheet,false);
    end;

    t := Now;
    MyWorkbook2.WriteToFile('...\Fluxo de Caixa.xlsx', sfOOXML, true);
    t := Now - t;
    WriteLn(Format('Execution time: %.3f sec', [t*24*60*60]));
  finally
    MyWorkbook.Free;
    MyWorkbook2.Free;       


follows the model I'm trying to clone

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: create a new worksheet (file) based on a template worksheet
« Reply #3 on: November 10, 2022, 08:29:21 pm »
Please check out the current version in the SVN repository (or download the zipped snapshot from https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpspreadsheet/). It fixes handling of hyperlinks when the referenced sheetname contains spaces.

Some formulas in your file are not supported by fpspreadsheet (INDEX, LARGE, IFERROR), and it also does not support embedded comboboxes as well as charts. You will miss these features when saving the workbook copied from the template...

 

TinyPortal © 2005-2018