Set Clipboard in CSV format

Hi,

Is there a way to specify the format of the data set on the Windows Clipboard?
I want to set CSV to the clipboard, but without setting the format, a paste interprets it as plain text.

The "Drag-and-drop trigger functions" allow some access to the clipboard and the formats it contains, but I want to do this without Drag-Drop.
SystemAdmin - Fri Oct 07 06:25:53 EDT 2011

Re: Set Clipboard in CSV format
Mathias Mamsch - Fri Oct 07 16:37:10 EDT 2011

I am not sure, if you really need that. I might be wrong, but I think clipboard formats are for automatic type conversions done by the system itself. The caller only needs to specify a custom clipboard format if he wants to put multiple data types.

What are you trying to do? If you want to paste tables to Excel then we already have a thread where this is described. Additionally you might have luck pasting RTF (which supports tables). Do you have a special application that will only understand CSV? Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Set Clipboard in CSV format
SystemAdmin - Mon Oct 10 06:13:53 EDT 2011

Mathias Mamsch - Fri Oct 07 16:37:10 EDT 2011
I am not sure, if you really need that. I might be wrong, but I think clipboard formats are for automatic type conversions done by the system itself. The caller only needs to specify a custom clipboard format if he wants to put multiple data types.

What are you trying to do? If you want to paste tables to Excel then we already have a thread where this is described. Additionally you might have luck pasting RTF (which supports tables). Do you have a special application that will only understand CSV? Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

My understanding is that data on the clipboard can be 'tagged' with a format. This allow multiple formats to be placed on the clipboard by an application, and the application pasted into to can query the formats to pick the most appropriate format and decide how to interpret the data.

Eg.
Word copys to the clipboard as: plain text, richtext, html, enhanced metafile etc. (displayed in 'paste special')
Pasting to word - Word chooses richtext.
Pasting to paint - Paint chooses enhanced metafile.
Pasting to notepad - Notepad chooses plain text.

So the recieving application interprets the data based on the format.
It CSV data is put on the clipboard, but with the plain text format, the recieving App will not know it is CSV and will not parse it as CSV.

I used CSV as an example, applications can specify their own formats which they understand. DOORS does this to copy items/objects and Word ignores the data in that format since it does not understand it.

So my app wants to query the clipboard for a specific format it can decode, and 'paste' it appropriatly. Hence I want to set the Format.

Re: Set Clipboard in CSV format
OurGuest - Mon Oct 10 10:28:21 EDT 2011

SystemAdmin - Mon Oct 10 06:13:53 EDT 2011
My understanding is that data on the clipboard can be 'tagged' with a format. This allow multiple formats to be placed on the clipboard by an application, and the application pasted into to can query the formats to pick the most appropriate format and decide how to interpret the data.

Eg.
Word copys to the clipboard as: plain text, richtext, html, enhanced metafile etc. (displayed in 'paste special')
Pasting to word - Word chooses richtext.
Pasting to paint - Paint chooses enhanced metafile.
Pasting to notepad - Notepad chooses plain text.

So the recieving application interprets the data based on the format.
It CSV data is put on the clipboard, but with the plain text format, the recieving App will not know it is CSV and will not parse it as CSV.

I used CSV as an example, applications can specify their own formats which they understand. DOORS does this to copy items/objects and Word ignores the data in that format since it does not understand it.

So my app wants to query the clipboard for a specific format it can decode, and 'paste' it appropriatly. Hence I want to set the Format.

This is not a DOORS specific request -- you should look in appropriate areas before posting a request. Perhpas here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx

Re: Set Clipboard in CSV format
SystemAdmin - Mon Oct 10 12:11:57 EDT 2011

OurGuest - Mon Oct 10 10:28:21 EDT 2011
This is not a DOORS specific request -- you should look in appropriate areas before posting a request. Perhpas here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx

Well, it is a DXL request.
DXL can interact with the clipboard formats inside Drag/Drop Callbacks - I just wondered if anybody knew to to do it from outside the Callbacks. The rest is just background info.

Re: Set Clipboard in CSV format
llandale - Mon Oct 10 14:59:58 EDT 2011

SystemAdmin - Mon Oct 10 06:13:53 EDT 2011
My understanding is that data on the clipboard can be 'tagged' with a format. This allow multiple formats to be placed on the clipboard by an application, and the application pasted into to can query the formats to pick the most appropriate format and decide how to interpret the data.

