Jump to content


Photo

feature request: add a .savetoclipboard


  • Please log in to reply
4 replies to this topic

#1 d6user

d6user
  • Members
  • 94 posts

Posted 05 October 2013 - 08:12 AM

not sure if this was ever requested or added since, but,

in NextGrid, for Saving grid content, the following are available:

NextGrid.SaveToHTML
NextGrid.SaveToIni
NextGrid.SaveToRegistry
NextGrid.SaveToStream
NextGrid.SaveToXMLFile
NextGrid.SaveToTextFile

and, for Loading grid content, the following are available:

NextGrid.LoadFromIni
NextGrid.LoadFromRegistry
NextGrid.LoadFromStream
NextGrid.LoadFromTextFile

i searched around for Saving/Loading the StringGrid content to clipboard, but did not find one. i am using v5.5 of nextgrid.

sometimes i have notepad or excel open and it would be nice if i could quickly copy to/from it quickly.

is it possible to have added, the following:

NextGrid.SaveToClipboard
NextGrid.LoadFromClipboard

thank you.

#2 RonnyH

RonnyH
  • Members
  • 29 posts
  • Gender:Male

Posted 05 October 2013 - 09:30 AM


uses ClipBrd;



procedure TForm1.Button1Click(Sender: TObject);

var

I,J: Integer;

pLine, pCompleteText: string;

const

delimiter = ',';

begin

for I := 0 to NextGrid1.RowCount-1 do begin

pLine := '';

for J := 0 to NextGrid1.Columns.Count-1 do begin

	 pLine := pLine + NextGrid1.Cell[J, I].AsString + delimiter;

end;

pCompleteText := pCompleteText + pLine + #13#10;

end;

Clipboard.Clear;

Clipboard.AsText := pCompleteText;

end;


#3 d6user

d6user
  • Members
  • 94 posts

Posted 05 October 2013 - 09:07 PM

thank you RonnyH, your code worked flawlessly, most helpful. later, i will look to see if i can do the reverse, the "load".

but, is there any chance to make it excel friendly? so that when i paste into excel, it will format into each cell instead of just one cell?

hopefully, this/these methods will be an added feature in v6 of nextgrid.

thank you.

#4 d6user

d6user
  • Members
  • 94 posts

Posted 05 October 2013 - 09:22 PM

i figured it out.

procedure TForm1.Button3Click(Sender: TObject);
var
  I,J: Integer;
  pLine, pCompleteText: string;
const
  textdelimiter = ',';	   // if plain text, ie, notepad, or
  exceldelimiter = vk_tab; // if pasting into excel in separate cells
begin
  for I := 0 to ng1.RowCount-1 do begin
    pLine := '';
    for J := 0 to ng1.Columns.Count-1 do begin
//    pLine := pLine + ng1.Cell[J, I].AsString + textdelimiter;
    pLine := pLine + ng1.Cell[J, I].AsString + char(exceldelimiter);
    end;
    pCompleteText := pCompleteText + pLine + #13#10;
  end;
  Clipboard.Clear;
  Clipboard.AsText := pCompleteText;
end;


#5 d6user

d6user
  • Members
  • 94 posts

Posted 06 October 2013 - 06:51 PM

and here is the load_from_clipboard method.

it assumes the "tab" codes as in working in excel, also works in notepad too. the only bug is that when you paste your grid to the clipboard and then copy it to excel worksheet, an extra (blank) column is included. i believe that those are the linefd codes. so be careful when you want to copy back the results from excel, that you include that column. and you can even create new lists of data using excel and paste it into the grid. only thing you have to concern yourself with is that your excel columns count meets with your grids column count.

it is not condensed code, so you can probably fine tune it to a shorter version.

procedure TForm1.Pastetogrid1Click(Sender: TObject);
const
  exceldelimiter = vk_tab; // if pasting into excel in separate cells
  linefd		 = #13#10; // the end-of-line carriage and line feed codes
var
  I,J, index: Integer;
  s, str : string;
  getRowCount: integer;
begin
  // first, get the rowcount of copied to clipboard grid content
  getRowCount := 0; index := 0; s := clipboard.astext;
  while pos(linefd, s) > 0 do begin
  index := pos(linefd, s);
  inc(getRowCount);
  delete(s,index,2);
  end;
  // now, clear grid completely
  ng1.ClearRows;
  // assign temporary string variable of the clipboard text (the grid)
  s := clipboard.astext; index:=0;
  // then, paste the clipboards content to the grid
  for I := 0 to getRowCount-1 do begin
    ng1.AddRow();
    for J := 0 to ng1.Columns.Count-1 do begin
	  str := leftstr(s, pos(char(vk_tab),s)-1);
	  delete(s, 1, length(str)+1);
	  ng1.Cell[J, I].AsString := str;
    end;
    delete(s, 1, 2);
  end;
end;





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users