Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » ArrayCtrl, HeaderCtrl & GridCtrl » GridCtrl empty cells pasted from Excel
GridCtrl empty cells pasted from Excel [message #17362] Sat, 09 August 2008 01:11 Go to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Hello all

First of all thanks to Ultimate++ developers because this is something I wait many time ago. It is simple, original and powerful!

Second the question: I found that when pasting a row on a GridCtrl copied from excel or notepad the empty cells are simply not copied.

For example:

Copied from Excel: Hello/(empty)/World
Pasted in GridCtrl: Hello/World/(empty)

However if this is copied from GridCtrl its is pasted to perfectly.

Surfing through the code I have found that in GridCtrl.cpp, function void GridCtrl::Paste(int mode), line 791, on

...
for(int i = 0; i < lines.GetCount(); i++)
{
Vector<String> cells = Upp::Split(lines[i], '\t');
for(int j = 0; j < cells.GetCount(); j++)
...

If I put Upp::Split(lines[i], '\t', false); instead, the row is copied Ok including empty cells.

Another comment: When copying to the clipboard from GridCtrl it seems is included a empty cell at the end (an extra '\t' character).

Best regards






Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17670 is a reply to message #17362] Mon, 25 August 2008 12:31 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Well, I have been working with it but still there is something wrong, as I get an exception and the date fields are not properly filled (at least using a non english date format and scan).

But in the other side, I think "Upp::Split(lines[i], '\t');" is not right as it discards empty cells that would have to be copied empty.

Still working in it. Any idea from unodgs?


Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17674 is a reply to message #17670] Mon, 25 August 2008 14:37 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

koldo wrote on Mon, 25 August 2008 06:31

Well, I have been working with it but still there is something wrong, as I get an exception and the date fields are not properly filled (at least using a non english date format and scan).

But in the other side, I think "Upp::Split(lines[i], '\t');" is not right as it discards empty cells that would have to be copied empty.

Still working in it. Any idea from unodgs?


Maybe excel fills clipboard in his own format too (not only as text). Then it would be easier to paste it to gridctrl. I must find some information about it. But I'll also check if there is a way to parse text data properly.
Re: GridCtrl empty cells pasted from Excel [message #17675 is a reply to message #17674] Mon, 25 August 2008 14:51 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

Excel seem to be using XlTable to store table in clipboard. However I was not able to find a good documentation about it. Maybe I'll try once again Smile
Re: GridCtrl empty cells pasted from Excel [message #17677 is a reply to message #17675] Mon, 25 August 2008 15:07 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

Ok, I think I found the reason Smile
Just replace:
Vector<String> cells = Upp::Split(lines[i], '\t');

with
Vector<String> cells = Upp::Split(lines[i], '\t', false);


Split skips empty strings by default...

[Updated on: Mon, 25 August 2008 15:15]

Report message to a moderator

Re: GridCtrl empty cells pasted from Excel [message #17683 is a reply to message #17677] Mon, 25 August 2008 22:02 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Hello uno

I have tried that fix that seems logic but when pasting I get an exception in linux and in windows XP:

Assertion failed in /home/.../upp/uppsrc/Core/Value.h, line 442
Invalid value conversion: N3Upp12RichValueRespINS_6StringEEE -> d

In both S.O. I copy the row from the program and paste it in Notepad or Gedit. I select it again including the last \t and I paste it to the program, getting the exception.

When I run the program again I see the row pasted right for all the cells but the Date field. For example: "08/25/2008" is pasted as "00/08/ 8"

Best regards


Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17684 is a reply to message #17683] Mon, 25 August 2008 23:03 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

Could you past here the code resposible for grid initialization (adding columns, setting conventers etc)?
Re: GridCtrl empty cells pasted from Excel [message #17694 is a reply to message #17362] Tue, 26 August 2008 11:16 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Hello unodgs

Sorry. I have found the problem in my program but I do not know how to fix it.

The problem comes from a field with "SetDisplay" that gets the exception when pasting from Notepad and corrupts the Date field beside it as described in the posts.

For example this works right:
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).Edit(isMarried).Default(-1);

and this wrong:
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).SetDisplay(Single<DispPM>()).Edit(isMarried).Default(-1);

isMarried is declared this way:
DropGrid isMarried;

And initialized this way:
isMarried.Add(1, t_("Yes")).Add(-1, t_("No"));

So I want to store an integer from a DropGrid with strings.

Going to DisPM, as I want to see "Yes" or "No" in the cells, this is the original version that works properly with rows copied directly from GridCtrl but gets an exception if copied from Notepad:

struct DispPM : GridDisplay
{
virtual void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style, Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
String text;
if(!val.IsNull())
text = int(val) > 0 ? "Yes" : "No";
else
text = "";
GridDisplay::Paint(w, x, y, cx, cy, Value(text), style, fg, bg, fnt, found, fs, fe);
}
};

This other version lets paste from Notepad but does not show "Yes" or "No" but empty cells

struct DispPM : GridDisplay
{
virtual void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style, Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
w.DrawRect(x, y, cx, cy, bg);
if(!val.IsNull())
w.DrawText(x, y, int(val) > 0 ? "Yes" : "No", fnt, fg);
GridDisplay::Paint(w, x, y, cx, cy, Value(""), style, fg, bg, fnt, found, fs, fe);
}
};

Do you have any idea?


Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17695 is a reply to message #17362] Tue, 26 August 2008 12:07 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Well, I have found the problem.

This function gets the exception in "int(val)" because when pasted from outside GridCtrl, a "1" instead of being an int is a string!!

The function:

struct DispPM : GridDisplay
{
virtual void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style, Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
String text;
if(!val.IsNull())
text = int(val) > 0 ? "Yes" : "No";
else
text = "";
GridDisplay::Paint(w, x, y, cx, cy, Value(text), style, fg, bg, fnt, found, fs, fe);
}
};

So changing
text = int(val) > 0 ? "Yes" : "No";

with
if (val.Is<String>()) {
String s = val;
if (s == "1")
text = "Yes";
else
text = "No";
} else
texto = int(val) > 0 ? "Yes" : "No";

it works.

The simplest way to get the exception is:

String s = "1";
Value val = s;
int i = int(v);

Question: Is there a way to convert a Value to int in a safe way?


Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17696 is a reply to message #17694] Tue, 26 August 2008 12:08 Go to previous messageGo to next message
unodgs is currently offline  unodgs
Messages: 1366
Registered: November 2005
Location: Poland
Ultimate Contributor

koldo wrote on Tue, 26 August 2008 05:16

Hello unodgs

Sorry. I have found the problem in my program but I do not know how to fix it.

The problem comes from a field with "SetDisplay" that gets the exception when pasting from Notepad and corrupts the Date field beside it as described in the posts.

For example this works right:
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).Edit(isMarried).Default(-1);

