TStringGrid copy/paste to/from MS Excel and OO Calc bug
Original Reporter info from Mantis: K155LA3
-
Reporter name:
Original Reporter info from Mantis: K155LA3
- Reporter name:
Description:
When copy cell with quote symbol procedure TCustomStringGrid.CopyCellRectToClipboard get string as " but after function QuoteText it will converted to """".
When paste from clipboard to StringGrid it set cell data to ".
When paste from clipboard to OO Calc it set cell data to "".
When copy cell with " from OO Calc clipboard contained this data: "# 13#10
When paste from clipboard to StringGrid it clear cell data (make empty cell).
When paste from clipboard to OO Calc it set cell data to ".
Symbol "1" in cell:
StringGrid -> clipboard = """1"""# 13# 10
OO Calc -> clipboard = "1"# 13# 10
MS Excel -> clipboard = "1"# 13# 10
StringGrid -> MS Excel = "1"
StringGrid -> OO Calc = ""1""
OO Calc -> MS Excel = "1"
MS Excel -> OO Calc = "1"
But...
When I copy "1"# 13# 10 to clipboard from text file:
clipboard -> StringGrid = 1
clipboard -> OO Calc = 1
clipboard -> MS Excel = 1
When I copy """1"""# 13# 10 to clipboard from text file:
clipboard -> StringGrid = "1"
clipboard -> OO Calc = ""1""
clipboard -> MS Excel = "1"
If fill 3x3 cells in Excel and Calc:
1 1 1
1 1 1
1 1 1
And fill 3x3 cells in StringGrid:
2 2 2
2 2
2
And copy it from StringGrid and paste for replace cells:
Excel:
2 2 2
2 2
2
Calc:
2 2 2
2 2 1
2 1 1
If copy 3x3 cells from Excel:
2 2 2
2 2
2
And paste it to Calc:
2 2 2
2 2
2
Steps to reproduce:
1 Place StringGrid to form.
2 Set in Options goEditing.
3 Run programm.
4 Place symbol " in one cell
5 Try copy/paste to/from MS Excel or OO Calc.
6 Fill 3x3 cell as above.
7 Try copy/paste to/from MS Excel or OO Calc.
Additional information:
MS Excel and OO Calc when copy cells to clipboard set this clipboard flag:
CF_Text = True
CF_Bitmap = False
CF_Picture = True
CF_MetaFilePict = False
CF_Object = False
CF_Component = False
CF_HTML = True
If CF_HTML = True then, MS Excel and OO Calc ignore text data and parse HTML data from clipboard.
Fix (only copy cells from StringGrid to Excel/Calc):
procedure TCustomStringGrid.CopyCellRectToClipboard(const R: TRect);
var
- SelStr: String;
+ SelStr, SelHTMLStr: String;
+ rflags: TReplaceFlags;
aRow,aCol,k: LongInt;
function QuoteText(s: string): string;
begin
DoCellProcess(aCol, aRow, cpCopy, s);
if (pos(#9, s)>0) or
(pos(#10, s)>0) or
(pos(#13, s)>0) or
(pos('"', s)>0)
then
result := AnsiQuotedStr(s, '"')
else
result := s;
end;
begin
SelStr := '';
+ SelHTMLStr := '<table>';
for aRow:=R.Top to R.Bottom do begin
+ SelHTMLStr := SelHTMLStr + '<tr>';
for aCol:=R.Left to R.Right do begin
if Columns.Enabled and (aCol>=FirstGridColumn) then begin
k := ColumnIndexFromGridColumn(aCol);
if not Columns[k].Visible then
continue;
if (aRow=0) and (FixedRows>0) then
- SelStr := SelStr + QuoteText(Columns[k].Title.Caption)
+ begin
+ SelStr := SelStr + QuoteText(Columns[k].Title.Caption);
+ SelHTMLStr := SelHTMLStr + '&LtPos;td>' + Columns[k].Title.Caption + '&LtPos;/td>';
+ end
else
+ begin
SelStr := SelStr + QuoteText(Cells[aCol,aRow]);
+ SelHTMLStr := SelHTMLStr + '&LtPos;td>' + Cells[aCol,aRow] + '&LtPos;/td>';
+ end;
end else
+ begin
SelStr := SelStr + QuoteText(Cells[aCol,aRow]);
+ SelHTMLStr := SelHTMLStr + '&LtPos;td>' + Cells[aCol,aRow] + '&LtPos;/td>';
+ end;
if aCol<>R.Right then
SelStr := SelStr + #9;
end;
SelStr := SelStr + sLineBreak;
+ SelHTMLStr := SelHTMLStr + '&LtPos;/tr>';
end;
+ rflags := [rfReplaceAll, rfIgnoreCase];
+ SelHTMLStr := StringReplace(SelHTMLStr, '"', '"', rflags) + '&LtPos;/table>';
- Clipboard.AsText := SelStr;
+ Clipboard.SetAsHtml(SelHTMLStr, SelStr);
end;
P.S.: To fix paste cells from Excel/Calc to StringGrid I think that need to parse HTML clipboard content in procedure TCustomStringGrid.SelectionSetText(TheText: String) or remove function QuoteText from procedure TCustomStringGrid.CopyCellRectToClipboard and correct operations with quote symbol in procedure LoadFromCSVStream.