Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Pegar de Clipboard a StringGrid (https://www.clubdelphi.com/foros/showthread.php?t=81684)

Cristhor1982 07-12-2012 21:29:55

Pegar de Clipboard a StringGrid
 
var
Vtextbuf:string;
Vnren:integer;
columna:integer;
begin
if Clipboard.HasFormat(CF_TEXT) then
begin
Vtextbuf:= Clipboard.AsText;
Vnren:=1;
stringgrid1.RowCount:=2;
columna:=strtoint(edit1.text);

while (pos(#13,Vtextbuf)>0)and(length(Vtextbuf)>2) do
begin
stringgrid1.Cells[Columna,Vnren]:=copy(Vtextbuf,1,pos(#13,Vtextbuf)-1);
delete(Vtextbuf,1,pos(#13#10,Vtextbuf)+1);

if (length(Vtextbuf)>2) then
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
Vnren:=Vnren+1;
end;
// ShowMessage(Vtextbuf);
end;
bt_validar.Click;
end
else
ShowMessage('No hay texto en el PortaPapeles');

Este Codigo me entrega lo copiado pero no el ultimo valor que puede ser?

Casimiro Notevi 07-12-2012 22:13:50

Cita:

Empezado por Cristhor1982 (Mensaje 451432)
...

Bienvenido a clubdelphi, ¿ya leiste nuestra guía de estilo?, gracias por tu colaboración :)

Recuerda poner los tags al código fuente, ejemplo:



Gracias :)

ecfisa 07-12-2012 23:02:44

Hola Cristhor1982.

Si no interpreté mal la finalidad del código, yo haría:
Código Delphi [-]
...
var
  TS: TStrings;
begin
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    TS := TStringList.Create;
    try
      TS.Text := clipboard.AsText;
      StringGrid1.RowCount := TS.Count;
      StringGrid1.Cols[StrToInt(Edit1.Text)]:= TS;
    finally
      TS.Free;
    end;
  end
  else
    ShowMessage('No hay texto en el PortaPapeles');
end;

Saludos.

nlsgarcia 08-12-2012 03:02:38

Cristhor1982,

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ClipBrd;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ClipBoard2StringGrid: TButton;
    StringGrid2Clipboard: TButton;
    txtRows: TEdit;
    Label1: TLabel;
    txtCols: TEdit;
    Label2: TLabel;
    procedure ClipBoard2StringGridClick(Sender: TObject);
    procedure StringGrid2ClipboardClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Divide una cadena de Texto por palabras
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

// Copia de Clipboard a StringGrid
procedure TForm1.ClipBoard2StringGridClick(Sender: TObject);
var
   StrList : TStringList;
   Row, Col, Data : Integer;

begin
   StrList := TStringList.Create;

   if Clipboard.HasFormat(CF_TEXT) then
   begin
      Split(' ',Clipboard.AsText,StrList);
      StringGrid1.FixedRows := 1;
      StringGrid1.FixedCols := 1;
      StringGrid1.RowCount := StrToIntDef(txtRows.Text,1)+1; // Por defecto una fila
      StringGrid1.ColCount := StrToIntDef(txtCols.Text,1)+1; // Por defecto una columna

      for Row:= 1 to StringGrid1.RowCount do
         StringGrid1.Rows[Row].Clear;

      for Col:= 1 to StringGrid1.ColCount do
         StringGrid1.Cols[Col].Clear;

      Data := 0;
      for Row:= 1 to StringGrid1.RowCount do
         for Col:= 1 to StringGrid1.ColCount-1 do
         begin
            if (Data <= StrList.Count-1) then
            begin
               StringGrid1.Cells[Col,Row] := StrList.Strings[Data];
               inc(Data);
            end
         end
   end;

   StrList.Free;
end;

// Copia de StringGrid a Clipboard
procedure TForm1.StringGrid2ClipboardClick(Sender: TObject);
var
   StringData : String;
   Row,Col : Integer;

begin
   Clipboard.Clear;

   StringData := '';

   for Row := 1 to StringGrid1.RowCount do
   begin
      for Col := 1 to StringGrid1.ColCount-1 do
      begin
         if (StringGrid1.cells[Col,Row] <> ' ') then
            StringData := StringData + StringGrid1.cells[Col,Row] + #9;
      end;
      StringData := StringData + #13 + #10;
   end;

   Clipboard.AsText := StringData;
end;

end.
El código anterior permite copiar por medio del Clipboard filas y columnas de data desde y hacia un control TStringGrid, es práctico para el intercambio de data con Excel.

Espero sea útil :)

Nelson.

Cristhor1982 10-12-2012 13:34:49

No logro pegar desde la Row 1 de la grilla, siempre comienza de la Row 0, me puedes ayudar, gracias

nlsgarcia 10-12-2012 16:27:31

Cristhor1982,

El código que te indique anteriormente define de forma dinámica las filas y columnas del control TStringGrid y permite copiar desde y hacia el control por medio del Clipboard, quizás esta solución se pueda adaptar a tu proyecto.

Espero sea útil :)

Nelson.

ecfisa 10-12-2012 17:47:11

Cita:

Empezado por Cristhor1982 (Mensaje 451556)
No logro pegar desde la Row 1 de la grilla, siempre comienza de la Row 0, me puedes ayudar, gracias

Hola.

De este modo pegará los valores en la columna y a partir de la fila que indique la celda seleccionada actualmente:
Código Delphi [-]
var
  TS: TStrings;
  i : Integer;
begin
  if Clipboard.HasFormat(CF_TEXT) then
    with StringGrid1 do
    begin
      TS := TStringList.Create;
      try
        TS.Text := clipboard.AsText;
        RowCount := TS.Count;
        for i:= 0 to TS.Count-1 do
          Cells[Col, StringGrid1.Row+i]:= TS[i]; //(*)
      finally
        TS.Free;
      end;
  end
end;
Si deseas utilizar el edit para seleccionar la columna, reemplaza la línea (*) por:
Código Delphi [-]
   Cells[StrToInt(Edit1.Text),StringGrid1.Row+i]:= TS[i];

Saludos.

Cristhor1982 11-12-2012 17:17:55

Muchas gracias, me parece que voy a probar poniendole algun ciclo...

Cristhor1982 11-12-2012 17:27:17

Ecfisa
Muchas Gracias...Resulto a la perfeccion :)

Cristhor1982 18-12-2012 20:55:51

Código Delphi [-]
procedure TFR_EliminacionFolioMasivo.SG1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var
  TS: TStrings;
  j,i : Integer;
begin
if ((ssCtrl in Shift) and (Key = 86)) or ((ssShift in Shift) and (Key = 45)) then
  if Clipboard.HasFormat(CF_TEXT) then
    with SG1 do
    begin
      TS := TStringList.Create;
      try
        TS.Text := clipboard.AsText;
        RowCount := TS.Count+1;
        for i:= 0 to TS.Count-1 do
        begin


          Cells[SG1.Col,SG1.Row+i]:= TS[i];   //desde row 1

          screen.Cursor := crsqlwait;
          fr_clave.SQL.Close;
              fr_clave.SQL.SQL.Clear;
              fr_clave.SQL.SQL.Add('EXEC_aCTeLIMINACIONfOLIOv2 1,0,');
              if ((Cells[1,1+i]<>'')AND ((Cells[2,1+i])<>'')) then
                begin
                 fr_clave.SQL.SQL.Add(''''+ Cells[1,1+i] +''',''' + Cells[2,1+i] +''',0,0,0,');
                 end
                 else
                 if ((Cells[1,1+i]<>'') AND (Cells[2,1+i]='')) then
                 begin
                 fr_clave.SQL.SQL.Add(''''+ Cells[1,1+i] +''',-1,0,0,0,')
                 end
                else
                fr_clave.SQL.SQL.Add('-1,''' + Cells[2,1+i] +''',0,0,0,');
                fr_clave.SQL.SQL.Add(''''+ fr_clave.TX_Rut.Text +''',0,0,0');
                fr_clave.SQL.Open;
                  if fr_clave.SQL.RecordCount <> 0 then
                  BEGIN

                 // SG1.RowCount :=SG1.RowCount+1;
                  SG1.Cells[1,i+1 ] := fr_clave.SQL['Folio'];
                  SG1.Cells[2,i+1 ] := fr_clave.SQL['Vale'];
                  SG1.Cells[3,i+1 ] := fr_clave.SQL['Periodo'];
                  SG1.Cells[4,i+1] := fr_clave.SQL['RutVisador'];
                  SG1.Cells[6,i+1 ] := fr_clave.SQL['RutVisado'];
                  SG1.Cells[8,i+1 ] := fr_clave.SQL['Motivo'];
                  SG1.Cells [5,i+1 ] := NombreTrabajador(SG1.Cells[4,i+1]);
                  SG1.Cells [7,i+1 ] := NombreTrabajador(SG1.Cells[6,i+1]);

          end;
          for j := 0 to 8 do
          begin
          SG1.Cells[j, SG1.RowCount] := '';
          end;
          end;
      finally
        screen.Cursor := crdefault;
        TS.Free;
        //clipboard.Clear;

    end;
end
  else
    ShowMessage('No hay texto en el PortaPapeles');
end;



Esto esta funcionando "Bien", pero necesito que los valores Vacios de lo que copio no los inserte, trate con un IF TS[i]='' then..etc
pero no puedo, alguien que me de una pista

nlsgarcia 18-12-2012 22:42:20

Cristhor1982,

Cita:

Empezado por Cristhor1982
Necesito que los valores Vacios de lo que copio no los inserte, trate con un IF TS[i]='' then..etc pero no puedo

La solución que te propuse en el Mensaje #4 tiene el siguiente código:
Código Delphi [-]
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;
Luego, si hay texto en el Clipboard este es procesado:
Código Delphi [-]
 if Clipboard.HasFormat(CF_TEXT) then
   begin
      Split(' ',Clipboard.AsText,StrList);
   ...
De esta forma solo se copia al TStringList los valores diferentes de blanco del Clipboard, usando el carácter blanco como separador de data.

Adicionalmente:
Código Delphi [-]
IF TS[i]='' Then
No verifica si el valor es blanco, solo verifica si el valor es empty, contrariamente:
Código Delphi [-]
IF TS[i]=' ' Then
Si verifica si el valor es blanco.

Te sugiero probar ambas opciones e implementes la que mejor se adapte a tu proyecto.

Espero sea útil :)

Nelson.

Cristhor1982 21-12-2012 17:15:16

Cita:

Empezado por nlsgarcia (Mensaje 452079)
Cristhor1982,


La solución que te propuse en el Mensaje #4 tiene el siguiente código:
Código Delphi [-]
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;
Luego, si hay texto en el Clipboard este es procesado:
Código Delphi [-]
 if Clipboard.HasFormat(CF_TEXT) then
   begin
      Split(' ',Clipboard.AsText,StrList);
   ...
De esta forma solo se copia al TStringList los valores diferentes de blanco del Clipboard, usando el carácter blanco como separador de data.

Adicionalmente:
Código Delphi [-]
IF TS[i]='' Then
No verifica si el valor es blanco, solo verifica si el valor es empty, contrariamente:
Código Delphi [-]
IF TS[i]=' ' Then
Si verifica si el valor es blanco.

Te sugiero probar ambas opciones e implementes la que mejor se adapte a tu proyecto.

Espero sea útil :)

Nelson.

Al final lo solucione con una variable x que almacenaba y luego en un ciclo le preguntaba si era '' ...y funciono.. Gracias

Cristhor1982 23-05-2013 00:59:03

Ayuda (2) - Clipboard hacia TStringGrid
 
Amigos, de nuevo con el problema para pegar desde clipboard a TStringGrid...

Ahora necesito pegar esto

Equipo 12345

Fecha/Hora 23-may-2013 11:49:11 CLT

Horometro 1111

Operador 12

Cancha 20

Insumo 6789

Cantidad 121

y quede de esta forma

Equipo Fecha/Hora Horometro Operador Cancha Insumo Cantidad
15503 23-may-2013 11:49:11 CLT 1515 363 32 50085645 121


POR FAVOR AYUDA...Llevo unos dias tratando y me pega en cualquier lado....


SAludos

ecfisa 23-05-2013 09:47:47

Hola Cristhor1982.

Recuerda que: preguntas diferentes = hilos diferentes (Punto 8 de la guía de estilo).

Saludos y gracias por tu colaboración :)


La franja horaria es GMT +2. Ahora son las 22:39:20.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi