unit GridUtils; // Процедуры для копирования, вставки и очистки таблиц interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, Buttons, StdCtrls, ClipBrd; const TableDivider: char = #9; //Процедура - копирование в буфер текста таблицы function CopySelection(AGrid: TStringGrid):boolean; //Вставить в таблицу текст из буфера function PasteSelection(AGrid: TStringGrid):boolean; //Очистить столбцы таблицы function ClearSelection(AGrid: TStringGrid):boolean; //Вырезать - копирование + очистка function CutSelection(AGrid: TStringGrid):boolean; //Процедура - удаление строки из таблицы function DeleteSelectRows(AGrid: TStringGrid):boolean; //Процедура - удаление столбцов function DeleteSelectCols(AGrid: TStringGrid):boolean; //Процедура - сдвиг строки вверх function UpSelectRows(AGrid: TStringGrid):boolean; //Процедура - сдвиг строки вверх function DownSelectRows(AGrid: TStringGrid):boolean; implementation function CopySelection; var S: string; i,j: integer; begin Result:=False; with AGrid do if (Selection.Left >=0) and (Selection.Top >=0) then begin S:=Cells[Selection.Left,Selection.Top]; for j:=Selection.Left + 1 to Selection.Right do S:=S+TableDivider+Cells[j,Selection.Top]; for i:=Selection.Top + 1 to Selection.Bottom do begin S:=S+#13+#10+Cells[Selection.Left,i]; for j:=Selection.Left + 1 to Selection.Right do S:=S+TableDivider+Cells[j,i]; end; ClipBoard.SetTextBuf(PChar(S)); Result:=True; end; end; function PasteSelection; var S: string; i,j,k: integer; begin Result:=False; with AGrid do begin S:=ClipBoard.AsText; if (S <> '') and (Selection.Left >= 0) and (Selection.Top >= 0) then begin //Вставка в таблицу j:=Selection.Top; i:=Selection.Left; Cells[i,j]:=''; k:=1; while k <= Length(S) do begin if S[k] = #9 then begin inc(i); if (i < ColCount) and (j < RowCount) then Cells[i,j]:=''; end else if S[k] = #13 then begin i:=Selection.Left; inc(j); if (i < ColCount) and (j < RowCount) then Cells[i,j]:=''; end else if not (S[k] = #10) then if (i < ColCount) and (j < RowCount) then Cells[i,j]:=Cells[i,j] + S[k]; inc(k); end; Result:=True; end; end; end; function ClearSelection; var i,j: integer; begin Result:=False; with AGrid do if (Selection.Left >=0) and (Selection.Top >=0) then begin for i:=Selection.Left to Selection.Right do for j:=Selection.Top to Selection.Bottom do Cells[i,j]:=''; Result:=True; end; end; function CutSelection; begin Result:=CopySelection(AGrid); ClearSelection(AGrid); end; function DeleteSelectRows; var i,j,C: integer; begin Result:=False; with AGrid do if Selection.Top >= 0 then begin C:=Selection.Bottom - Selection.Top + 1; for i:=Selection.Bottom + 1 to RowCount - 1 do for j:=0 to ColCount - 1 do Cells[j,i - C]:=Cells[j,i]; RowCount:=RowCount - C; Result:=True; end end; function DeleteSelectCols; var i,j,C: integer; begin Result:=False; with AGrid do if Selection.Left >= 0 then begin C:=Selection.Right - Selection.Left + 1; for i:=Selection.Right + 1 to ColCount - 1 do for j:=0 to RowCount - 1 do Cells[i - C,j]:=Cells[i,j]; ColCount:=ColCount - C; Result:=True; end end; function UpSelectRows; var newindex,oldindex,j: integer; S: string; R: TGridRect; begin Result:=False; with AGrid do if Selection.Top > AGrid.FixedRows then begin oldindex:=Selection.Top; newindex:=oldindex - 1; for j:=0 to ColCount - 1 do begin S:=Cells[j,oldindex]; Cells[j,oldindex]:=Cells[j,newindex]; Cells[j,newindex]:=S; end; R.Top:=newindex; R.Bottom:=newindex; R.Left:=Selection.Left; R.Right:=Selection.Right; Selection:=R; Result:=True; end end; function DownSelectRows; var newindex,oldindex,j: integer; S: string; R: TGridRect; begin Result:=False; with AGrid do if Selection.Top < RowCount - 1 then begin oldindex:=Selection.Top; newindex:=oldindex + 1; for j:=0 to ColCount - 1 do begin S:=Cells[j,oldindex]; Cells[j,oldindex]:=Cells[j,newindex]; Cells[j,newindex]:=S; end; R.Top:=newindex; R.Bottom:=newindex; R.Left:=Selection.Left; R.Right:=Selection.Right; Selection:=R; Result:=True; end end; end.