Eg.
Word copys to the clipboard as: plain text, richtext, html, enhanced metafile etc. (displayed in 'paste special')
Pasting to word - Word chooses richtext.
Pasting to paint - Paint chooses enhanced metafile.
Pasting to notepad - Notepad chooses plain text.

So the recieving application interprets the data based on the format.
It CSV data is put on the clipboard, but with the plain text format, the recieving App will not know it is CSV and will not parse it as CSV.

I used CSV as an example, applications can specify their own formats which they understand. DOORS does this to copy items/objects and Word ignores the data in that format since it does not understand it.

So my app wants to query the clipboard for a specific format it can decode, and 'paste' it appropriatly. Hence I want to set the Format.

Yes the target application can choose to interpret bytes on the clipboard in a variety of formats when pasting and can look at that data and determine its format; but that does not mean that the clipboard has any notion of that format. That is I don't think you can copy to the clipboard, then paste, then tell the clipboard its a different format, then do the same paste expecting different results.

Seems to me then with DXL that you would "paste" into your string variable, then write code to parse it expecting CSV format.

  • Louie

Re: Set Clipboard in CSV format
SystemAdmin - Tue Oct 11 10:25:24 EDT 2011

llandale - Mon Oct 10 14:59:58 EDT 2011
Yes the target application can choose to interpret bytes on the clipboard in a variety of formats when pasting and can look at that data and determine its format; but that does not mean that the clipboard has any notion of that format. That is I don't think you can copy to the clipboard, then paste, then tell the clipboard its a different format, then do the same paste expecting different results.

Seems to me then with DXL that you would "paste" into your string variable, then write code to parse it expecting CSV format.

  • Louie

I must have failed to explain properly.

I do not want to GET Data from the clipboard.
I do not want to apply a Format to Data already on the clipboard.

I want to SET the clipboard with Data and a Format - using DXL.

It can be done for PlaintText:

copyToClipboard("This is not {\\b bold}?")

 


It can be done for RichText:

 

 

setRichClip(richText("This is {\\b bold}"))



It can be done in any format in a drag/drop Trigger:



 

 

string FormatName = "SomeFormat"
int FormatID = registeredFormat(FormatName)
string Error = setDropString(FormatID, aTrigger, "The Data" , false)



I was hoping for a function (probably undocumented and used by the functions above) along the lines of:



 

 

string FormatName = "SomeFormat"
int FormatID = registeredFormat(FormatName)
string Error = setClipboardWithFormat(FormatID, "The Data")

 

Re: Set Clipboard in CSV format
Mathias Mamsch - Tue Oct 11 18:13:20 EDT 2011

SystemAdmin - Tue Oct 11 10:25:24 EDT 2011

I must have failed to explain properly.

I do not want to GET Data from the clipboard.
I do not want to apply a Format to Data already on the clipboard.

I want to SET the clipboard with Data and a Format - using DXL.

It can be done for PlaintText:

copyToClipboard("This is not {\\b bold}?")

 


It can be done for RichText:

 

 

setRichClip(richText("This is {\\b bold}"))



It can be done in any format in a drag/drop Trigger:



 

 

string FormatName = "SomeFormat"
int FormatID = registeredFormat(FormatName)
string Error = setDropString(FormatID, aTrigger, "The Data" , false)



I was hoping for a function (probably undocumented and used by the functions above) along the lines of:



 

 

string FormatName = "SomeFormat"
int FormatID = registeredFormat(FormatName)
string Error = setClipboardWithFormat(FormatID, "The Data")

 

Ok, short answer: there is no such function in DXL. It would be interesting to hear to what application you want to paste (if you have any special application in mind). Excel for example will automatically convert your clipboard text (without the need to explicitly set your format). Pasting to Word however will give you just the text. What you could do is open Excel invisibly using create object, paste there and try to copy from there in a different format.

Regards, Mathias
 

// paste this to excel ...
string s = "<HTML>
    <BODY>
    <TABLE BORDER><TR><TH ROWSPAN=2>Head1</TH><TD>Item 1
   </TD><TD>Item 2</TD><TD>Item 3</TD>
   <TD>Item 4</TD></TR><TR><TD>Item 5</TD>
   <TD>Item 6</TD><TD>Item 7</TD><TD>Item 8</TD>
   </TR><TR><TH>Head2</TH><TD>Item 9</TD>
     <TD>Item 10</TD><TD>Item 11</TD><TD>Item 12</TD></TR></TABLE>
    </BODY>
    </HTML>"
 
