Bug 99035 - [Feature Request] "Convert text to table" lacks 'number of columns' option
Summary: [Feature Request] "Convert text to table" lacks 'number of columns' option
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium enhancement
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
: 146841 (view as bug list)
Depends on:
Blocks: Writer-Tables-Enhancements
  Show dependency treegraph
 
Reported: 2016-04-01 16:19 UTC by Paul
Modified: 2022-01-31 18:25 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments
text that I want to end up in a 2-column table (13.24 KB, application/vnd.oasis.opendocument.text)
2016-04-10 14:38 UTC, Paul
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul 2016-04-01 16:19:01 UTC
When converting a selection of text to a table in Writer, there should be an option to spec the desired number of columns. I currently have some text, all separated by paragraph returns, that I would like to make into a two-column table. That seems impossible without, possibly, changing some of the delimiters before the conversion.

I'm in LO 5.1.1.3 x64 .deb.
Comment 1 Buovjaga 2016-04-10 12:38:50 UTC
Can you attach a minimal example document with the starting point text and the desired end result table?

Set to NEEDINFO.
Change back to UNCONFIRMED after you have provided the document.
Comment 2 Paul 2016-04-10 14:38:59 UTC
Created attachment 124231 [details]
text that I want to end up in a 2-column table
Comment 3 Paul 2016-05-05 14:56:27 UTC
Puzzled here. This request was assigned to me, the mere reporter?
Comment 4 Buovjaga 2016-05-05 15:36:20 UTC
Looking at the history we can see you assigned it to yourself: https://bugs.documentfoundation.org/show_activity.cgi?id=99035
Comment 5 Heiko Tietze 2016-09-25 06:39:01 UTC
Yes, sometimes data are not well organized (=> NEW). 

Example in the attachment is 
Parameter 1
data 1
Parameter 2
data 2

with the goal to get
Parameter 1 | data 1
Parameter 2 | data 2


But I would rather not add this complexity to the text-to-table conversion of _Writer_. If you regularly have to organize data like this you can do the trick with Calc. And eventually an extension/macro could also become handy.

