Excel li的更紧凑代码
我正试图编写一个autolisp程序,在图形中插入一行20个块(一个接一个),然后从Excel文件中的一行插入每个块的属性。我正在使用Jeffrey Sanders的“GetCells”函数。Excel文件为“RO-16.xlsx”
以下是我到目前为止所做的:
(defun加法器(/cnt yval)
(setq cnt 1)
(setq yval 0)
(setvar“osmode”0)
(加载“getcells.lsp”)
(虽然(
(
(setq*doc*(vla get activedocument(vlax get acad object)))
(命令“insert”“C:\\07509\\BD”(strcat“0”,(itoa yval))“1”“1”“04”(getCellsFunction“C:\\07509\\RO-16.xlsx”“ALLOCATED”“K2”))
(setq cnt(1+cnt))
(setq yval(+8 yval))
)
)
)
因此,第一个属性“PLC”是从Excel文件中的单元格K2中读取的。共有7个属性。有没有更紧凑的方法(使用列表或数组)来实现这一点。我希望避免出现一条长线(这是对Autocad提示块属性的响应),它看起来像:(getCellsFunction“C:\\07509\\RO-16.xlsx”“ALLOCATED”“L2”)、(getCellsFunction“C:\\07509\\RO-16.xlsx”“ALLOCATED”“M2”)等等。 您可能需要搜索更多的getxecel。lsp,不确定getcells中有什么。lsp但一个选项是不使用“K2”,而是使用行、列方法,这样您可以有第二个循环,K2=11,2或X Y,然后将X增加到8 K8。您可能希望对代码进行更多的黑客攻击,以便在代码的早期锁定电子表格名称和工作簿(getcellsfunction k2)。我在getxecel中注意到。lsp我刚才看到了一个名为getrows(getrows K2 7)的函数,从K2开始,得到接下来的7个单元格。
看了一下getcells。lsp,你可以很容易地重写一个x,y样式,加上一个额外的defun,使“k2”11,2过26对于列来说有点问题。
(defun getCellsFunction( cellName / fileName sheetName myXL myBook mySheet myRange cellValue) but you need to ask these two questions next two lines.
;;;--- Get the excel file
(setq fileName(getfiled "Select Excel File" "" "*" 16)) ; remove from below
;;;--- Get the sheet name
(setq sheetName(getstring T "\nName of sheet: ")) ; remove from below
(setq myXL(vlax-get-or-create-object "Excel.Application"))
我不明白-我应该从“getcells.lsp”中删除红色代码吗? BIGAL告诉你的是在getcells中。lsp函数可以返回一个
单元格列表(不仅仅是单个单元格。
(defun getCells (fileName sheetName cellName / xl workbook sheet range cellValue)
(setq xl(vlax-get-or-create-object "Excel.Application"))
(vla-put-visible xl :vlax-false)
(vlax-put-property xl 'DisplayAlerts :vlax-false)
(setq workbook (vl-catch-all-apply 'vla-open (list (vlax-get-property xl "WorkBooks") fileName)))
(setq sheet (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property workbook "Sheets") "Item" sheetName)))
(vlax-invoke-method sheet "Activate")
(setq range (vlax-get-property (vlax-get-property sheet 'Cells) "Range" cellName))
(setq cellValue(vlax-variant-value (vlax-get-property range 'Value2)))
(vl-catch-all-apply 'vlax-invoke-method (list workbook "Close"))
(vl-catch-all-apply 'vlax-invoke-method (list xl "Quit"))
(if (not (vlax-object-released-p range))(progn(vlax-release-object range)(setq range nil)))
(if (not (vlax-object-released-p sheet))(progn(vlax-release-object sheet)(setq sheet nil)))
(if (not (vlax-object-released-p workbook))(progn(vlax-release-object workbook)(setq workbook nil)))
(if (not (vlax-object-released-p xl))(progn(vlax-release-object xl)(setq xl nil)))
(if(= 'safearray (type cellValue))
(progn
(setq tempCellValue(vlax-safearray->list cellValue))
(setq cellValue(list))
(if(= (length tempCellValue) 1)
(progn
(foreach a tempCellValue
(if(= (type a) 'LIST)
(progn
(foreach b a
(if(= (type b) 'LIST)
(setq cellValue(append cellValue (list (vlax-variant-value (car b)))))
(setq cellValue(append cellValue (list (vlax-variant-value b))))
)
)
)
(setq cellValue(append cellValue (list (vlax-variant-value a))))
)
)
)
(progn
(foreach a tempCellValue
(setq tmpList(list))
(foreach b a
(setq tmp(vlax-variant-value b))
(setq tmpList(append tmpList (list tmp)))
)
(setq cellValue(append cellValue tmpList))
)
)
)
)
)
cellValue
)
(defun c:test ()
(vl-load-com)
(setq fname "c:\\users\\ymg\\documents\\testgetcell.xlsx"
shname "sheet1"
range"B10:G10"
)
(setq lst (getcells fname shname range))
)
您将在列表中获得从B10到G10的所有值 好的,谢谢你澄清 我想说的是,您可以将文件名和表声明为局部变量,因此当获取单元格时,您的代码仅为(getcell cellreq)即(setq cellreq“k2”)
我还有一些问题,如果你从一个电子表格中读取多个块,那么代码应该根据你给出的块名自动搜索excel,看看创建一系列defuns,这样你就可以执行多个,
(setq row (getint "\nStart row number"))
(setq x (getint "\nhow many rows"))
(setq col (getstring "row name"))
(repeat x
(getcell (strcat col (rtos row 2 0)))
(setq row (+ row 1))
)
好的,这应该会大大缩短我的代码。谢谢 再来一次
(if (not AH:getval3) (load "Getvals"))
(ah:getval3 "Enter Column " 5 4 "Enter Start row " 5 4 "How many down" 5 4)
; val1 val2 val3 these are returned as strings
(setq col val1)
(setq row val2)
(setq down (atoi val3))
(repeat down
(setq cell (strcat col row))
(setq row (rtos (+ (atoi row) 1) 2 0))
(princ cell)
;(getcell here)
)
GETVALS。lsp
回到这里。
我想知道你是怎么得到这个消息框的。有一些VB代码吗?
无论如何,我用值“C”、“2”和“5”运行这个,得到了以下输出:
C2C3C4C5C6“C6”
我怎样才能让它显示单元格的内容? 下载Getvals链接它是一个自动DCL创建器,您需要使用getcell函数,但它仍然可以为您获取多行。
页:
[1]