18.10.2023, 12:00
This script generates CSV with datatypes that can be imported into ETS 6.1.0. csv variable contains the CSV file data as a string.
Code:
function csvescape(val)
val = tostring(val)
val = val:gsub('"', '""'):gsub('^[-=+@\t\r]', "'%0")
return '"' .. val .. '"'
end
function getdpst(dpt)
dpt = tonumber(dpt)
if not dpt then
return ''
elseif dpt < 1000 then
return 'DPT-' .. dpt
else
local mdpt = dpt % 1000
dpt = math.floor(dpt / 1000)
return 'DPST-' .. dpt .. '-' .. mdpt
end
end
objs = grp.all()
buf = { '"Group name";"Address";"Central";"Unfiltered";"Description";"DatapointType"' }
for _, obj in ipairs(objs) do
if obj.id < 0x10000 then
row = {
csvescape(obj.name or ''),
csvescape(obj.address),
'""',
'""',
csvescape(obj.comment or ''),
csvescape(getdpst(obj.datatype)),
}
buf[ #buf + 1 ] = table.concat(row, ';')
end
end
csv = table.concat(buf, '\n')