Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
Hi there,
I am doing a script which writes in a CSV value and name of a list of object selected by a tag.
Since the script is launched with a push button, I am wondering if It Is possibile download the file CSV directly on the browser. I mean such as trend log file push button export.
Thanks as Always.
Br.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code: $(function() {
// additional class must be set to csv
$('.csv').on('vclick', function() {
window.open('/user/csv.lp');
});
});
Use this as a starting point for csv.lp:
Code: <?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
Thank you Admin.
Can I do that in Mosaico as well?
Br
Posts: 4653
Threads: 24
Joined: Aug 2017
Reputation:
208
Mosaic does not have custom js but maybe you could do something in the html widget.
------------------------------
Ctrl+F5
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
(20.11.2020, 06:57)admin Wrote: You can use .lp file to generate a CSV that will be sent to the user.
First you need to attach an event to a button that will open .lp in a new window:
Code: $(function() {
// additional class must be set to csv
$('.csv').on('vclick', function() {
window.open('/user/csv.lp');
});
});
Use this as a starting point for csv.lp:
Code: <?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
csv = '...'
-- output to the user
print(csv)
Hi admin,
sorry but do not understand program language above. If I use <? LM inform me " Lua syntax error at line 1: unexpected symbol near '<'".
Thanks.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
.lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
(24.11.2020, 07:34)admin Wrote: .lp file is not a script, it's a Lua file that only the web server can run. It should be uploaded via FTP to the user directory using apps username. If the file is named csv.lp then the URL will be http://LM_IP/user/csv.lp
Code: <?
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'report', 'csv')
-- generate csv here
value=grp.tag("consumption")
filename = string.format('consumption.csv', os.date('%d-%m-%Y-%H-%M'))
a={}
for index, id in ipairs(value) do
name=id["name"]
meter=id["data"]
consumption=name .."," .. meter
a[index]=consumi
log(a)
end
csv = a
result, err = io.writefile (filename, csv)
-- output to the user
print(csv)
I generate file as above without success. Is it correct the procedure? Thanks.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Try this:
Code: <?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")
a={}
for index, id in ipairs(value) do
name=id["name"]
meter=id["data"]
consumption=name .."," .. meter
a[index]=consumi
end
print(a)
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
(24.11.2020, 09:16)admin Wrote: Try this:
Code: <?
require('apps')
-- set headers for csv file download
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
-- generate csv here
value=grp.tag("consumption")
a={}
for index, id in ipairs(value) do
name=id["name"]
meter=id["data"]
consumption=name .."," .. meter
a[index]=consumi
end
print(a)
Thank you very much Admin, but unfortunately when I launch the script on the visu I receive response from http://192.168.1.10/user/csv.lp that it is not possible reach the web site. The script is correct and I cannot identify the problem.
Thanks.
BR
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
There were some errors in the script, this should work:
Code: <?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
csv = {}
objects = grp.tag('consumption')
for _, obj in ipairs(objects) do
csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end
print(table.concat(csv, '\n'))
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
(24.11.2020, 09:56)admin Wrote: There were some errors in the script, this should work:
Code: <?
require('apps')
setdlheader('text/csv; charset=utf-8', 'Consumption', 'csv')
csv = {}
objects = grp.tag('consumption')
for _, obj in ipairs(objects) do
csv[ #csv + 1 ] = obj.name .. ',' .. tostring(obj.value)
end
print(table.concat(csv, '\n'))
FANTASTIC ADMIN!! THANK YOU VERY MUCH.
I would like only to clerify a thing.
string.gsub(tostring(obj.value), ".", ",")
With this above I would like to replace dot with comma in the number. I do not know why but he replace all the number with ",". Hence 23.88 became ,,,,,
What is the reason?
Thanks.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code: value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.
Posts: 400
Threads: 88
Joined: Jul 2016
Reputation:
3
(24.11.2020, 10:33)admin Wrote: dot is a special symbol that is treated as "any character" by the gsub. You need to escape it like this:
Code: value = string.gsub(tostring(obj.value), "%.", ",")
csv[ #csv + 1 ] = obj.name .. ';' .. value
Since you are using comma as a decimal separator you should use ; instead of comma as csv field separator.
Yes I know. I already use ";" in fact the format of the file in excel as well is perfect. Great Job. Thanks.
Now I work to download file from Mosaic as well.
Thanks.
|