;; Copyright 2006-2015, Matthew Welland. ;; ;; This program is made available under the GNU GPL version 2.0 or ;; greater. See the accompanying file COPYING for details. ;; ;; This program is distributed WITHOUT ANY WARRANTY; without even the ;; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ;; PURPOSE. ;; (use refdb) (include "refdb-module.scm") (import refdb) (define help (conc "Usage: refdb action params ... Note: refdbdir is a path to the directory containg sheet-names.cfg import filename.gnumeric refdbdir : Import a gnumeric file into a txt db directory export refdbdir filename.gnumeric : Export a refdb to a gnumeric file edit refdbdir : Edit a refdbdir using gnumeric. ls refdbdir : List the keys for specified level lookup refdbdir sheetname row col : Look up a value in the text db getrownames refdbdir sheetname : Get a list of row titles getcolnames refdbdir sheetname : Get a list of column titles getrow refdbdir sheetname rowname : Get the column-value pairs for given row getcol refdbdir sheetname colname : Get the row-value pairs for given column dump2sqlite3 refdbdir fname.db : dump refdb to sqlite3 dump2csv refdbdir sheetname [csvfile] : dump refdb sheet to csv set refdbdir sheetname row col val : set value gencsv : Dump all sheets as csv files Settings refdbdir/settings.cfg [setup] record [row|col] : records are switched to row or col in gnumeric To export to other formats; first export to gnumeric then use ssconvert. e.g. refdb export mydata mydata.gnumeric ssconvert -T Gnumeric_html:html40 mydata.gnumeric mydata.html Part of the Megatest tool suite. Learn more at http://www.kiatoa.com/fossils/megatest Version: " refdb-version)) (let ((debugcontrolf (conc (get-environment-variable "HOME") "/.refdbrc"))) (if (file-exists? debugcontrolf) (load debugcontrolf))) ;;====================================================================== ;; This routine dispaches or executes most of the commands for refdb ;;====================================================================== ;; (define (process-action action-str . param) (let ((num-params (length param)) (action (string->symbol action-str))) (cond ((eq? num-params 1) (case action ((edit) (edit-refdb (car param))) ((ls) (map print (list-sheets (car param)))) ((export) #f) ((gencsv) ;; (list-of-lists->csv (dat->list-of-lists (read-dat "testrefdb/Sheet2.dat" record: 'row))) (let* ((refdbpath (car param)) (sheets (list-sheets refdbpath)) (record (conf-get-record refdbpath))) (map (lambda (sheet) (let* ((fname (conc refdbpath "/" sheet ".dat")) (dat (read-dat fname record: record)) ;; FINISHME!!! Needs record: support (outn (conc sheet ".csv"))) (with-output-to-file outn (lambda () (print (list-of-lists->csv (dat->list-of-lists dat))))))) sheets))))) ((eq? num-params 2) (let ((param1 (car param)) (param2 (cadr param))) (case action ((getrownames) (print (string-intersperse (get-rowcol-names param1 param2 car) " "))) ((getcolnames) (print (string-intersperse (get-rowcol-names param1 param2 cadr) " "))) ((import) (import-gnumeric-file param1 param2)) ;; fname targname ((export) (refdb-export param1 param2)) ((dump2sqlite3) (alist3->sqlite3db (refdb->alist param1) param2)) ((dump2csv) (let ((dat (read-dat (conc param1 "/" param2 ".dat")))) (with-output-to-file (conc param1 "-" param2 ".csv") (lambda () (print (list-of-lists->csv (dat->list-of-lists dat))))))) (else (print "Unrecognised command " action)(print help))))) ((eq? num-params 3) (let ((p1 (car param)) (p2 (cadr param)) (p3 (caddr param))) (case action ((getrow getcol) (for-each (lambda (dat) (print (car dat) " " (cdr dat))) (get-row-or-column p1 p2 p3 get-type: (if (eq? action 'getrow) 'row 'col)))) ((dump2csv) (let ((dat (read-dat (conc p1 "/" p2 ".dat")))) (with-output-to-file p3 (lambda () (print (list-of-lists->csv (dat->list-of-lists dat))))))) ))) ((eq? num-params 4) (case action ((lookup) ;; path section row col (let ((res (apply lookup param))) (if res (print res) (begin (print "") (exit 1))))))) ((eq? num-params 5) (case action ((set) ;; dbpath section row col val (apply set-sheet-var param)))) ))) (define (main) (let* ((args (argv)) (prog (car args)) (rema (cdr args))) (cond ((null? rema)(print help)) ((eq? (length rema) 1) (case (string->symbol (car rema)) ((mtedit) ;; Edit a Megatest area (megatest->refdb)))) ((>= (length rema) 2) (apply process-action (car rema)(cdr rema))) (else (print help))))) (main) #| (define x (refdb:read-gnumeric-xml "testdata-stripped.xml")) ;; Write out sxml (with-output-to-file "testdata.sxml" (lambda()(pp x))) ;; (serialize-sxml a output: "new.xml") (with-output-to-file "testdata-stripped.xml" (lambda ()(print (sxml-serializer#serialize-sxml y)))) ;; Read in sxml file (with-input-from-file "testdata.sxml" (lambda ()(set! y (read)))) (find-section x 'http://www.gnumeric.org/v10.dtd:Workbook) (define sheets (find-section x 'http://www.gnumeric.org/v10.dtd:Sheets)) (define sheet1 (car sheets)) (define cells-sheet1 (find-section sheet1 'http://www.gnumeric.org/v10.dtd:Cells)) (map (lambda (c)(find-section c 'Row)) cells-sheet1) (for-each (lambda (cell) (let* ((len (length cell)) (row (car (find-section cell 'Row))) (col (car (find-section cell 'Col))) (val (let ((res (cdShr (filter (lambda (x)(not (list? x))) cell)))) (if (null? res) "" (car res))))) (print "Row=" row " col=" col " val=" val))) cells-sheet1) (map (lambda (c)(filter (lambda (x)(not (list? x))) c)) cells-sheet1) |#