Sujet : Re: Remove new line char
De : mssr953 (at) *nospam* gmail.com (Michael Soyka)
Groupes : comp.lang.tclDate : 20. Mar 2025, 16:12:08
Autres entêtes
Organisation : self
Message-ID : <vrhb88$3eoll$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 03/20/2025 5:34 AM, alexandru wrote:
Here is the code.
Can anywone test on Windows with Excel?
The test procedure is:
Open an Excel table with data an copy one single cell.
In the Tcl/Tk console execute the code below.
See how the copied text is output to the window.
In my case there is an additional line under the copied text, meaning a
new line was still created.
package require twapi
proc ::ClipboardGetText {} {
::twapi::open_clipboard
set text [::twapi::read_clipboard 1]
::twapi::close_clipboard
return $text
}
proc ::ClipboardSetText {text} {
# Write clipboard content
::twapi::open_clipboard
::twapi::empty_clipboard
::twapi::write_clipboard_text $text
::twapi::close_clipboard
}
## Remove line breaks at the end of string (mostly to avoid issues when
copying from Excel)
proc ::test {} {
set text [::ClipboardGetText]
puts [format %x [scan [string index $text end] %c]]
set text [regsub {[\r\n]+$} $text ""]
set text [regsub {[[:space:]]+$} $text ""]
set text [regsub {[\s]+$} $text ""]
set text [regsub {\x0$} $text ""]
# Do not use "trim" since we want to preserve trailing white spaces
but remove new line char
puts $text
}
::test
--
I'm running Windows 10 with tcl 9 in the USA. Following your suggestion above and using your code, I did this: set text [::ClipboardGetText] foreach c [split $text {}] {puts [scan c $c]}and I get the desired text plus 3 trailing characters: \r\n\x00.So, would this regular expression: {\r\n\x00} do what you want?
-mike