Forum > Networking and Web Programming
Solved: Load CSV string from an API to a Stringgrid
(1/1)
arneolav:
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?
dsiders:
--- Quote from: arneolav 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?
--- End quote ---
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.
arneolav:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;{$mode objfpc}{$H+}interfaceuses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids , LAZFileUtils , fphttpclient ; type { TForm1 } TForm1 = class(TForm) Button1: TButton; CheckBox1: TCheckBox; Memo1: TMemo; StringGrid1: TStringGrid; procedure Button1Click(Sender: TObject); private Procedure GetInpAPIcsv; Function InpAPIstream:TMemoryStream; procedure WriteMemStream(FFileName:String); public end;var Form1: TForm1; FFileName : string; // if read from fileimplementation{$R *.lfm}{ TForm1 }procedure TForm1.Button1Click(Sender: TObject);begin GetInpAPIcsv; with StringGrid1 do begin Memo1.Append( Cells[14, 1]); // col / Row Memo1.Append( Cells[15, 1]); endend; Procedure TForm1.GetInpAPIcsv;var FileStream: TFileStream; // if read from filebegin FFileName := 'MyFile.CSV'; try StringGrid1.BeginUpdate; StringGrid1.Clear; // Reset the grid if FileExistsUTF8(FFileName) then // May read from file begin FileStream := TFileStream.Create(FFileName, fmOpenRead+fmShareDenyWrite); StringGrid1.LoadFromCSVStream(FileStream, ';', true, 0, true); FileStream.Free; end else begin // Read with a memorystream InpAPIstream.Position:=0; StringGrid1.LoadFromCSVStream(InpAPIstream, ';', true, 0, true); if CheckBox1.Checked then WriteMemStream(FFileName); // We may want to inspect the file InpAPIstream.Free; end; finally StringGrid1.EndUpdate; end;end; Function TForm1.InpAPIstream: TMemoryStream; // The function as a Memorystreamvar HTTPS: TFPHTTPClient; URL:String;begin Result := TMemoryStream.Create; URL:= 'MyUrl.....'; HTTPS := TFPHTTPClient.Create(nil); try try HTTPS.Get(URL, Result); {<------- HTTPS.Get(URL)----------} Result.Position:=0; except on E: Exception do begin Memo1.Append(' Error in HTTPS.Get ' + E.Message); end; end; finally HTTPS.RequestBody.Free; HTTPS.Free; end;end; // We may want to save the file:procedure TForm1.WriteMemStream(FFileName:String);begin try InpAPIstream.Position:=0; InpAPIstream.SaveToFile(FFileName); except // on E: Exception do ... end;end; (*function ReadStream(FFileName: string): string; // Read a stream:var Strm: TFileStream; n: longint; txt: string;begin txt := ''; Strm := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite); try n := Strm.Size; SetLength(txt, n); Strm.Read(txt[1], n); finally Strm.Free; end; Result := txt;end; // Read values from a csv stringfunction TForm2.ReadCSVCell(iText: String; ARow, ACol: Integer): String;// Uses csvdocument //ReadCSVCellvar CSVDoc:TCSVDocument;begin CSVDoc := TCSVDocument.Create; try CSVDoc.Delimiter := ';'; CSVDoc.LoadFromFile('Myfile.CSV'); CSVDoc.CSVText:=iText; Result := CSVDoc.Cells[ACol, ARow]; finally CSVDoc.Free; end;end;*) end.
Navigation
[0] Message Index