Sujet : Re: Remove new line char
De : alexandru.dadalau (at) *nospam* meshparts.de (alexandru)
Groupes : comp.lang.tclDate : 20. Mar 2025, 10:34:40
Autres entêtes
Organisation : novaBBS
Message-ID : <dbede06f17ced01322709a92b6258417@www.novabbs.com>
References : 1 2 3 4 5
User-Agent : Rocksolid Light
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
--