-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_csv.lua
More file actions
83 lines (57 loc) · 1.33 KB
/
import_csv.lua
File metadata and controls
83 lines (57 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
importer.csv_map_fields=function(self, field_map, fields)
local toks, tok, count, field
local fieldlist={}
count=0
toks=strutil.TOKENIZER(fields, ",")
tok=toks:next()
while tok ~= nil
do
field=field_map[tok]
if field == nil then field="notes" end
count=count + 1
fieldlist[count]=field
tok=toks:next()
end
return fieldlist
end
importer.import_csv_line=function(self, box, line, fields)
local toks, tok, field
local key=""
local value=""
local notes=""
local count=1
toks=strutil.TOKENIZER(line, ",", "Q")
tok=toks:next()
while tok ~= nil
do
field=self:map_field(fields, "", count)
count=count+1
if field == "key" then key=tok
elseif field == "value" then value=tok
else notes=notes.." "..tok
end
tok=toks:next()
end
if strutil.strlen(key) > 0
then
box:add(key, value, notes)
self.items_imported=self.items_imported + 1
return(true)
end
return(false)
end
importer.import_csv=function(self, box, doc)
local lines, line
lines=strutil.TOKENIZER(doc, "\n", "Q")
if self.fields_type=="map" then fields=self:csv_map_fields(self.fields, lines:next())
else fields=self.fields
end
--if no fieldlist supplied then assume first line of file is a fieldlist
if fields==nil then fields=self:read_fieldlist(lines:next()) end
line=lines:next()
while line ~= nil
do
self:import_csv_line(box, line, fields)
line=lines:next()
end
end