Recent

Author Topic: Solved: Load CSV string from an API to a Stringgrid  (Read 470 times)

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
Solved: Load CSV string from an API to a Stringgrid
« on: March 06, 2023, 11:29:14 pm »
String from API:
I can save the string to a file and then do: Stringgrid:LoadFromCSVStream. Ok!

But to avoid writing file to disk I try this:
HTTPS.Get(URL, Stream);
Error: Writing to Tstream not supported

Are there some other way to load a string to a Stringgrid?
« Last Edit: March 07, 2023, 12:31:38 pm by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Load string from an API to to a Stringgrid
« Reply #1 on: March 06, 2023, 11:40:15 pm »
String from API:
I can save the string to a file and then do: Stringgrid:LoadFromCSVStream. Ok!

But to avoid writing file to disk I try this:
HTTPS.Get(URL, Stream);
Error: Writing to Tstream not supported

Are there some other way to load a string to a Stringgrid?

The answer is in the error message - and the source code. TStream.Write is not implemented and raises the exception. you need to use one of the descendants like TMemoryStream, TStringStream, TFileStream, et. al. You'll likely need to reset the stream position to 0 after the HTTP get and before calling LoadFromCSVStream.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
Re: Load string from an API to to a Stringgrid
« Reply #2 on: March 07, 2023, 12:26:51 pm »
Thanks,
it did the trick, I should know but my head was empty...

I put in an example of my solution, may be help for others.

And as a comment in the bottom some example of read a fileStream and
read ReadCSVCells:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids
  6.   , LAZFileUtils
  7.   , fphttpclient
  8.   ;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     CheckBox1: TCheckBox;
  15.     Memo1: TMemo;
  16.     StringGrid1: TStringGrid;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     Procedure GetInpAPIcsv;
  20.     Function InpAPIstream:TMemoryStream;
  21.     procedure WriteMemStream(FFileName:String);
  22.   public
  23.   end;
  24. var
  25.   Form1: TForm1;
  26.   FFileName  : string;  // if read from file
  27. implementation
  28. {$R *.lfm}
  29. { TForm1 }
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   GetInpAPIcsv;
  33.   with StringGrid1 do
  34.   begin
  35.     Memo1.Append( Cells[14, 1]);           // col / Row
  36.     Memo1.Append( Cells[15, 1]);
  37.   end
  38. end;
  39.  
  40. Procedure TForm1.GetInpAPIcsv;
  41. var
  42.   FileStream: TFileStream;   // if read from file
  43. begin
  44.   FFileName := 'MyFile.CSV';
  45.   try
  46.     StringGrid1.BeginUpdate;
  47.     StringGrid1.Clear;                                          // Reset the grid
  48.  
  49.     if FileExistsUTF8(FFileName) then                      // May read from file
  50.     begin
  51.        FileStream := TFileStream.Create(FFileName, fmOpenRead+fmShareDenyWrite);
  52.        StringGrid1.LoadFromCSVStream(FileStream, ';', true, 0, true);
  53.        FileStream.Free;
  54.      end
  55.     else
  56.     begin                                                         // Read with a memorystream
  57.       InpAPIstream.Position:=0;
  58.       StringGrid1.LoadFromCSVStream(InpAPIstream, ';', true, 0, true);
  59.       if CheckBox1.Checked then WriteMemStream(FFileName);      // We may want to inspect the file
  60.       InpAPIstream.Free;
  61.     end;
  62.   finally
  63.     StringGrid1.EndUpdate;
  64.   end;
  65. end;
  66.  
  67. Function TForm1.InpAPIstream: TMemoryStream;   // The function as a Memorystream
  68. var HTTPS: TFPHTTPClient;
  69.     URL:String;
  70. begin
  71.   Result := TMemoryStream.Create;
  72.   URL:=  'MyUrl.....';
  73.   HTTPS := TFPHTTPClient.Create(nil);
  74.   try
  75.     try
  76.       HTTPS.Get(URL, Result);                {<------- HTTPS.Get(URL)----------}
  77.       Result.Position:=0;
  78.     except
  79.       on E: Exception do
  80.       begin
  81.         Memo1.Append(' Error in HTTPS.Get ' +  E.Message);
  82.       end;
  83.     end;
  84.  finally
  85.    HTTPS.RequestBody.Free;
  86.    HTTPS.Free;
  87.  end;
  88. end;
  89.                                                   // We may want to save the file:
  90. procedure TForm1.WriteMemStream(FFileName:String);
  91. begin
  92.   try
  93.     InpAPIstream.Position:=0;
  94.     InpAPIstream.SaveToFile(FFileName);
  95.   except
  96.      // on E: Exception do ...
  97.   end;
  98. end;
  99.  
  100. (*
  101. function ReadStream(FFileName: string): string;        //  Read a stream:
  102. var
  103.   Strm: TFileStream;
  104.   n: longint;
  105.   txt: string;
  106. begin
  107.   txt := '';
  108.   Strm := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite);
  109.   try
  110.     n := Strm.Size;
  111.     SetLength(txt, n);
  112.     Strm.Read(txt[1], n);
  113.   finally
  114.     Strm.Free;
  115.   end;
  116.   Result := txt;
  117. end;
  118.                                                         // Read values from a csv string
  119. function TForm2.ReadCSVCell(iText: String; ARow, ACol: Integer): String;
  120. // Uses csvdocument     //ReadCSVCell
  121. var
  122.   CSVDoc:TCSVDocument;
  123. begin
  124.   CSVDoc := TCSVDocument.Create;
  125.   try
  126.     CSVDoc.Delimiter := ';';
  127.     CSVDoc.LoadFromFile('Myfile.CSV');
  128.     CSVDoc.CSVText:=iText;
  129.     Result := CSVDoc.Cells[ACol, ARow];
  130.   finally
  131.     CSVDoc.Free;
  132.   end;
  133. end;
  134. *)
  135.  
  136. end.
« Last Edit: March 07, 2023, 12:32:19 pm by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

 

TinyPortal © 2005-2018