get width of each column based on the maximum width of text in a list of lists

get width of each column based on the maximum width of text in a list of lists

Anonymous
Not applicable
931 Views
1 Reply
Message 1 of 2

get width of each column based on the maximum width of text in a list of lists

Anonymous
Not applicable

I am trying to write a subroutine to take the output of excel sheet data with different numbers of columns and rows and then turn them into AutoCAD tables. I have to subroutine to create a table from a variable size list.

 

My problem is how to get the width of each column based on the maximum width of text in each column.

Here is an example list -

(setq List_Table
  (list
    (list "Heading 1" "Heading 2" "Heading 3")
    (list "Row 2 Text Value" "0.11" "This is a number")
    (list "Row 3 Text Description" "This is more" "0.23")
    (list "Row 4" "Line 4" "This is a longer cell")
  )
)

0 Likes
Accepted solutions (1)
932 Views
1 Reply
Reply (1)
Message 2 of 2

phanaem
Collaborator
Collaborator
Accepted solution

Hi

 

You can find here a solution.

I extracted and adapted the part you need:

 

(setq List_Table
  (list
    (list "Heading 1" "Heading 2" "Heading 3")
    (list "Row 2 Text Value" "0.11" "This is a number")
    (list "Row 3 Text Description" "This is more" "0.23")
    (list "Row 4" "Line 4" "This is a longer cell")
  )
  ht 2.0
)

(mapcar
 '(lambda (col)
    (apply 'max
      (mapcar
        '(lambda (x)
           ((lambda (txb) (+ (abs (- (caadr txb) (caar txb))) (* 2.0 ht)))
            (textbox (list (cons 1 (vl-princ-to-string x)) (cons 7 (getvar 'textstyle)) (cons 40 ht)))
           )
         )
         col
       )
     )
   )
   (apply 'mapcar (cons 'list List_Table))
)

Make sure your list contain the same number of items in each row.

The column width depend on text height (variable ht).

The result is a little larger than the actual width of the text. To get the exact width, replace (* 2 ht) with 0.

 

EDIT: I think you can use insert_table function from the link.

0 Likes