copyToClipboard s

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Re: Set Clipboard in CSV format
SystemAdmin - Wed Oct 12 08:50:15 EDT 2011

Mathias Mamsch - Tue Oct 11 18:13:20 EDT 2011

Ok, short answer: there is no such function in DXL. It would be interesting to hear to what application you want to paste (if you have any special application in mind). Excel for example will automatically convert your clipboard text (without the need to explicitly set your format). Pasting to Word however will give you just the text. What you could do is open Excel invisibly using create object, paste there and try to copy from there in a different format.

Regards, Mathias
 

// paste this to excel ...
string s = "<HTML>
    <BODY>
    <TABLE BORDER><TR><TH ROWSPAN=2>Head1</TH><TD>Item 1
   </TD><TD>Item 2</TD><TD>Item 3</TD>
   <TD>Item 4</TD></TR><TR><TD>Item 5</TD>
   <TD>Item 6</TD><TD>Item 7</TD><TD>Item 8</TD>
   </TR><TR><TH>Head2</TH><TD>Item 9</TD>
     <TD>Item 10</TD><TD>Item 11</TD><TD>Item 12</TD></TR></TABLE>
    </BODY>
    </HTML>"
 
copyToClipboard s

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Ok thanks, I shall give up on this.
The App is just an internally created legacy (rubbish) program someody wrote years ago that "processes data".
It should be binned in my opinion!

Not important now but...
In Excel 2003, a paste after running your code dosen't insert a the table to cells - it put in the plaintext in rows of one column.
Putting the same thing on the clipboard but with the format set to HTML does work. Seems that is the mechanism for copy pasting Word tables.
Annoyingly, it splits cells with newlines over separate rows and vertically merges other cells of the HTML table row - rather than putting the newlines in one cell (as in Alt-Enter).

Re: Set Clipboard in CSV format
Mathias Mamsch - Wed Oct 12 12:03:02 EDT 2011

SystemAdmin - Wed Oct 12 08:50:15 EDT 2011
Ok thanks, I shall give up on this.
The App is just an internally created legacy (rubbish) program someody wrote years ago that "processes data".
It should be binned in my opinion!

Not important now but...
In Excel 2003, a paste after running your code dosen't insert a the table to cells - it put in the plaintext in rows of one column.
Putting the same thing on the clipboard but with the format set to HTML does work. Seems that is the mechanism for copy pasting Word tables.
Annoyingly, it splits cells with newlines over separate rows and vertically merges other cells of the HTML table row - rather than putting the newlines in one cell (as in Alt-Enter).

Strange. For Excel 2003 German it worked fine with pasting the HTML ... no idea. Anyway we have successfully used pasting plain text to excel for fast exports to excel from DXL. We also addressed newlines and other escaping in this post - if you are interested see here:

https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14627116&#14627116

Maybe this helps, regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Set Clipboard in CSV format
SystemAdmin - Thu Oct 13 10:15:05 EDT 2011

Mathias Mamsch - Wed Oct 12 12:03:02 EDT 2011
Strange. For Excel 2003 German it worked fine with pasting the HTML ... no idea. Anyway we have successfully used pasting plain text to excel for fast exports to excel from DXL. We also addressed newlines and other escaping in this post - if you are interested see here:

https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14627116&#14627116

Maybe this helps, regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Thanks for the link - Didn't know about the convertToCodepage function before.

I have discovered something regarding Excel Paste..
Excel 2003 UK - http://Excel (11.8231.8202) SP3

I paste plaintext CSV into Excel - it all goes into 1 column.
I then use the "Data > Text to Columns..." command. - Delimited, Comma, Text qualifier: "
Now the data is as it should be.

Now...
Clear the sheet.
Paste to Excel.
This time it pastes to multi columns straight away.

So the settings in "Data > Text to Columns..." determine how the paste works.

For me, when opening Excel the these settings are on Tab not Comma which is why it didn't paste CSV as expected.
I guess they could be set differently for different versions/languages.
I've not found how to change these defaults yet.

Re: Set Clipboard in CSV format
llandale - Thu Oct 13 17:07:34 EDT 2011