WONTFIX in my opinion.
Comment 6 Paul 2016-09-25 14:40:52 UTC
(In reply to Buovjaga from comment #4)
> Looking at the history we can see you assigned it to yourself:
> https://bugs.documentfoundation.org/show_activity.cgi?id=99035

My apologies. I'm new at this and must have hit the wrong button.
Comment 7 Buovjaga 2016-09-26 08:09:32 UTC
One developer on IRC would rather see this made as an extension or simply a macro.
Comment 8 Cor Nouws 2016-09-26 08:11:33 UTC
Hi Paul,

(In reply to Paul from comment #0)
> I currently have some text,
> all separated by paragraph returns, that I would like to make into a
> two-column table. 

I do have a very simple trick for that: I add one Tab (for every extra column) on one line, before selecting and hitting Ctrl+F12.

So personally I would not dare to ask extra UI stuff for that. Would that work for you too?
Comment 9 Cor Nouws 2016-09-26 08:13:13 UTC
@paul: please consider to ask questions too at one of our great community support options
 http://www.libreoffice.org/get-help/community-support/
Comment 10 Tomaz Vajngerl 2016-09-26 08:35:22 UTC
He wants to make from text (oriented in one column) a two column table - there is no extra column. Without manually changing the data I don't think it is easily possible to do it in Calc, or am I mistaken.
Comment 11 Heiko Tietze 2016-09-26 09:22:03 UTC
(In reply to Tomaz Vajngerl from comment #10)
> He wants to make from text (oriented in one column) a two column table -
> there is no extra column. Without manually changing the data I don't think
> it is easily possible to do it in Calc, or am I mistaken.

Depends on the actual values. You can split text into multiple columns and create a pivot table with the running numbers as row criteria. 

But back to Writer: Another example could be 
P1 #9 D1 #9 P2 #9 D2
P3 #9 D3 #9 P4 #9 D4

(#9 as tab, could be any other delimiter)

again with the goal to get 
P1|D1
P2|D2

The request is to have an option to limit the number of columns. E.g. 
[ ] auto split
    split into [2]+/-

That means to ignore line wraps (#10#13), or rather virtually replace by a delimiter, if the number of columns is not reached. And to add a line break otherwise even when there are more delimiters if the specified number of columns is reached. 

Perhaps it's not so difficult to implement, and from the UI POV I changed my mind and guess user will understand the solution (OTOH undo is always possible).
Comment 12 Tomaz Vajngerl 2016-09-26 11:28:01 UTC
I'm sure it is not difficult - should be a nice non-trivial EasyHack.
Comment 13 Paul 2016-09-26 11:50:15 UTC
In retrospect, I can see how the request would seem somewhat esoteric. It's been a while now, and I don't recall the nature of the file I was up against, but I do know it was a massive problem, and that I felt it was one I would encounter fairly often (I format a lot of scanned documents - or at least, was, before taking a new job).

I guess that by macro I could replace every !Nth linefeed with a tab. That seems a remote solution for the typical user, but it may be the best solution unless I can recall the original problem in greater detail and it proves to be common enough to warrant a GUI solution (as I thought it was at the time).
Comment 14 Heiko Tietze 2016-09-26 12:13:57 UTC
EASYHACK, please add the code pointer (=> NEEDINFO). 

The UI needs only small changes with the addition of a (by default marked) checkbox "auto split" and a numerical stepper below that is enabled when this checkbox is switched off (all below the "separate text at" options). 

Auto split means every break counts, while the numerical input defines how many columns the result will have. See also comment 11.
Comment 15 Xisco Faulí 2016-09-27 10:36:06 UTC Comment hidden (obsolete)
Comment 16 Paul 2018-02-16 19:27:41 UTC
Revisiting this, after quite a while (sorry, wasn't feeling too well back then). I tried the trick of adding tabs to the first line, and indeed, this creates a table with a corresponding number of columns, which is pretty neat. But the data is not filled into those extra columns, they remain empty. The data continues to be populated, each line into a new row. IOW, if the original text is:

word tab tab
word2
word3
word4
word5
word6

It converts to:

word col2 col3
word2
word3
word4
word5
word6

Whereas I would like it to be:

word  word2  word3
word4 word5 word6

I format a lot of older public-domain documents, and I run into this frequently. My personal take is that this functionality is pretty basic. I'm surprised there hasn't been more call for it.

Since I seem to be the only one desiring this function, I think asking over at the Ask is a good idea. I suppose a macro converting linefeeds to delimiters within a selection, while leaving every Nth feed untouched, would be the solution, applied before table conversion.
Comment 17 Heiko Tietze 2018-02-17 08:21:25 UTC
(In reply to Paul from comment #16)
> I suppose a macro converting linefeeds to
> delimiters within a selection, while leaving every Nth feed untouched, would
> be the solution, applied before table conversion.

True. If you start with an extension please share the result so that we can close this ticket eventually. 

But I could imagine that more users benefit from the change and would keep it as easyhack as long no solution is available.
Comment 18 Paul 2018-02-19 19:39:39 UTC
Ok, I asked and received a really nice macro reply. I slightly altered it by adding an Input box to spec the number of columns on the fly, and to exit if the input box was left blank. The Ask thread is at http://bit.ly/2ETWnbD.

I will print the finished macro below. But there is one strange problem. Though the macro works fine when invoked from the Basic editor, it errors out when I use a custom Menu entry for it. The error is: 

    A Scripting Framework error occurred while running the Basic script 
    Standard.Module1.Writer_SelectionToColumns.

    Message: wrong number of parameters!

Also errors out via hotkey. Here's the macro. I would like to see something like this incorporated into Core somehow. :)

Sub Writer_SelectionToColumns( ByRef iNumColumns As Integer, Optional sSeparator )
  REM Courtesy of librebel, 2/19/18; http://bit.ly/2ETWnbD. My input box added
  REM Convert selected Text into a specified number of Table Columns.
  REM <iNumColumns> : The number of columns for the Table to be created.
  REM <sSeparator>  : String pattern separating the Text portions to be inserted into the Table Cells.
  REM NB. This method does not preserve the existing text format of the selection.
  REM TODO: Format text; AutoFormat Table.
    Dim oDocument As Object : oDocument = ThisComponent
    If oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
    	Dim iNumColumns As Integer 
        iNumColumns =  InputBox("Enter desired number of columns (blank = 0)")

        If iNumColumns <= 0 Then Exit Sub
        If IsMissing( sSeparator ) Or sSeparator  = "" Then sSeparator = chr(10)

        Dim oSelection As Object : oSelection = oDocument.CurrentController.Selection.getByIndex( 0 )
        Dim aParts() As String   : aParts     = Split( oSelection.getString(), sSeparator )
        Dim iNumRows As Integer  : iNumRows   = ( 1 + uBound( aParts ) ) \ iNumColumns
        If ( 1 + uBound( aParts ) ) Mod iNumColumns > 0 Then iNumRows = iNumRows + 1    REM Ceiling()...
        If iNumRows = 0 Then Exit Sub

        Dim oTable As Object     : oTable     = oDocument.createInstance( "com.sun.star.text.TextTable" )
        oTable.initialize( iNumRows, iNumColumns )

        oDocument.Text.insertTextContent( oSelection, oTable, True )    REM Replace selection with Table.

        Dim i As Integer
        For i = 0 To uBound( aParts )
            oTable.getCellByPosition( i Mod iNumColumns, i \ iNumColumns ).setString( aParts( i ) )
        Next i

    End If

End Sub
Comment 19 Paul 2018-02-20 00:25:08 UTC
Ok, I fixed it by taking the "iNumColumns As Integer," out of the Sub definition:

    Sub Writer_SelectionToColumns( ByRef Optional sSeparator )
Comment 20 Dieter 2022-01-31 18:25:52 UTC
*** Bug 146841 has been marked as a duplicate of this bug. ***