View Issue Details

IDProjectCategoryView StatusLast Update
0033111LazarusLCLpublic2018-03-15 19:20
ReporterSnus Assigned ToJesus Reyes  
Status resolvedResolutionduplicate 
Product Version1.8 
Summary0033111: StringGrid incorrect copy-paste parsing
DescriptionStringGrid treats the text on the clipboard as a CSV stream while paste. But not while copying. It causes data changing and data location changing on copy-paste procedure.
Steps To ReproducePut StringGrid on form. Set goEditing in grid options to true. Run. Write text in cell[1,1]:
in cell[2,1]:
"123" "234"
Select those two cells. Copy ctrl+c. Single click(important!) select cell [2,2]. Paste ctrl+v.
Two cells will merge into one with removed two external double quotes. Screenshot attached. The same happens when copy-paste from excel. Also double quotes are removed when paste from any source into one cell.
TagsCSV, Quotes, StringGrid
Fixed in Revision
Attached Files


related to 0030623 resolvedJesus Reyes TStringGrid copy/paste to/from MS Excel and OO Calc bug 



2018-02-01 10:42


quotes.rar (66,900 bytes)

Bart Broersma

2018-02-01 11:59

developer   ~0106162

The issue occurs when the editor is not visible.
The text on the clipboard is always handled as a CSV stream, with Tab as the separator (hardcoded).

While this may be a good idea for pasting tabulated text, it makes no sense if the text does not contain tabs.
More-over, CSV with Tab as separator does not require to quote fields that have a space or a comma in it, which IMO makes the unquoting a bit odd.


2018-02-01 13:24

developer   ~0106163

Last edited: 2018-02-01 13:42

View 3 revisions

But cells with a tab or a linebreak must be quoted. It should be quoted also if the cell contains a double-quote character - whch does not happen at the moment. I think cell content like 'Song title: "Blowing In The Wind"' is a valid cell text which can be input easily.

This works for me (in procedure TCustomStringGrid.CopyCellRectToClipboard of grids.pas):

  function QuoteText(s: string): string;
    DoCellProcess(aCol, aRow, cpCopy, s);
    if (pos(# 9, s)>0) or // space after # intentionally added due to Mantis software
       (pos(# 10, s)>0) or
       (pos(# 13, s)>0) or
       (pos('"', s)>0) // <--- added
      result := AnsiQuotedStr(s, '"')
      result := s;

Snus, please test if this modification solves the issue for you, too.


2018-02-01 15:10

reporter   ~0106166

Last edited: 2018-02-01 15:11

View 2 revisions

Thank you for replies.
>Snus, please test if this modification solves the issue for you, too.
Well it solves the issue when you copy from a StringGrid. Of course not from another sources like quoted text from TEdit or excel. Double quotes with an empty string ("") results in empty cell. But single doublequote(") is coping fine. It makes a little mess in users heads.


2018-02-01 17:01

developer   ~0106167

When pasting the quotes are already dropped by the csv reader before the grid can do anything with the pasted text. This is because many programs always quote strings in the csv format. Excel is different, it quotes only when needed.

Since the csv reader is in a separate unit (lcvsutils) I think it is necessary to extend the routines LoadCSVfromFile and LoadCSVFromStream with an additional, optional boolean parameter "AllTextQuoted" (or so) which defaults to true (to avoid breaking code if somebody uses this function independently of the grid) and which is set to false for pasting from the clipboard.

Bart Broersma

2018-02-01 18:30

developer   ~0106169

In TCustomStringGrid.SelectionSetText the paramter TheText holds the text, with quotes (I'm talking about the example project: "test").
In the example case, TheText does NOT contain a Tab character, nor a LineEnding, hence it should not be considered to be / treated as a CSV stream.

In this particular case TheText should be integrally (unstripped) pasted into one cell.

Bart Broersma

2018-02-04 13:25


grids.paste.diff (501 bytes)   
Index: lcl/grids.pas
--- lcl/grids.pas	(revision 57251)
+++ lcl/grids.pas	(working copy)
@@ -11229,6 +11229,9 @@
+  //if range of TheText is a single cell, prevent removal of surrounding quotes. Issue #0033111
+  if (Pos(#9, TheText)=0) and (Pos(LineEnding,TheText)=0) then
+    TheText := LazUtf8.UTF8QuotedStr(TheText, '"');
   Stream := TStringStream.Create(TheText);
     StartCol := Selection.left;
grids.paste.diff (501 bytes)   

Bart Broersma

2018-02-04 13:26

developer   ~0106222

Possible patch attached.
@Jesus: please review.
I did not commit myself, because I'm not sure if current behaviour is "as designed".

Jesus Reyes

2018-03-15 04:32

developer   ~0107119

Last edited: 2018-03-15 04:36

View 2 revisions

Bart, I think we have to find another more general solution, indeed I think this report and the one in 0030623 are so related that this one should be treated as duplicate of 0030623, I think we should reopen 0030623 and resolve this one. Going to do just that.

Jesus Reyes

2018-03-15 04:38

developer   ~0107120

marked as duplicated as I think is closer to it than any other resolution.

Issue History

Date Modified Username Field Change
2018-02-01 10:42 Snus New Issue
2018-02-01 10:42 Snus File Added: quotes.rar
2018-02-01 11:22 Snus Tag Attached: CSV
2018-02-01 11:22 Snus Tag Attached: Quotes
2018-02-01 11:22 Snus Tag Attached: StringGrid
2018-02-01 11:59 Bart Broersma Note Added: 0106162
2018-02-01 11:59 Bart Broersma LazTarget => -
2018-02-01 11:59 Bart Broersma Status new => confirmed
2018-02-01 13:08 wp Relationship added related to 0030623
2018-02-01 13:24 wp Note Added: 0106163
2018-02-01 13:25 wp Note Edited: 0106163 View Revisions
2018-02-01 13:42 wp Note Edited: 0106163 View Revisions
2018-02-01 15:10 Snus Note Added: 0106166
2018-02-01 15:11 Snus Note Edited: 0106166 View Revisions
2018-02-01 17:01 wp Note Added: 0106167
2018-02-01 18:30 Bart Broersma Note Added: 0106169
2018-02-04 13:25 Bart Broersma File Added: grids.paste.diff
2018-02-04 13:25 Bart Broersma Assigned To => Jesus Reyes
2018-02-04 13:25 Bart Broersma Status confirmed => assigned
2018-02-04 13:26 Bart Broersma Note Added: 0106222
2018-03-15 04:32 Jesus Reyes Note Added: 0107119
2018-03-15 04:36 Jesus Reyes Note Edited: 0107119 View Revisions
2018-03-15 04:38 Jesus Reyes Note Added: 0107120
2018-03-15 04:38 Jesus Reyes Status assigned => resolved
2018-03-15 04:38 Jesus Reyes Resolution open => duplicate