SystemAdmin - Thu Oct 13 10:15:05 EDT 2011
Thanks for the link - Didn't know about the convertToCodepage function before.

I have discovered something regarding Excel Paste..
Excel 2003 UK - http://Excel (11.8231.8202) SP3

I paste plaintext CSV into Excel - it all goes into 1 column.
I then use the "Data > Text to Columns..." command. - Delimited, Comma, Text qualifier: "
Now the data is as it should be.

Now...
Clear the sheet.
Paste to Excel.
This time it pastes to multi columns straight away.

So the settings in "Data > Text to Columns..." determine how the paste works.

For me, when opening Excel the these settings are on Tab not Comma which is why it didn't paste CSV as expected.
I guess they could be set differently for different versions/languages.
I've not found how to change these defaults yet.

WOOT! Useful discovery. Someone here will surely now use DOORS to command that setting in Excel.

So now we can create our own "XSV" format, where "X" is a short sequence of non-printable characters, perhaps charOf(22) "" charOf(23) "" charOf(24) "". Then get each desired cell, use that separator, past into excel, then command excel to delimit it correcly based on that sequence.

  • Louie

Re: Set Clipboard in CSV format
SystemAdmin - Fri Oct 14 05:10:54 EDT 2011

llandale - Thu Oct 13 17:07:34 EDT 2011
WOOT! Useful discovery. Someone here will surely now use DOORS to command that setting in Excel.

So now we can create our own "XSV" format, where "X" is a short sequence of non-printable characters, perhaps charOf(22) "" charOf(23) "" charOf(24) "". Then get each desired cell, use that separator, past into excel, then command excel to delimit it correcly based on that sequence.

  • Louie

Continuing to stray off topic...

The PasteCsvIntoRange function will Paste a CSV after setting the TextToColumns options up for CSV.
 

string PasteStringIntoRange(OleAutoObj ObjRange, string StringToPaste)
{
    OleAutoArgs autoArgs = create
    OleAutoObj ObjWorksheet = null
 
    oleGet(ObjRange, "Worksheet", ObjWorksheet)
 
    // Lock the Clipboard
    ClipboardLock LockedClipboardRef = null
    while(null LockedClipboardRef)
    {
        LockedClipboardRef = create(2000)
        if (null LockedClipboardRef)
        {
            if (!confirm("Failed to lock the clipboard.  Retry?"))
            {
                break
            }
        }
    }
 
    string Error = ""
    if(!null LockedClipboardRef)
    {
        noError
 
        // Copy
        if(copyToClipboard(convertToCodepage(850, StringToPaste)))
        {
            // Paste
            clear autoArgs
            put(autoArgs, "Destination", ObjRange)
            oleMethod(ObjWorksheet, "Paste", autoArgs)
        } else {
            Error = "Failed to Copy"
        }
 
        // Unlock the Clipboard
        string LastError = lastError()
        Error = Error LastError
        delete LockedClipboardRef
    } else {
        Error = "Failed to Lock the Clipboard"
    }
 
    delete autoArgs
    ObjWorksheet = null
 
    return Error
}
 
OleAutoObj PasteCsvIntoRange(OleAutoObj ObjRange, string CsvValues)
{
    OleAutoArgs autoArgs = create
        OleAutoObj ObjCellRange = null
        OleAutoObj ObjPasteRange = null
 
        // Get first cell in range
        clear autoArgs
        put(autoArgs, "RowIndex", 1)
        oleGet(ObjRange, "Item", autoArgs, ObjCellRange)
 
        // Put some Data in
        olePut(ObjCellRange, "Value2", "SetCsvDecode")
 
        // Run TextToColumns to change the settings
        clear autoArgs
        put(autoArgs, "DataType", 1)          //xlDelimited
        put(autoArgs, "TextQualifier", 1)     //xlDoubleQuote
        put(autoArgs, "Comma", true)
        oleMethod(ObjCellRange, "TextToColumns", autoArgs)
 
        // Clear the cell
        oleMethod(ObjCellRange, "Clear")
 
        // Paste the CSV
        if(null(PasteStringIntoRange(ObjRange, CsvValues)))
        {
                // Set WrapText so multi line shows
                oleGet(ObjRange, "CurrentRegion", ObjPasteRange)
                olePut(ObjCellRange, "WrapText", true)
        }
 
        delete autoArgs
        ObjCellRange = null
 
        return ObjPasteRange
}