Jump to content

Paste text to a grid


augusto.pellis

Recommended Posts

6 hours ago, augusto.pellis said:

could anyone suggest me how to, after having copied cells content from an xls file, paste it to a uni grid, having each excel cell value pasted into a unigrid cell?

Hello,

I will give you the sequence of actions (one of the possible solutions of course, also you should think about error handling)...

1. UniDBGrid1.ClientEvents.ExtEvents ->

function keydown(e, t, eOpts) 
{
    var grid = this.grid;
    grid.arr = [];
    grid.arrJSON = [];
    var models = this.grid.getStore().getRange();
    if (e.ctrlKey && e.keyCode == 86) {
        navigator.clipboard.readText()
            .then(text => {
                grid.arr = text.split("\n");
                if (grid.arr.length == 1) {
                    grid.arr = text.split("\n");
                } else {
                    grid.arr.splice(-1, 1);
                }

                grid.arr.forEach(function(a, r) {
                    a.split('\t').forEach(function(b, c) {
                        console.log(b, r, c);
                        try {
                            grid.arrJSON.push({
                                "r": grid.uniRow + r,
                                "c": grid.uniCol + c,
                                "v": b
                            });
                            //models[grid.uniRow + r].set(String(grid.uniCol + c), b)
                        } catch (err) {}
                    })
                });
                ajaxRequest(grid, 'copypaste', ['val='+JSON.stringify(grid.arrJSON)]);
            })
            .catch(err => {
                console.error('Failed to read clipboard contents: ', err);
            });
    }
}

2.

Uses ..., XSuperJSON, XSuperObject;

https://github.com/onryldz/x-superobject

3. 

type
  TRec = record
    R: Integer;
    C: Integer;
    V: string;
  end;

4. UniDBGrid1.OnAjaxEvent

procedure TMainForm.UniDBGrid1AjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
var
  Test: TArray<TRec>;
  S: String;
  I: Integer;
begin
  if EventName = 'copypaste' then
  begin
    S := Params.Values['val'];
    Test := TJSON.Parse<TArray<TRec>>(S);
    with (Sender as TUniDBGrid) do
    begin
      BeginUpdate;
      for I := 0 to High(Test) do
      begin
        DataSource.DataSet.RecNo := Test[I].R+1;
        DataSource.DataSet.Edit;
        DataSource.DataSet.Fields[Test[I].C].AsString := Test[I].V;
        DataSource.DataSet.Post;
      end;
      EndUpdate;
    end;
  end;

end;

Try...

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...