Recent

Author Topic: [SOLVED] FPSpreadsheet, read number as string from csv  (Read 395 times)

nomorelogic

  • Full Member
  • ***
  • Posts: 161
[SOLVED] FPSpreadsheet, read number as string from csv
« on: March 27, 2023, 12:48:10 pm »
hi all
I've a CSV where third column contains barcodes
Code: Pascal  [Select][+][-]
  1. 'SomeData1','I','                  001234000345','SomeData2'
  2. 'SomeData1','I','                  001234090456','SomeData2'
  3. 'SomeData1','I','                  001234100678','SomeData2'
  4.  

As you can see, all columns are strings delimited using single quote.
Anyway, reading 3rd column using
Code: Pascal  [Select][+][-]
  1. s := UTF8ToString(CsvWorksheet.ReadAsText(CurCell^.Row, CurCell^.Col));
  2.  
I get: 1234000345, 1234090456 and 1234100678 (the initial 0es are missing)

Columns are string, but CurCell^.ContentType = cctNumber.
I need initial zeroes as they're part of barcode.

Any idea on how I can read this value as string?

Thanks
nomorelogic
« Last Edit: March 27, 2023, 06:43:56 pm by nomorelogic »

Bart

  • Hero Member
  • *****
  • Posts: 4978
    • Bart en Mariska's Webstek
Re: FPSpreadsheet, read number as string from csv
« Reply #1 on: March 27, 2023, 01:01:26 pm »
As a workaround, if the numbers are of fixed with, you can zero-padd the result?

Bart

nomorelogic

  • Full Member
  • ***
  • Posts: 161
Re: FPSpreadsheet, read number as string from csv
« Reply #2 on: March 27, 2023, 01:17:08 pm »
zero-padd the result is a possible workaround
anyway I must assume the length as a costant (in this case length = 12)

there may be other cases where the length of the field is different
if there was a setting to force reading as a string (after all, the field IS delimited as a string) I would prefer it

wp

  • Hero Member
  • *****
  • Posts: 10872
Re: FPSpreadsheet, read number as string from csv
« Reply #3 on: March 27, 2023, 01:22:09 pm »
Import of csv files into fpspreadsheet is determined by the CSVParams record; there is a field "DetectContentType" which you must set to false in order to disable automatic cell format detection.

Code: Pascal  [Select][+][-]
  1. program test_project;
  2.  
  3. uses
  4.   fpspreadsheet, fpstypes, fpscsv;
  5. var
  6.   book: TsWorkbook;
  7.   sheet: TsWorksheet;
  8.   r, c: Integer;
  9. begin
  10.   CSVParams.Delimiter := ',';
  11.   CSVParams.DetectContentType := false;
  12.   CSVParams.QuoteChar := '''';
  13.  
  14.   book := TsWorkbook.Create;
  15.   try
  16.     book.ReadFromFile('data.csv', sfCSV);
  17.     sheet := book.GetFirstWorksheet;
  18.     for r := 0 to sheet.GetLastRowIndex do
  19.       for c := 0 to sheet.GetLastcolIndex do
  20.         WriteLn('Row ', r, ' Col ', c, ': >', sheet.ReadAsText(r, c), '<');
  21.   finally
  22.     book.Free;
  23.   end;
  24.  
  25.   ReadLn;
  26. end.
« Last Edit: March 27, 2023, 05:44:39 pm by wp »

nomorelogic

  • Full Member
  • ***
  • Posts: 161
Re: FPSpreadsheet, read number as string from csv
« Reply #4 on: March 27, 2023, 05:48:46 pm »
it works!
thank you all

nomorelogic

 

TinyPortal © 2005-2018