forked from jjensen/lua-xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlsx.lua
More file actions
292 lines (246 loc) · 8.38 KB
/
Copy pathxlsx.lua
File metadata and controls
292 lines (246 loc) · 8.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
--[[
* lua-xlsx
*
* Based on the original implementation by Josh Jensen.
* Modified to be compatible with Lua 5.3 and lib-zip by Simon Spivey.
*
--]]
---The module lua-xlsx allows read access to .xlsx files.
---@class lua-xlsx
local lib = {}
---return file info; for embedded purposes
---@private
function lib:INFO(_)
local info = {}
for k in pairs(self) do table.insert(info, k) end
return {
version = {
major = 0,
minor = 1,
revision = 1,
},
library = {
modulename = "lua-xlsx"
},
dependencies = {
"lua-zip",
"luaexpat"
},
functions = info
}
end
local ZIP = require('brimworks.zip')
local lxp = require('lxp')
local colRowPattern = "([a-zA-Z]*)(%d*)"
local A_BYTE = string.byte('A')
--[[
Builds the same "@" / "#" node shape used throughout this file:
node['@'] -> table of attributes (always present, may be empty)
node['#'] -> either a string (leaf text content), or a table
mapping child tag name -> array of child nodes
--]]
local function _xlsx_parsexml(data)
local root = {}
local stack = { root }
local callbacks = {}
function callbacks.StartElement(_, name, attrs)
local cleanAttrs = {}
for k, v in pairs(attrs) do
if type(k) == 'string' then
cleanAttrs[k] = v
end
end
local node = { ['@'] = cleanAttrs }
local parent = stack[#stack]
if type(parent['#']) ~= 'table' then
parent['#'] = {}
end
local siblings = parent['#'][name]
if not siblings then
siblings = {}
parent['#'][name] = siblings
end
siblings[#siblings + 1] = node
stack[#stack + 1] = node
end
function callbacks.EndElement(_, name)
local node = table.remove(stack)
if node['#'] == nil then
node['#'] = ''
end
end
function callbacks.CharacterData(_, text)
if not text or text == '' then return end
local current = stack[#stack]
if type(current['#']) == 'table' then
return
end
current['#'] = (current['#'] or '') .. text
end
local parser = lxp.new(callbacks)
local ok, err, line, col = parser:parse(data)
if ok then
ok, err, line, col = parser:parse()
end
parser:close()
if not ok then
error(("XML parse error: %s (line %s, col %s)"):format(tostring(err), tostring(line), tostring(col)))
end
return root['#']
end
local function _xlsx_readdocument(tbl, documentName)
local xlsx = ZIP.open(tbl.filename)
local file = xlsx:open(documentName)
if not file then
xlsx:close()
return
end
local buffer
local stat = xlsx:stat(documentName)
if stat and stat.size and stat.size > 0 then
buffer = file:read(stat.size)
else
---this is original logic, which I presume was due to a limitation of the previous xml reader;
---keeping this chunk as a fallback just in case
local chunks = {}
while true do
local chunk = file:read(8192)
if not chunk or chunk == '' then break end
chunks[#chunks + 1] = chunk
end
buffer = table.concat(chunks)
end
xlsx:close(file)
if not buffer or buffer == '' then return end
return _xlsx_parsexml(buffer)
end
local function _xlsx_loadsheet(workbook, id, name)
local sheetDoc = _xlsx_readdocument(workbook, ("xl/worksheets/sheet%d.xml"):format(id))
local data = {}
local sheetData = sheetDoc and sheetDoc.worksheet[1]['#'].sheetData
local rowNodes = sheetData and sheetData[1]['#'].row
if rowNodes then
local headers = {}
local dataRowIndex = 0
for rowIdx, rowNode in ipairs(rowNodes) do
local rowValues = {}
if rowNode['#'].c then
for _, columnNode in ipairs(rowNode['#'].c) do
local cellId = columnNode['@'].r
local colLetters = cellId and cellId:match(colRowPattern)
local colNum = 0
if colLetters and colLetters ~= '' then
for index = 1, #colLetters do
colNum = colNum * 26 + (colLetters:byte(index) - A_BYTE + 1)
end
end
local colType = columnNode['@'].t
local value
if columnNode['#'].v then
value = columnNode['#'].v[1]['#']
if colType == 's' then
value = workbook.sharedStrings[tonumber(value) + 1]
elseif colType == 'b' then
value = (value == '1')
elseif colType ~= 'str' then
value = tonumber(value)
end
end
rowValues[colNum] = value
end
end
if rowIdx == 1 then
for colNum, value in pairs(rowValues) do
headers[colNum] = (value ~= nil and value ~= '') and tostring(value) or ('Column' .. colNum)
end
else
local record = {}
for colNum, value in pairs(rowValues) do
record[headers[colNum] or ('Column' .. colNum)] = value
end
dataRowIndex = dataRowIndex + 1
data[dataRowIndex] = record
end
end
end
return { name = name, data = data }
end
local __workbookMetatableMembers = {
GetTotalWorksheets = function(self)
return #self.__sheets
end,
GetWorksheet = function(self, key)
return self.__sheets[key]
end,
GetAnsiSheetName = function(self, key)
return self:GetWorksheet(key).name
end,
GetUnicodeSheetName = function(self, key)
return self:GetWorksheet(key).name
end,
GetSheetName = function(self, key)
return self:GetWorksheet(key).name
end,
Sheets = function(self)
local i = 0
return function()
i = i + 1
return self.__sheets[i]
end
end
}
local __workbookMetatable = {
__len = function(self)
return #self.__sheets
end,
__index = function(self, key)
local value = __workbookMetatableMembers[key]
if value then return value end
return self.__sheets[key]
end
}
---@package
---@class XlsxWorkbook
---@field filename string Path the workbook was opened from.
---@field sharedStrings string[] Raw shared-string table from xl/sharedStrings.xml.
---@field workbookDoc table Parsed xl/workbook.xml document (internal, "@"/"#" shape).
---@field __sheets {name:string, data:table[]}[] data table keys are column names and values are row values
---Opens an .xlsx file and parses its shared strings, workbook, manifest, and every worksheet.
---@param filename string Path to the .xlsx file to read.
---@return XlsxWorkbook
function lib.Workbook(filename)
local self = {}
self.filename = filename
local sharedStringsXml = _xlsx_readdocument(self, 'xl/sharedStrings.xml')
self.sharedStrings = {}
if sharedStringsXml then
for _, str in ipairs(sharedStringsXml.sst[1]['#'].si) do
if str['#'].r then
local concatenatedString = {}
for _, rstr in ipairs(str['#'].r) do
local t = rstr['#'].t[1]['#']
if type(t) == 'string' then
concatenatedString[#concatenatedString + 1] = rstr['#'].t[1]['#']
end
end
concatenatedString = table.concat(concatenatedString)
self.sharedStrings[#self.sharedStrings + 1] = concatenatedString
else
self.sharedStrings[#self.sharedStrings + 1] = str['#'].t[1]['#']
end
end
end
self.workbookDoc = _xlsx_readdocument(self, 'xl/workbook.xml')
local sheets = self.workbookDoc.workbook[1]['#'].sheets
self.__sheets = {}
local id = 1
for _, sheetNode in ipairs(sheets[1]['#'].sheet) do
local name = sheetNode['@'].name
local sheet = _xlsx_loadsheet(self, id, name)
self.__sheets[id] = sheet
id = id + 1
end
setmetatable(self, __workbookMetatable)
return self
end
return lib