Jump to content


Photo

Addition to NextGridUtils


  • Please log in to reply
No replies to this topic

#1 coasting

coasting
  • Members
  • 29 posts
  • Gender:Male
  • Location:Germany

Posted 28 June 2007 - 04:35 PM

Hi Wim,
Hi all,

I found your NextGridUtils Unit very useful and I am using it with my own projects. Meanwhile I added two further functions for my personal usage which I would like to share with the public as well. Source is based on your code.

CODE
function NG_ToQuotedCsv(const NextGrid: TNextGrid; const header: Boolean = True; const selected: Boolean = False): string;
procedure NG_FromQuotedCsv(const NextGrid: TNextGrid; const QuotedCsvString: string);


Here an example on how to use both functions with Clipboard:

CODE
uses Clipbrd;

// Cut to Clipboard
procedure TfrmMain.Button1Click(Sender: TObject);
var
  ls: char;
begin
  ls := ListSeparator;
  ListSeparator := ',';
  Clipboard.AsText := NG_ToQuotedCsv(NextGrid1,FALSE,True);
  ListSeparator := ls;
  NextGrid1.ClearRow(NextGrid1.SelectedRow);
end;

// Copy to Clipboard
procedure TfrmMain.Button2Click(Sender: TObject);
var
  ls: char;
begin
  ls := ListSeparator;
  ListSeparator := ',';
  Clipboard.AsText := NG_ToQuotedCsv(NextGrid1,FALSE,True);
  ListSeparator := ls;
end;

// Paste from Clipboard
procedure TfrmMain.Button3Click(Sender: TObject);
var
  ls: char;
begin
  ls := ListSeparator;
  ListSeparator := ',';
  NG_FromQuotedCsv(NextGrid1, Clipboard.AsText);
  ListSeparator := ls;
end;



Sourcecode of both functions:

CODE
function NG_ToQuotedCsv(const NextGrid: TNextGrid; const header: Boolean = True; const selected: Boolean = False): string;
var
r, c              : Integer;
sl                : TStringList;
s                 : string;

function AddQuoteChar(const value: string): string;
begin
   Result := StringReplace(value, value, '"'+value+'"', [rfReplaceAll])
end;

begin
sl := TStringList.Create;

if header then
begin
  s := '';
  for c := 0 to Pred(NextGrid.Columns.Count) do
  begin
   if c <> 0 then
    s := s + ListSeparator + AddQuoteChar(NextGrid.Columns[c].Header.Caption)
   else
    s := AddQuoteChar(NextGrid.Columns[c].Header.Caption);
  end;
  sl.Add(s);
end;

for r := 0 to Pred(NextGrid.RowCount) do
  if selected and NextGrid.Selected[r] then
  begin
   s := '';
  for c := 0 to Pred(NextGrid.Columns.Count) do
  begin
   if c <> 0 then
    s := s + ListSeparator + AddQuoteChar(NextGrid.Cell[c, r].AsString)
   else
    s := AddQuoteChar(NextGrid.Cell[c, r].AsString);
  end;
    sl.Add(s);
end;
Result := sl.Text;
sl.Free;
end;

procedure NG_FromQuotedCsv(const NextGrid: TNextGrid; const QuotedCsvString: string);
var
slText: TStringList; // Whole List
slLine: TStringList; // Line
i, j: Integer;
begin
slText := TStringList.Create;
slLine := TStringList.Create;
slLine.Delimiter := ListSeparator;
slLine.QuoteChar:='"';
  try
   slText.Text := QuotedCsvString;
   for j := 0 to slText.Count - 1 do
   begin
    if j + NextGrid.SelectedRow >= NextGrid.RowCount then Break;
    slLine.DelimitedText := slText[j];
    for i := 0 to slLine.Count - 1 do
     begin
      if i + NextGrid.SelectedColumn >= NextGrid.Columns.Count then Break;
      NextGrid.Cells[i + NextGrid.SelectedColumn, j + NextGrid.SelectedRow] := slLine[i];
     end;
   end;
  finally
   slText.Free;
   slLine.Free;
end;
end;


brgds
coasting




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users