and this wrong:
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).SetDisplay(Single<DispPM>()).Edit(isMarried).Default(-1);


in this case much better is to use conveter. DropGrid implements Convert interface so you can just write:
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).Edit(isMarried).SetConvert(isMarried).Default(-1);

or in short form
myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).EditConvert(isMarried).Default(-1);

Quote:


isMarried is declared this way:
DropGrid isMarried;

And initialized this way:
isMarried.Add(1, t_("Yes")).Add(-1, t_("No"));

So I want to store an integer from a DropGrid with strings.

Going to DisPM, as I want to see "Yes" or "No" in the cells, this is the original version that works properly with rows copied directly from GridCtrl but gets an exception if copied from Notepad:

struct DispPM : GridDisplay
{
virtual void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style, Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
String text;
if(!val.IsNull())
text = int(val) > 0 ? "Yes" : "No";
else
text = "";
GridDisplay::Paint(w, x, y, cx, cy, Value(text), style, fg, bg, fnt, found, fs, fe);
}
};

This other version lets paste from Notepad but does not show "Yes" or "No" but empty cells

struct DispPM : GridDisplay
{
virtual void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style, Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
w.DrawRect(x, y, cx, cy, bg);
if(!val.IsNull())
w.DrawText(x, y, int(val) > 0 ? "Yes" : "No", fnt, fg);
GridDisplay::Paint(w, x, y, cx, cy, Value(""), style, fg, bg, fnt, found, fs, fe);
}
};

Do you have any idea?

When you paste data from excel clipboard you in fact copy String values into the grid's cells. Now in your code you cast String value to int value and there is your exception. In DropGrid case everything is fine as it stores in cell integer value.

It's a serious problem. I think grid should provide WhenPasteCell(int x, int y, Value& v) callback when you could convert String value object to proper type (like int). I'll try to do sth with it. Maybe I'll add some kind of auto conventer too.
Re: GridCtrl empty cells pasted from Excel [message #17699 is a reply to message #17362] Tue, 26 August 2008 16:11 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Well, I have changed SetPaint by SetConvert(Single<ConvBool>()) defined as:

struct ConvBool : Convert
{
Value Format(const Value &q) const
{
if (!q.IsNull()) {
if (q.Is<String>())
return (String(q) == "1") ? "Yes" : "No";
else if (IsNumber(q))
return (int(q) == 1) ? "Yes" : "No";
else
return "¿?";
} else
return Null;
}
};

so SetConvert asssures the way some column is going to be shown as text, as SetPaint sets the way the column is going to be painted as a draw o picture.

Anyway, to questions:
- Is there any way to convert safely a Value to a String?.
- When you put "myGridCtrl.AddColumn(ISMARRIED, t_("Is married?")).EditConvert(isMarried).Default(-1);", how do you define the struct Convert with "Value Format(const Value &q) const" inside?


Best regards
Iñaki
Re: GridCtrl empty cells pasted from Excel [message #17700 is a reply to message #17699] Tue, 26 August 2008 16:24 Go to previous message
koldo is currently offline  koldo
Messages: 3358
Registered: August 2008
Senior Veteran
Well, surfing through the upp source I have found and test the first question.

- Is there any way to convert safely a Value to a String?.
Yes, it is ToString(), so the function Format is now:

struct ConvBool : Convert
{
Value Format(const Value &q) const
{
if (!q.IsNull()) {
return (q.ToString() == "1") ? "Yes" : "No";
else
return Null;
}
};


Best regards
Iñaki
Previous Topic: adding row from another window and updating GUI
Next Topic: Unicode character paste in GridCtrl
Goto Forum:
  


Current Time: Mon Apr 29 06:56:56 CEST 2024

Total time taken to generate the page: 0.03374 seconds