如何给表格指定一个整定的精度位数?
前天,提了一个问题是,如何给表格的其中一个单元格指定精度位数。在热心朋友的帮助下,已经解决了。就是用(vla-SetCellFormat tabela 4 0 "%lu2%pr1")
但是如何给表格指定一个整定的精度位数?我多次尝试都失败了。
(vla-SetFormat tabela (+ acDataRow acHeaderRow acTitleRow) "%lu2%pr2");给整个表格整体指定精度位数为2
或者是
(vla-SetFormat tabela acTitleRow "%lu2%pr2")
(vla-SetFormat tabela (+ acDataRow acHeaderRow) "%lu2%pr2")
都失败了。
这个是table对象的一些方法和属性。我用的CAD2024
https://help.autodesk.com/view/ACD/2024/CHS/?guid=GUID-B5AFE3DE-47CF-481E-9729-F5E15341BAF5
这是我的测试文件。不成功
(defun AddTable (Title datalst RowH / TableList-C2R datalst colms maxrow wlstrow colm i rown coln)
(defun TableList-C2R (lst n / l lst)
(setq l nil)
(repeat n
(setq l (cons (mapcar
'car
lst
) l
)
lst (mapcar
'cdr
lst
)
)
)
(reverse l)
)
(setq datalst (mapcar
'(lambda (y)
(mapcar
'vl-princ-to-string
y
)
)
datalst
)
colms (length datalst)
maxrow (apply
'max
(mapcar
'length
datalst
)
)
wlst (mapcar
'(lambda (x)
(apply
'max
(mapcar
'(lambda (y)
(if y
(strlen y)
0
)
)
x
)
)
)
(TableList-C2R datalst maxrow)
)
tabela (vla-addTable (vla-get-ModelSpace
(vla-get-ActiveDocument
(vlax-get-acad-object)
)
) (vlax-3d-point (getpoint "\n在屏幕上指定插入点创建表格."));
(1+ colms) maxrow RowH (* RowH 50)
)
)
(vla-put-regenerateTableSuppressed tabela :vlax-true)
(setq i -1)
(foreach w wlst
(vla-setcolumnwidth tabela (setq i (1+ i))
1700
)
)
(vla-SetTextHeight tabela acTitleRow (* 0.75 RowH))
(vla-SetTextHeight tabela (+ acDataRow acHeaderRow) (* 0.5 RowH))
(vla-setalignment tabela acDataRow acMiddleCenter)
(vla-setText tabela 0 0 Title)
(setq row 0)
(foreach data datalst
(setq row (1+ row)
colm -1
)
(foreach str data
(vla-setText tabela row (setq colm (1+ colm)) str)
)
)
(vla-SetFormat tabela (+ acDataRow acHeaderRow acTitleRow) "%lu2%pr2");给整个表格整体指定精度位数为2
(vla-put-regenerateTableSuppressed tabela :vlax-false)
(princ)
)
(defun c:fff()
(vl-load-com)
(AddTable "TITLE" '( ("Pe(kW)" "6.0012")
("Kd" "1.0012")
("cosφ" "0.8012")
("Pc(kW)" "=B3*B2")
("Ic(A)" "=B5/1.732/0.38/B4")
) 600
)
) SetFormat是处理所有同类单元格,要求所有单元格的数据类型都一致,否则,就要一个个单元格设置。你的数据里面明显大把的字符串,怎么可能设置精度? 我又改了下,这次输入的有数字的,不全是字符串。我现在想指定数字单元格的格式也指定不了。没有效果不知道什么原因。
(vl-load-com)
(defun AddTable (Title datalst RowH / TableList-C2R datalst colms maxrow wlst tabela row colm i rown colnins hgt app spc col)
(setq
colms (length datalst)
maxrow (apply
'max
(mapcar
'length
datalst
)
)
tabela (vla-addTable (vla-get-ModelSpace
(vla-get-ActiveDocument
(vlax-get-acad-object)
)
) (vlax-3d-point (getpoint "\n在屏幕上指定插入点创建表格."))
(1+ colms) maxrow RowH (* RowH 50)
)
)
(vla-put-regenerateTableSuppressed tabela :vlax-true)
(setq i -1)
(while (< i 1)
(vla-setcolumnwidth tabela (setq i (1+ i))
1700
)
)
(vla-SetTextStyle tabela (+ acDataRow acHeaderRow acTitleRow) "_TEL_DIM")
(vla-SetTextHeight tabela acTitleRow (* 0.75 RowH))
(vla-SetTextHeight tabela (+ acDataRow acHeaderRow) (* 0.5 RowH))
(vla-setalignment tabela acDataRow acMiddleCenter)
(vla-setText tabela 0 0 Title)
(setq row 0)
(foreach data datalst
(setq row (1+ row)
colm -1
)
(foreach str data
(vla-setText tabela row (setq colm (1+ colm)) str)
)
)
(setq ins (getvar 'viewctr)
hgt (getvar 'textsize)
app (vlax-get-acad-object)
spc (vlax-get-property (vla-get-activedocument app) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
col (vla-getinterfaceobject app (strcat "autocad.accmcolor." (substr (getvar 'acadver) 1 2))))
(vla-setrgbcol 255 0 0)
(vla-SetCellContentColor tabela 5 1 col)
(vla-SetCellFormat tabela 2 1 "%lu2%pr2")
(vla-SetCellFormat tabela 3 1 "%lu2%pr2")
(vla-SetCellFormat tabela 4 1 "%lu2%pr2")
(vla-DeleteRows tabela 0 1)
(vla-put-regenerateTableSuppressed tabela :vlax-false)
(princ)
)
(defun c:fff()
(vl-load-com)
(AddTable "TITLE" '( ("Pe(kW)" 6.00)
("Kd" 1.00)
("cosφ" 0.80)
("Pc(kW)" "=B3*B2")
("Ic(A)" "=B5/1.732/0.38/B4")
) 600
)
) kozmosovia 发表于 2023-8-8 10:36
SetFormat是处理所有同类单元格,要求所有单元格的数据类型都一致,否则,就要一个个单元格设置。你的数据 ...
大神,你看看我四楼的代码,我又改了下,这次输入的有数字了。我现在不弄整个单元格的精度了。因为里面有字符串。我现在想指定数字单元格的精度。也是指定不了。目前只能指定公式单元格的精度。不知道是什么原因。难道是用了settext这个函数的原因吗?自动将数字转换成字符串了?
请问,如何将数字直接写入单元格啊。或者再退一步,怎么将某个单元格里的数值字符串转成数字格式? cfc 发表于 2023-8-8 14:59
大神,你看看我四楼的代码,我又改了下,这次输入的有数字了。我现在不弄整个单元格的精度了。因为里面有 ...
(setq b (cond ((or (= (type b) 'REAL) (= (type b) 'INT)) (rtos b 2 pre))
((and (= (type b) 'STR) (distof b)) (rtos (distof b) pre))
(t b)
)
)
页:
[1]