Forum > LCL
unit Grids.pas SaveToCSVStream bug?
KodeZwerg:
--- Quote from: wp on December 19, 2022, 11:35:08 am ---Since your fixed column is empty, simply turn it off (Grid.FixedCols := 0), and everything will be fine.
--- End quote ---
When I do that, it be same like on my first post screensnap, leading delimiter ::)
Thanks for all of your text, I understand now that this is a wanted behavior and my thinking was wrong.
rvk:
--- Quote from: KodeZwerg on December 19, 2022, 12:07:31 pm ---
--- Quote from: wp on December 19, 2022, 11:35:08 am ---Since your fixed column is empty, simply turn it off (Grid.FixedCols := 0), and everything will be fine.
--- End quote ---
When I do that, it be same like on my first post screensnap, leading delimiter ::)
--- End quote ---
After doing that (setting FixedCols to 0) you would need to reload the text in your grid, and your first real field will end up in column 0.
In that case you don't have a first empty column (but you will also loose a visible margin column).
wp:
--- Quote from: KodeZwerg on December 19, 2022, 12:07:31 pm ---my thinking was wrong.
--- End quote ---
Hmmm... I think there's no wrong or right here, it's just a matter of preferences. The problem is that once a procedure was written and has been released it is hard to change it without break existing code.
wp:
--- Quote from: KodeZwerg on December 19, 2022, 12:07:31 pm ---
--- Quote from: wp on December 19, 2022, 11:35:08 am ---Since your fixed column is empty, simply turn it off (Grid.FixedCols := 0), and everything will be fine.
--- End quote ---
When I do that, it be same like on my first post screensnap, leading delimiter ::)
--- End quote ---
Sorry. Setting Grid.FixedCells to 0 simply converts the cells in the first column (index 0) from fixed cells to normal cells, and then the SaveToCSVFile will still be the same because it does not distinguish between normal and fixed cells.
What I meant is: Set Grid.FixedCols=0 before you fill the grid and when you fill it make sure that every column index is decremented by 1 compared with the current code.
KodeZwerg:
--- Quote from: rvk on December 19, 2022, 11:55:25 am ---
--- Quote from: KodeZwerg on December 19, 2022, 11:45:04 am ---
--- 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";}};} ---s := s + AStringGrid.Cells[LCol, LRow] + ADelimiter
--- End quote ---
You definitely want to fix these lines ::)
Using a TStringList.DelimitedText, there is some logic which adds protection for a delimiter character in a string.
In your code you don't.
So if one of you cells contain a ADelimiter character, the whole CSV-line would be corrupt.
--- End quote ---
I agree, I was not that deep thinking about such situation and fixed it.
--- 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";}};} ---// SaveGridToFile method by KodeZwerg for FreePascal forum// this method can plain save grid content into a text file where each element is seperated by a delimiter (default = ",")// argument #1: the stringgrid that you want to save// argument #2: the filepath and filename to save, no checking if valid// argument #3: optional set a delimiter, default ","// argument #4: optional shall the save include fixed cells, default False// argument #5: optional shall the save include the title cells, default Falseprocedure SaveGridToFile(const AStringGrid: TStringGrid; const AFileName: string; const ADelimiter: Char = ','; const AIncludeFixed: Boolean = False; const AIncludeTitle: Boolean = False);var LRow, LCol, // where are we inside loop LRowStart, LColStart: Integer; // where do we start working at sl, hl: TStringList; // sl is the main list with formated results from hlbegin sl := TStringList.Create; // create object, needed in the main loop try hl := TStringList.Create; // create object, needed in the main loop to create the sl list try hl.Delimiter := ADelimiter; // setup Delimiter hl.StrictDelimiter := False; //force quoting of strings that contain whitespace or Delimiter if AIncludeFixed then // user wants to include the fixed cells in the results begin if AIncludeTitle then // user wants to include the title LRowStart := 0 // giving every row else if ((AStringGrid.FixedRows >= 1) and (AStringGrid.RowCount > 0)) then // giving fixed row without title LRowStart := 1 // we cut the title off else LRowStart := 0; // giving all LColStart := 0; // 0 for the fixed beginning end // user wants to include the fixed cells in the results else begin // user does not want to have fixed cells if AIncludeTitle then // user wants to include title (what results in getting all cells) LRowStart := 0 // giving all else LRowStart := AStringGrid.FixedRows; // user want no fixed row data LColStart := AStringGrid.FixedCols; // user want no fixed col data end; // user does not want to have fixed cells for LRow := LRowStart to Pred(AStringGrid.RowCount) do // begin outer loop for the rows begin hl.Clear; // clear stringlist for LCol := LColStart to Pred(AStringGrid.ColCount) do // begin inner loop for the columns hl.Add(AStringGrid.Cells[LCol, LRow]); // transport string-grid-cell to stringlist sl.Add(hl.DelimitedText); // store the generated delimited end; finally hl.Free; // free the hl list end; if (sl.Count > 0) then // we gathered something, lets save sl.SaveToFile(AFilename, True); // save main list to disk // this method assume that you want to save and overwrite // so check outside of this method your filename on disk etc... finally sl.Free; // free the main list end;end;
Navigation
[0] Message Index
[*] Previous page