So, my previous post titled "Modify Table Column Widths" was deemed successful, so I decided to start a new thread.
Several times, we generate a drawing, and the text widths in the columns vary greatly. Some have text that is ten characters long, and others have thirty-plus characters.
Is there a way (does anyone think) to be able to select the table (or perform the command to all tables in the current layout) and have each of the columns expand to the appropriate width, that depends on the longest string of text in that column, for each column. The problem I see, is that some tables have two columns, some have six, and of course other with any number in between. The same is also true for the amount of rows.
How would one organize the code?
Is this a tall order?
Solved! Go to Solution.
Solved! by phanaem. See the answer in context.
2 minor changes in red
(setq wid 0
(repeat (1- (vla-get-rows tb))
Alot of things still need to be considered but here is something to start
(vl-load-com) (defun C:TEST ( / ss tb e col wid row val h lst) (if (setq ss (ssget '((0 . "ACAD_TABLE")))) (repeat (sslength ss) (setq tb (vlax-ename->vla-object (setq e (ssname ss 0)))) (setq col 0) (repeat (vla-get-columns tb) (setq wid 0 row 0) (repeat (vla-get-rows tb) (if (setq val (vlax-invoke tb 'GetCellValue row col)) (setq wid (max wid (+ (setq h (vlax-invoke tb 'GetCellTextHeight row col)) (vlax-invoke tb 'GetMargin row col acCellMarginLeft) (vlax-invoke tb 'GetMargin row col acCellMarginRight) (- (caadr (setq lst (textbox (list (cons 1 (if (numberp val) (rtos val) val)) (cons 40 h) (cons 7 (vlax-invoke tb 'GetCellTextStyle row col)) ) ) ) ) (caar lst) ) ) ) ) ) (setq row (1+ row)) ) (vla-SetColumnWidth tb col wid) (setq col (1+ col)) ) (ssdel e ss) ) ) (princ) )
My first column has a zero (0) assigned to the "col" variable, and it seems "1 1/2" is always assigned to the "wid" variable. In my example (screenshot posted), it should be way less than "1 1/2". Looking at the code, i don't know where 1.5 is coming from. I've check the fields and there are no weird characters that I can see, spaces, or anything of that nature.
All the rest of the columns are perfect.
That was the fix.
So in my case, it was getting the initial value from what we have laid out to be the "header" and doing it's thing from there?
I'm glad I could help you...
But I have to warn you, there are situations when my cod is not working properly.
I can think of two at this moment:
- multiple text-lines in the same cell
- Long text in the Title cell, longer than the sum of all columns
When the text is wrapped, it works. I haven't seen a case in our templates, where the text is forced to be multiline. But thanks for the heads up. If something goes awry, I'll do the work-around.
This is a huge time-saver
Access a broad range of knowledge to help get the most out of your products and services.
Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.
Upgrading to a 2015 product? Make sure to check these out 1st!