3.0.0

Copy & Paste

Does this component support copy & paste. I need to use data that I have in Excel to edit the datagrid. Is that possible?

Thnx in advance
Lorenzo
November 24,
The grid will load a CVS file which excel will save. but it won't modify a xls directly
J
November 24,
Hello,

Here is an example of copy-paste from the grid to excel and from Excel to the grid, using Internet Explorer.
Notes :
. When copying from the grid, Ctrl-C copies all cells not just the selected ones. (Ctrl-A does not select all cells, I have to implement that later.)
. This code copies only visible cells, and pastes only in cells both visible and editable.
. In the code below, awgrid is the grid instance which you have to replace by your own variable name.

awgrid.setEvent("onkeydown", function (event)
{
if(event.ctrlKey)
{
var key=event.keyCode;
if (key==67) // Ctrl-c copies all visible cells into the clipboard
{
var text = "";
if (ctlA)
{
var rowIndices = awgrid.getRowIndices();
var nbShownRows = rowIndices.length;
var colIndices = awgrid.getColumnIndices();
var nbShownColumns = colIndices.length;
for (var r=0; r<nbShownRows; r++)
{
for (var c=0; c<nbShownColumns; c++)
{
text += awgrid.getCellText(colIndices[c], rowIndices[r]);
if (c!=nbShownColumns -1) text += "\t"
}
if (r!=nbShownRows -1) text += "\n"
}
//ctlA = false;
}
else
{
var c = awgrid.getCurrentColumn();
var r = awgrid.getCurrentRow();
text = awgrid.getCellText(c, r);
}
clipboardData.setData("Text", text);
}
if (key==86) // Ctrl-v
{
text = clipboardData.getData("Text");
if (ctlA)
{
var rowIndices = awgrid.getRowIndices();
var nbShownRows = rowIndices.length;
var colIndices = awgrid.getColumnIndices();
var nbShownColumns = colIndices.length;
var cCurrent = awgrid.getCurrentColumn();
var cCurrentTrueIdx = 0;
for(var i=0; i<nbShownColumns; i++)
{
if (colIndices[i]==cCurrent)
{
cCurrentTrueIdx = i;
break;
}
}
var rCurrent = awgrid.getCurrentRow();
var rCurrentTrueIdx = 0;
for(var i=0; i<nbShownRows; i++)
{
if (rowIndices[i]==rCurrent)
{
rCurrentTrueIdx = i;
break;
}
}
var copiedRows = text.split('\r\n');
if (copiedRows.length-1 > nbShownRows)
{
var nbMissingRows = copiedRows.length-1 -nbShownRows;
if (nbMissingRows==1)
alert("Your copied data has too many rows for this grid, the final row will not be pasted.");
else
alert("Your copied data has too many rows for this grid, the " + nbMissingRows + " final rows will not be pasted.");
}
nbRows = Math.min(copiedRows.length-1, nbShownRows);
var alertColsDisplayed = false;
for(var r=0; r<nbRows; r++)
{
var nbUsableColumns = nbShownColumns - cCurrent;
var items = copiedRows[r].split('\t');
if (items.length > nbUsableColumns && !alertColsDisplayed)
{
var nbMissingCols = items.length - nbUsableColumns;
if (nbMissingCols==1)
alert("Your copied data has too many columns for this grid, the final column will not be pasted.");
else
alert("Your copied data has too many columns for this grid, the " + nbMissingCols + " final columns will not be pasted.");
alertColsDisplayed = true;
}
nbUsableColumns = Math.min(items.length, nbUsableColumns);
for(var c=0; c<nbUsableColumns; c++)
{
if (awgrid.getCellEditable(colIndices[cCurrentTrueIdx*1 +c], rowIndices[rCurrentTrueIdx*1 +r]))
awgrid.setCellText(items[c], colIndices[cCurrentTrueIdx*1 +c], rowIndices[rCurrentTrueIdx*1 +r]);
}
}
//ctlA = false;
}
else
{
awgrid.setCellText(text, cCurrent, rCurrent);
}
}
}
});
Franck
November 24,
Wow! Thanx for the fast response.
This is just what i needed. Thank u very much

Lorenzo
November 24,

This topic is archived.

See also:


Back to support forum

Search

Version 2

Looking for AW 2.6 ?
The old site is here.