Hi,
Here's a way, the read-csv routine returns an association list like: (("A1" . value_1) ("A2" . value_2) ...)
Using example:
(setq cells (read-csv (getfiled "Choose a file" "" "csv" 0)))
to get the C2 cell value:
(cdr (assoc "C2" cells))
;; Num2Alpha
;; Converts Number into Alpha string
;; Arguments:
;; Num = Number to convert
;; Syntax example: (Num2Alpha 731) = "ABC"
(defun num2alpha (num / r)
(if (< num 27)
(chr (+ 64 num))
(if (= 0 (setq r (rem num 26)))
(strcat (num2alpha (1- (/ num 26))) "Z")
(strcat (num2alpha (/ num 26)) (chr (+ 64 r)))
)
)
)
;; STR2LST
;; Converts a string with separator into a list of strings
;;
;; Arguments
;; str = the string
;; sep = the separator pattern
;; Syntax example: (str2lst "A,B,C" ",") = ("A" "B" "C")
(defun str2lst (str sep / pos)
(if (setq pos (vl-string-search sep str))
(cons (substr str 1 pos)
(str2lst (substr str (+ (strlen sep) pos 1)) sep)
)
(list str)
)
)
;; READ-CSV
;; Reads a csv file and returns an association list (("A1" . value_1) ("A2" . value_2) ...)
;;
;; Argument
;; path = the file complete path
;; Syntax example: (read-csv "C:\\Temp\\test.csv")
(defun read-csv (path / file cell row col lst)
(and
(setq file (open path "r"))
(setq row 1)
(setq col 0)
(while (setq line (read-line file))
(setq lst (cons
(mapcar
(function
(lambda (x)
(cons
(strcat (num2alpha (setq col (1+ col))) (itoa row))
x
)
)
)
(str2lst line ",")
)
lst
)
row (1+ row)
col 0
)
)
(close file)
)
(apply 'append (reverse lst))
)