-
imagine I have this minimal CSV file:
I need use your csv = require("csv")
xml = require("xml")
csvtab, msg = csv.decode("csv-file.csv")
if not csvtab then
print(msg)
os.exit(-1)
end
csvtab._name = "data"
for i=first_line,#csvtab do
csvtab[i]._name = "row_" .. tonumber(i)
for j=1,#csvtab[i] do
local val = csvtab[i][j]
csvtab[i][j] = { _name = "cell_" .. tonumber(j), val }
end
end
ok, err = xml.encode_table(csvtab)
if not ok then
os.exit(-1)
end The requirements of the above CSV data for data split are:
How could I achieve that? BTW, I would say that in previous versions had not required Many thanks for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Not sure if I understand it correctly and I am not sure if this is really a speedata Publisher issue, but you can try something like this: csv = require("csv")
xml = require("xml")
csvtab, msg = csv.decode("data.csv")
if not csvtab then
print(msg)
os.exit(-1)
end
dofile("debug.lua")
local datatab
local datacounter = 0
for i=1,#csvtab do
local has_value = false
local val = {}
for j=1,#csvtab[i] do
val[#val+1] = {_name = "cell", csvtab[i][j]}
if csvtab[i][j] ~= "" then has_value = true end
end
if not has_value or i == #csvtab then
if datatab then
datacounter = datacounter + 1
ok, err = xml.encode_table(datatab,string.format("data%d.xml",datacounter))
if not ok then
os.exit(-1)
end
end
datatab = nil
else
datatab = datatab or { _name = "data" }
datatab[i] = { _name = "row" }
for v = 1, #val do
datatab[i][#datatab[i]+1] = val[v]
end
end
end
os.exit(0) to split the CSV table. |
Beta Was this translation helpful? Give feedback.
-
besides my own use, I would like to offer an example of single-document generation from CSV data with Publisher. The filter works fine, but I'm afraid I haven't expressed myself accurately enough. From such a CSV file:
I need single XML files for Mary, Anne, John and Jane. I mean, when cell from column A in LibreOffice Calc is not empty, a new XML file should be generated. I have tried to modify your code (with a csv = require("csv")
xml = require("xml")
csvtab, msg = csv.decode("c.csv")
if not csvtab then
print(msg)
os.exit(-1)
end
-- dofile("debug.lua")
local datatab
local datacounter = 0
for i=1,#csvtab do
local has_value = false
local start_file = false
local val = {}
for j=1,#csvtab[i] do
val[#val+1] = {_name = "cell", csvtab[i][j]}
if csvtab[i][j] ~= "" then has_value = true end
if j == 1 and csvtab[i][j] ~= "" then start_new = true end
end
if not has_value or i == #csvtab then
if datatab and start_new then
datacounter = datacounter + 1
ok, err = xml.encode_table(datatab,string.format("data%d.xml",datacounter))
if not ok then
os.exit(-1)
end
end
datatab = nil
else
datatab = datatab or { _name = "data" }
datatab[i] = { _name = "row" }
for v = 1, #val do
datatab[i][#datatab[i]+1] = val[v]
end
end
end
os.exit(0) I don't know where to include BTW, Many thanks for your help. |
Beta Was this translation helpful? Give feedback.
-
I'm afraid your original filter from your first reply to this message is not able to export the last line. With the CSV data from my original report, Sorry, but I don't know why the last line of the CSV file isn't read with the filter. Many thanks for your help. |
Beta Was this translation helpful? Give feedback.
Not sure if I understand it correctly and I am not sure if this is really a speedata Publisher issue, but you can try something like this: