Bhull1985 发表于 2022-7-5 22:39:48

需要使用mapcar的帮助

嘿,大家好,我不得不在这个cad-to-excel程序中添加大量数据,我在过去很长一段时间里一直在工作。。。
 
下面是现在硬编码到例程中的数据列表示例


(setq x1tr-t1 (list "Valve, Gate, NPT, 800#, A352-LCB/LCC or A350-LF2 Body, SS Wedge, HF-SS Seat, API Trim 10, 12 or 16, HW Op., Reduced Port, Bolted Bonnet- OS&Y, Graphite Packing, Solid Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "150" "1100" "quoting 1/2\" Newco 18T- LF24/3NCRP"))

(SETQ X1TR-T1-2 (LIST "Valve, Gate, NPT, 800#, A-182 F316/A-351Gr.CF8M Body, SS Stem/Wedge/Seat/Bonnet, API Trim 10, 12 or 16, HW Op., Red. Port, Sealed SS Bolted Extended Bonnet, Graphite/TFE Packing, Flex Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "250" "550" "DSI 4121F12-18"))

(SETQ X1TR-T1-3 (LIST "Valve, Floating Ball, NPT, 2000#, A-216Gr.WCB Body, SS Stem/Ball, PTFE Seats, -20 to 400 F, Lever Op., Red. Port, Hydrocarbon Trim, Anti-blowout Stem, API-607, ASME B16.34, 500ppm VOC, Bubble Tight Seal" "250" "550" "DSI 4121F12-18"))

(SETQ X1TR-T1-4 (LIST "Valve, Trunnion Ball, 300# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -20 to 400 F, Gear Op., Red. Port, Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "250" "550" "DSI 4121F12-18"))

(SETQ X1TR-T1-5 (LIST "Valve, Globe, SW, 800#, A352-LCB/LCC or A350-LF2 Body, SS Disc, HF- SS Seat, MS Stem, API Trim 10, 12 or 16, 800#, -50 to 800 F, HW Op., Bolted Bonnet-OS&Y, Graphite Packing, API-602 & 598, ASME B16.34 & B16.10, 500ppm VOC" "250" "550" "Newco 18TLF24/3NCRP"))

(SETQ X1TR-S2L (LIST "Valve, Piston Check, NPT, A-182 F316/A-351Gr.CF8M Body, SS Piston/Disc, SS Spring, API Trim 10, 12 or 16, 800#, -20 to 800 F, Bolted Bonnet, Std. Port, Renew. Seats, API-602 & 598, ASME B16.34" "150" "1100" "OIC 8GT6LUSEG"))

(SETQ X1TR-2SL-2 (LIST "Valve, Floating Ball, 300# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -20 to 400 F, Lever Op. \(Gear Op>8\" 300#\), Full Port, Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "150" "1100" "OIC 8GT6LUSEG"))

(SETQ X1TR-S2 (LIST "Valve, Gate, NPT, 800#, A-182 F316/A-351Gr.CF8M Body, SS Stem/Wedge/Seat/Bonnet, API Trim 10, 12 or 16, HW Op., Red. Port, Sealed SS Bolted Extended Bonnet, Graphite/TFE Packing, Flex Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "150" "1100" "OIC 8GT6LUSEG"))

(SETQ X1TR-S2-2 (LIST "Valve, Piston Check, NPT, A-182 F316/A-351Gr.CF8M Body, SS Piston/Disc, SS Spring, API Trim 10, 12 or 16, 800#, -20 to 800 F, Bolted Bonnet, Std. Port, Renew. Seats, API-602 & 598, ASME B16.34" "150" "1100" "OIC 8GT6LUSEG"))

(SETQ X4S-C1 (LIST "Valve, Gate, NPT, 800#, A-182 F316/A-351Gr.CF8M Body, SS Stem/Wedge/Seat/Bonnet, API Trim 10, 12 or 16, HW Op., Red. Port, Sealed SS Bolted Extended Bonnet, Graphite/TFE Packing, Flex Wedge, API-600 & 598, ASME B16.34 & B16.10, 500ppm VOC" "175" "1100" "OIC 8GL6LUSWG-EB"))

(SETQ X4S-C1-2 (LIST "Valve, Butterfly, Insert Type, 300# RF, A-182 F316/A-351Gr.CF8M Body Extended Stem, SS, 17-4 PH or Inconel Shaft/Disc/Pins/Retainers/Rings, Cryogenic Trim to -320 F or Specified, PTFE or Kel-F Packing, ASME B16.34, API-609, Offset to be Specified by Engineer" "175" "1100" "OIC 8GL6LUSWG-EB"))

(SETQ X4S-C1-3 (LIST "Valve, Swing Check, SW, 800#, A-182 F316/A-351Gr.CF8M Body, SS Disc/Pins, API Trim 10, 12 or 16, Cryogenic Trim to -320F unless otherwise indicated, SS Bolted Bonnet, Std. Port, Renew. Seats, API-600 & 598, ASME B16.34 & B16.10" "175" "1100" "OIC 8GL6LUSWG-EB"))

(setq X4S-H1 (list "Valve, Globe, SW, 800#, A-105 Body, CS/13Cr Disc, HF-CS Seat, MS Stem, API Trim 5 or 8, -20 to 800 F, HW Op., Bolted Bonnet-OS&Y, Graphite Packing, Renew, Seats, API-602 & 598, ASME B16.34, 500ppm VOC" "175" "1100" "DSI 4211A8-18"))

(setq X4S-H2 (list "Valve, Globe, SW, 800#, A-105 Body, CS/13Cr Disc, HF-CS Seat, MS Stem, API Trim 5 or 8, -20 to 800 F, HW Op., Bolted Bonnet-OS&Y, Graphite Packing, Renew, Seats, API-602 & 598, ASME B16.34, 500ppm VOC" "175" "1100" "DSI 4211A8-18"))

(SETQ X4S-H2-3 (LIST "Valve, Trunnion Ball, 300# RF, A-216Gr.WCB/A-105 Body, SS Stem/Ball, Hydrocarbon Compatible Seat/O-Rings/Packing, -20 to 400 F, Gear Op., Full Port, Anti-blowout Stem, API-607, ASME B16.34 & B16.10, 500ppm VOC, Bubble Tight Seal" "175" "1100" "DSI 4211A8-18"))

 
我目前正在使用(car)(cadr)(caddr)和(caddr)获取数据。这些数据的结构都是相同的。
这是使用数据的例程的一部分,我想知道如何合并mapcar语句,这样就不必为每个数据段(在50-100之间)做四个表达式,这是一个重复循环。这是一个很大的处理过程,我相信这会减慢程序的速度,我知道mapcar是我用来缩短它的工具,但我只是没有练习,需要一些帮助来应用它。
 
不过,下面是子功能,其中有两个较长的方法(我认为会)来完成任务,因为您可以想象,将相同的行添加到我的所有数据条目中将创建大约500行新代码,我认为仅使用几行和mapcar即可完成相同的任务:

(defun doTYPE ( vallist / c cell namedata)


(if
(setq cell (strcat "K" (itoa (vl-bb-ref 'excelrow))))
(acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist))
);if
(setq c 0)
(setq len (length vallist))
(setq cell (string_ cell))
(repeat len
(setq data (nth c vallist))
(setq typedata (cadr data))
(putcell cell typedata)



(if (= typedata "U2TR-H2")
(progn
(setq desccell (strcat "I" (itoa (vl-bb-ref 'excelrow))))
(setq descdata (car u2tr-h2))
(PUTCELL desccell descdata)

(setq tempcell (strcat "F" (itoa (vl-bb-ref 'excelrow))))
(setq tempdata (cadr u2tr-h2))
(PUTCELL tempcell tempdata)

(setq presscell (strcat "E" (itoa (vl-bb-ref 'excelrow))))
(setq pressdata (caddr u2tr-h2))
(PUTCELL presscell pressdata)

(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr u2tr-h2))
(putcell mrfcell mrfdata)

);PROGN
);IF U2TR-H2



(setq X1TR-H2 (list "Valve, Gate, NPT, 800#, A-105 Body, CS/13Cr Wedge, HF-CS Seat, MS Stem, API Trim 5 or 8, -20 to 800 F, HW Op., Red. Port, Bltd. Bonnet-OS&Y, Graphite Packing, Solid Wedge, Renew. Seats, API-602 & 598, ASME B16.34,

500ppm VOC" "595" "1100" "DSI 4121A8-18"))

(if (= typedata "X1TR-H2")
(progn
(setq desccell (strcat "I" (itoa (vl-bb-ref 'excelrow))))
(setq descdata (car X1TR-H2))
(PUTCELL desccell descdata)

(setq tempcell (strcat "F" (itoa (vl-bb-ref 'excelrow))))
(setq tempdata (cadr X1TR-H2))
(PUTCELL tempcell tempdata)

(setq presscell (strcat "E" (itoa (vl-bb-ref 'excelrow))))
(setq pressdata (caddr X1TR-H2))
(PUTCELL presscell pressdata)

(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr X1TR-H2))
(PUTCELL mrfcell mrfdata)

);PROGN
);IF X1TR-H2



(setq cell (string_ cell))
(acet-ui-progress-safe c)
(setq c (1+ c))
);repeat
(acet-ui-progress-done)
(if (= c len)(dotag vallist));;goto do-tag sub
);defun doname

 
现在它的行为很奇怪。。。看一看:

Command: (setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
"A6"

Command: (setq mfrdata (cadddr X1TR-H2))
"DSI 4121A8-18"

Command: (PUTCELL mrfcell mrfdata)
; error: bad argument type: stringp nil

;;not sure why routine did not put "DSI 4121A8-18" because...

Command: !MFRDATA
"DSI 4121A8-18"

Command: !MFRCELL
"A6"

Even though these are showing as strings to me the routine gave a stringp nil error. So, I tried it manually, and it worked. The correct string was placed into the correct excel field, using:

Command: (PUTCELL "A6" "DSI 4121A8-18")

This one worked! Why, why why :/

 
这两个任务,我确实需要一些帮助。
 
任何人

7o7 发表于 2022-7-5 23:09:08

也许你把“单元格”值放到一个有数据的单元格中,excel不允许你覆盖它。

MSasu 发表于 2022-7-5 23:23:19

请注意,您颠倒了变量名称中的一些字母(“fr”与“rf”):
(setq mfrcell (strcat "A" (itoa (vl-bb-ref 'excelrow))))
(setq mfrdata (cadddr X1TR-H2))
(PUTCELL mrfcell mrfdata)

Bhull1985 发表于 2022-7-5 23:32:20

谢谢Msasu!
 
 
7o7-Excel允许您覆盖现有单元格内容,但无论如何谢谢您!

Bhull1985 发表于 2022-7-5 23:40:56

好的,成交!没有使用mapcar,但有一个替代方案。。。一个或四个辅助函数。
来,看看。我告诉你,当我计算出最后一行,程序按预期运行时,我感到非常高兴。
这是一种方法,使用与我在上面发布的相同的数据列表

(if vallist
(acet-ui-progress-init "Writing valve TYPES to Excel." (length vallist))
);if

(setq c 0)
(setq len (length vallist))

(repeat len
(setq cella (CELL-ADDR "K"))
(setq data (nth c vallist))
(setq cell (itoa (vl-bb-ref 'excelrow)))
(setq typedata (cadr data))
(putcell cella typedata)
(setq typekey (read typedata))

   (putcell (cell-addr "I") (desc-data typekey))
   (PUTCELL (cell-addr "F") (temp-data typekey))
   (PUTCELL (cell-addr "E") (press-data typekey))
   (PUTCELL (cell-addr "A") (mfr-data typekey))

(setq cell (string_ cell))
(setq cellb (atoi cell))
(vl-bb-set 'excelrow cellb)
(acet-ui-progress-safe c)
(setq c (1+ c))
);repeat

(acet-ui-progress-done)
(setvar "cmdecho" 0)
(princ "\n\t\t*************************************************************")(princ)
(princ "\n\t\t***\tAutoExcel Report \t*******************************")(princ)
(princ (strcat "\n\t\t***\t" (itoa (length @dupeslist)) " unique items duplicated in excel file \t\t***"))(princ)
(princ "\n\t\t***\t")
(princ lst)
(princ "\n\t\t***\tThese items have had their cells colored Grey\t***")(princ)
(princ "\n\t\t***\tFinished!\t\t\t\t\t*****************************")(princ)
(princ "\n\t\t*************************************************************")(princ)
(if vallist (setq svflg 1))
(setq vallist nil)
(vl-bb-set 'dupeslist nil)
(setq @dupeslist nil)
(setq @excel_row "6")
(setq *excelApp% nil)
(princ "\n")
;;end of excel input, routine will alert to user that it is complete
(princ)
(setq *error* nil)
(gc)

);defun dotype

 
我只是想说声谢谢,然后就过去了。喔!
页: [1]
查看完整版本: 需要使用mapcar的帮助