Hi,

While copying data from EditGrid, I see that the column names are not
copied. This makes the data extremely ambiguous to use when pasting directly
into excel or a similar spreadsheet application. Attached is a patch which
prepends the column names in select cases of data copy from the EditGrid.

Cases when the column names are prepended to the dataset:
1. Selection of the complete Grid
2. Selection of one (or more) columns
3. Selection of all the columns of one (or more rows)

Case when the column names are not prepended:
1. Selection of columns less than the max number of columns. i.e. any subset
of the data-set when a complete row / column is not selected, would have
only data and not the respective column names.

p.s.: I hope my fixation for EditGrid isn't becoming a problem :)

Regards,
*Robins Tharakan*
Index: pgadmin/ctl/ctlSQLGrid.cpp
===================================================================
--- pgadmin/ctl/ctlSQLGrid.cpp	(revision 7164)
+++ pgadmin/ctl/ctlSQLGrid.cpp	(working copy)
@@ -112,6 +112,52 @@
     return str;
 }
 
+wxString ctlSQLGrid::GetExportColName(int col1, int col2)
+{
+    wxArrayInt cols;
+    wxString str;
+    int i;
+
+    if (col2 < col1)
+        return str;
+
+    cols.Alloc(col2 - col1 + 1);
+    for (i = col1; i <= col2; i++) 
+	{
+        cols.Add(i);
+    }
+
+    return GetExportColName(cols);
+}
+
+wxString ctlSQLGrid::GetExportColName(wxArrayInt cols)
+{
+    wxString str;
+    unsigned int col;
+
+    if (GetNumberCols() == 0)
+        return str;
+
+    for (col=0 ; col < cols.Count() ; col++)
+    {
+        if (col > 0)
+            str.Append(settings->GetCopyColSeparator());
+
+        wxString text = GetColLabelValue(cols[col]).BeforeFirst('\n');
+
+		bool needQuote  = false;
+		/* Quote everything */
+		needQuote = true;
+
+		if (needQuote)
+            str.Append(settings->GetCopyQuoteChar());
+        str.Append(text);
+        if (needQuote)
+            str.Append(settings->GetCopyQuoteChar());
+    }
+    return str;
+}
+
 int ctlSQLGrid::Copy()
 {
     wxString str;
@@ -121,8 +167,17 @@
     if (GetSelectedRows().GetCount()) 
 	{
         wxArrayInt rows = GetSelectedRows();
+        int max_cols = GetNumberCols();
+        
+        if (rows.GetCount() > 0)
+        {
+            str.Append(GetExportColName(0, max_cols - 1));
+            str.Append(END_OF_LINE);
+        }
 
-        for (i=0 ; i < rows.GetCount() ; i++)
+        // Copy the rows requested, but don't copy the last line (its a 
+        // blank line and not data)
+        for (i=0 ; i < rows.GetCount() && i < GetNumberRows() - 1 ; i++)
         {
             str.Append(GetExportLine(rows.Item(i)));
     
@@ -135,10 +190,16 @@
     else if (GetSelectedCols().GetCount()) 
 	{
         wxArrayInt cols = GetSelectedCols();
-        size_t numRows = GetNumberRows();
+        size_t numRows = GetNumberRows() - 1; 
 
-        for (i=0 ; i < numRows ; i++)
+        if (numRows > 0)
         {
+            str.Append(GetExportColName(cols));
+            str.Append(END_OF_LINE);
+        }
+        
+        for (i=0 ; i < numRows; i++)
+        {
             str.Append(GetExportLine(i, cols));
     
             if (numRows > 1)
@@ -156,7 +217,21 @@
         x2 = GetSelectionBlockBottomRight()[0].GetCol();
         y1 = GetSelectionBlockTopLeft()[0].GetRow();
         y2 = GetSelectionBlockBottomRight()[0].GetRow();
-
+        
+        // If the selection includes all the columns, provide the 
+        // column names along with copied data
+        if ((x2 - x1 + 1) == GetNumberCols())
+        {
+            str.Append(GetExportColName(x1, x2));
+            str.Append(END_OF_LINE);
+        }
+        
+        // Don't copy the last line (its a blank line and not data)
+        if (y2 >= GetNumberRows() - 1)
+        {
+            y2 = GetNumberRows() - 2;
+        }
+        
         for (i = y1; i <= y2; i++) 
 		{
             str.Append(GetExportLine(i, x1, x2));
Index: pgadmin/include/ctl/ctlSQLGrid.h
===================================================================
--- pgadmin/include/ctl/ctlSQLGrid.h	(revision 7164)
+++ pgadmin/include/ctl/ctlSQLGrid.h	(working copy)
@@ -25,6 +25,8 @@
     wxString GetExportLine(int row);
     wxString GetExportLine(int row, wxArrayInt cols);
     wxString GetExportLine(int row, int col1, int col2);
+    wxString GetExportColName(wxArrayInt cols);
+    wxString GetExportColName(int col1, int col2);
     virtual bool IsColText(int col) { return false; }
     int Copy();
 
-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to