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 |
|
koldo
Messages: 3362 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 #17674 is a reply to message #17670] |
Mon, 25 August 2008 14:37 |
|
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 #17694 is a reply to message #17362] |
Tue, 26 August 2008 11:16 |
|
koldo
Messages: 3362 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 |
|
koldo
Messages: 3362 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 |
|
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 #17700 is a reply to message #17699] |
Tue, 26 August 2008 16:24 |
|
koldo
Messages: 3362 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
|
|
|
Goto Forum:
Current Time: Sun May 19 16:56:10 CEST 2024
Total time taken to generate the page: 0.00801 seconds
|