none
Create a table out of a nested list RRS feed

  • Question

  • I am trying to create a word table to represent a nested list.

    '''**Pseudocode/Python/C#'''

    the_list = [FT1,[ST1,[TT1, TT2, TT3], ST2,[TT1]], FT2,[ST1]]

    iter_list(the_list)

    def iter_list(the_list):
    //Iterates through nested lists
       row_count = 0   
       for each_item in the_list:
          column_count = 0
             if isinstance(the_list, list):
                column_count += 1
                iter_list(each_item)             
             else:
                //Append item to table
                table.Rows(row_count+1).Cells(column_count+1).Range.Text = row[column_count] // Won't work
       row_count += 1
    
                                 
                        


    For example, if I have a list:


    • First Tier 1

           - Second Tier 1

                + Third Tier 1

                + Third Tier 2

                + Third Tier 3

           - Second Tier 2

                + Third Tier 1

                + Third Tier 2

    • First Tier 2

          - Second Tier 1

            

    I would like to create a word table, like the one below:

    [First Tier]    [Second Tier]    [Third Tier]

    [First Tier 1]   [Second Tier 1]    [Third Tier 1]

                                                     -----------------

                                                      [Third Tier 2]

                                                     -------------------

                                                      [Third Tier 3]

                          -----------------------------------------

                         [Second Tier 2]      [Third Tier 1]

                                                      -------------------

                                                      [Third Tier 2]

    --------------------------------------------------

    [First Tier 2]  [Second Tier 1]
    ------------------------------------------------------------


    ** Just to clarify the table drawing, there are two rows in the first column, 3 rows in the second column, and 6 rows (including 1 blank) in the third/last column


    Thursday, October 23, 2014 8:52 PM

Answers

  • Hi Sanket,

    Do you read it as a string a collection of list?

    If it is a string, I suggest that you could use Stack class to handle this string.

    1. Iterate the char of that string
    2. Push the char to the Stack, if the char is “]”, pop char until the char is “[“
    3. Define two variables (I and J), which to use to identity the column and row index. (e.g. if the char is “[“, then J=J+1, if char is “]”, then I=I+1)

    If it is a collection of list, then you could base on the item’s index to create the table directly.

    About create Word tables, please refer to these links below:

    # Programmatically Create Word Tables

    http://msdn.microsoft.com/en-us/library/w1702h4a.aspx

    # Word Automation using C#: Create a Word Table Programatically

    http://www.codeproject.com/Tips/818331/Word-Automation-using-Csharp-Create-a-Word-Table

    If you still have the issue, please provide detail code. (I don’t understand the code that you provided, it isn’t the VB or C# code.)

    Best Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 27, 2014 2:18 AM
    Moderator

All replies

  • Hi Sanket,

    According to your description, I would like to know the details:

    1. How do you get that string ([FT1,[ST1,[TT1, TT2, TT3], ST2,[TT1]], FT2,[ST1]])?
    2. Do you want to achieve that by using the VBA or add-in?

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 24, 2014 9:36 AM
    Moderator
  • 1. It is stored in my system as a tree, so I am reading it in as a list of lists

    2. I'm a little new to microsoft developing, but I believe add -in.


    Friday, October 24, 2014 2:59 PM
  • Hi Sanket,

    Do you read it as a string a collection of list?

    If it is a string, I suggest that you could use Stack class to handle this string.

    1. Iterate the char of that string
    2. Push the char to the Stack, if the char is “]”, pop char until the char is “[“
    3. Define two variables (I and J), which to use to identity the column and row index. (e.g. if the char is “[“, then J=J+1, if char is “]”, then I=I+1)

    If it is a collection of list, then you could base on the item’s index to create the table directly.

    About create Word tables, please refer to these links below:

    # Programmatically Create Word Tables

    http://msdn.microsoft.com/en-us/library/w1702h4a.aspx

    # Word Automation using C#: Create a Word Table Programatically

    http://www.codeproject.com/Tips/818331/Word-Automation-using-Csharp-Create-a-Word-Table

    If you still have the issue, please provide detail code. (I don’t understand the code that you provided, it isn’t the VB or C# code.)

    Best Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 27, 2014 2:18 AM
    Moderator