LISP for automatic table summation at the botton cell

LISP for automatic table summation at the botton cell

aitorm
Advocate Advocate
312 Views
3 Replies
Message 1 of 4

LISP for automatic table summation at the botton cell

aitorm
Advocate
Advocate

Hey everyone,

I'm trying to create a LISP routine for AutoCAD that will automatically add a sum row to the bottom of selected tables, but I'm a bit stuck. Here's what I'm aiming for:

  1. Add a Row: The LISP should add a new row at the very bottom of whatever table I select. It may be locked as normally it will come from dataextraction commands.

  2. Sum the Column: Specifically, I need it to automatically insert a SUM formula into the bottom-right cell of that new row. This formula should add up all the numbers directly above it in that column, starting from the third row of the table (skipping the header).

  3. Dynamic Row Counting: This is the tricky part! I don't want to hardcode the number of rows in the formula (like SUM(B3:B9)). It needs to figure out how many rows are in the table and use that to build the SUM formula correctly. So, if there are 15 rows in a table, it should sum from row 3 up to row 14. If the next table has 20 rows it should sum from row 3 up to 19. I guess the way to do so is by asking for the total number of rows and remove 1 unit.

I'm familiar with basic LISP, but I'm struggling to get the dynamic row count and formula insertion working correctly.

Does anyone have a LISP routine that does something similar or could point me in the right direction? Any help would be greatly appreciated!

Thanks in advance

0 Likes
313 Views
3 Replies
Replies (3)
Message 2 of 4

pendean
Community Legend
Community Legend
since SUM is already built into TABLE command as a solution, its in the RIBBON, how would this lisp speed things up if I may ask?
0 Likes
Message 3 of 4

komondormrex
Mentor
Mentor

hey there,

check the following simple code

(defun c:sum_row (/ table_object row_number column_number column_index)
  (setq table_object (vlax-ename->vla-object (car (entsel "\nPick target table: ")))
	row_number (vla-get-rows table_object)
	column_number (vla-get-columns table_object)
	column_index 0
  )
  (vla-insertrows table_object row_number (vla-getrowheight table_object (1- row_number)) 1)
  (repeat column_number 
	  (vla-setcelltextstyle table_object row_number column_index (vla-getcelltextstyle table_object (1- row_number) column_index))
	  (vla-setcelltextheight table_object row_number column_index (vla-getcelltextheight table_object (1- row_number) column_index))
	  (vla-settext table_object row_number column_index (strcat "=Sum(" (chr (+ 65 column_index)) "4:" (chr (+ 65 column_index)) (itoa row_number) ")"))
    	  (setq column_index (1+ column_index))
  )
  (princ)
)
0 Likes
Message 4 of 4

Sea-Haven
Mentor
Mentor

This may be useful converts a number to Alpha, so can do a row column type answer. 1=A

 

 

 

;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
  (if (< Num# 27)
    (chr (+ 64 Num#))
    (if (= 0 (setq Val# (rem Num# 26)))
      (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
      (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
    )
  )
)

 

0 Likes