Recent

Author Topic: Using fpSptreadshet in Free Pascal?  (Read 1297 times)

mellotronworker

  • Newbie
  • Posts: 1
Using fpSptreadshet in Free Pascal?
« on: February 25, 2023, 01:26:03 pm »
Can I use fpSpreadsheet in FP?  I am NOT interested in having to use Lazarus, just FP.

I would have expected to see something about the dependencies required for the libray when I downloaded it, but it seems to assume that I am using Lazarus?

thanks

M

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Using fpSptreadshet in Free Pascal?
« Reply #1 on: February 25, 2023, 02:46:43 pm »
Yes.  In the past I have made some simple console programs that set up excel sheets with e.g. schedule information and fill it with some strandard posts. Nothing fancy, but you can just do

Code: [Select]
  MyWorkbook := TsWorkbook.Create;
  MyWorksheet := MyWorkbook.AddWorksheet('Sheet');

in a console program

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Using fpSptreadshet in Free Pascal?
« Reply #2 on: February 25, 2023, 04:07:29 pm »
All the demo programs in folder examples/read_write of the fpspreadsheet installation have a command-line user interface and do not require Lazarus. If you only want to use FPC you only need the laz_fpspreadsheet package (and laz_fpspreadsheet_crypto if you want to read encrypted Excel files). The corresponding source files are in folder source/common (and source/crypto, respectively).

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: Using fpSptreadshet in Free Pascal?
« Reply #3 on: February 25, 2023, 06:07:35 pm »
As I plan to use fpspreadsheet in headless Linux server I am also interested in this.
I used FP IDE, FPC version 3.2.0 on Debian.
Steps:
- get fpspreadsheet 1.14 from lazarus-ccr
- get 'lazutils' directory from Lazarus' Gitlab repository
- set appropriate directories for units and include files
- because of use C-like operators (I don't like them) in lazutf8.pas they had to be enabled in Compiler options
- set Compiler Mode to 'Object Pascal extension on' ({$mode objfpc} in main source didn't work). Otherwise, Result variable isn't enabled in functions and consequently compiling fpscrypto.pas fails.

Code to create test Excel file:
Code: Pascal  [Select][+][-]
  1. program test_excel;
  2.  
  3. uses SysUtils, fpspreadsheet, xlsxooxml;
  4.  
  5. var excel: TsWorkbook;
  6.     list: TsWorkSheet;
  7.     i: Integer;
  8.  
  9. begin
  10.   excel := TsWorkbook.Create;
  11.   try
  12.     list := excel.AddWorksheet('Test sheet');
  13.     i := 0;
  14.     list.WriteCellValueAsString(i, 0, 'ID');
  15.     list.WriteCellValueAsString(i, 1, 'Value');
  16.  
  17.     While i < 20 do begin
  18.       inc(i);
  19.  
  20.       list.WriteCellValueAsString(i, 0, IntToStr(i));
  21.       list.WriteNumber(i, 1, i * 10);
  22.     end;
  23.  
  24.     inc(i);
  25.     list.WriteCellValueAsString(i, 0, 'Total:');
  26.     list.WriteFormula(i, 1, '=sum(B2:B' + IntToStr(i) + ')');
  27.  
  28.     excel.WriteToFile('test.xlsx', True);
  29.   finally
  30.     excel.Free;
  31.   end;
  32. end.
« Last Edit: March 15, 2023, 03:30:25 am by dseligo »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Using fpSptreadshet in Free Pascal?
« Reply #4 on: February 25, 2023, 06:28:46 pm »
- set Compiler Mode to 'Object Pascal extension on' ({$mode objfpc} in main source didn't work). Otherwise, Result variable isn't enabled in functions and consequently compiling fpscrypto.pas fails.
Just added the missing {$mode objfpc}{$H+} directive to the fpscrypto and xlsxooxml_crypto units.

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: Using fpSptreadshet in Free Pascal?
« Reply #5 on: February 25, 2023, 10:33:32 pm »
- set Compiler Mode to 'Object Pascal extension on' ({$mode objfpc} in main source didn't work). Otherwise, Result variable isn't enabled in functions and consequently compiling fpscrypto.pas fails.
Just added the missing {$mode objfpc}{$H+} directive to the fpscrypto and xlsxooxml_crypto units.

Great, thank you.

Would be possible to change lazutf8.pas (from lazutils) C-style operators to Pascal style? They are in 5 places in that file. Then it wouldn't be necessary to switch C-style operators in compiler options.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Using fpSptreadshet in Free Pascal?
« Reply #6 on: February 25, 2023, 10:51:05 pm »
Post the issue in the bugtracker.

dseligo

  • Hero Member
  • *****
  • Posts: 1222

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: Using fpSptreadshet in Free Pascal?
« Reply #8 on: March 15, 2023, 04:48:33 am »
As I plan to use fpspreadsheet in headless Linux server I am also interested in this.
I used FP IDE, FPC version 3.2.0 on Debian.
Steps:
- get fpspreadsheet 1.14 from lazarus-ccr
- get 'lazutils' directory from Lazarus' Gitlab repository
- set appropriate directories for units and include files
- because of use C-like operators (I don't like them) in lazutf8.pas they had to be enabled in Compiler options
- set Compiler Mode to 'Object Pascal extension on' ({$mode objfpc} in main source didn't work). Otherwise, Result variable isn't enabled in functions and consequently compiling fpscrypto.pas fails.


Well this is exactly where I struggle - appropriate directories for units and include files.
Whenever I try compiling the FPSpreadsheet, compilation fails because of missing files. Can we go step-by-step what do I move from Github, and where do I put it?

I need to use FPSpreadsheet in Free Pascal, just like OP.
« Last Edit: March 15, 2023, 04:51:17 am by Weiss »

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: Using fpSptreadshet in Free Pascal?
« Reply #9 on: March 15, 2023, 05:26:58 am »
Can we go step-by-step what do I move from Github, and where do I put it?
No need for github.

1 - download: https://packages.lazarus-ide.org/FPSpreadsheet.zip
2 - download: https://sourceforge.net/projects/lazarus/files/Lazarus%20Zip%20_%20GZip/Lazarus%202.2.4/lazarus-2.2.4-0.zip

in your project directory
3 - make a directory "fps" (for FP SpreadSheet)
4 - make a directory "lazutils" (for lazutils)

5 - from FPSpreadsheet.zip, extract the contents of the directory /fpspreadsheet/source into the created "fps" directory.
6 - from lazarus-2.2.4-0.zip, extract the contents of the directory /lazarus/components/lazutils into the created "lazutils" directory

7 - write your test program (for example test_excel from user dseligo) and save the source-code to your project directory.

8 - compile, f.e. test_excel.pas from commandline in your project directroy with:
$> fpc -B -Mobjfpc test_excel.pas -Fu./fps/* -Fi./fps -Fu./lazutils

Note that you can store the extracted content from the zip files into another location if you wish to do so but that you need to modify the -Fu and -Fi options accordingly.

You could for instance store the extracted contents to a more global location (such as the location of your other 3th party installed libraries) and add the additional search path options  -Fu and -Fi to your fpc.cfg file

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: Using fpSptreadshet in Free Pascal?
« Reply #10 on: March 15, 2023, 02:36:25 pm »
In addition what Tron wrote.

The example I posted was compiled under Free Pascal IDE.
FPspreadsheet is placed in my user directory in fpc/fpspreadsheet directory and lazutils is under fpc/lazutils directory.

In Free Pascal IDE I added three lines in 'Options/Directories'.
In 'Options/Directories/Units' I added:
Code: Text  [Select][+][-]
  1. ../fpc/fpspreadsheet/source/common
  2. ../fpc/lazutils

And in 'Options/Directories/Include files' I added:
Code: Text  [Select][+][-]
  1. ../fpc/fpspreadsheet/source

Lazarus developers don't want to change C style operators to Pascal in file lazutils/lazutf8.pas, so you have to either turn on compiler option 'C like operators' or change Result+= to Result := Result + in 5 places in that file.

 

TinyPortal © 2005-2018