Export to csv

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Export to csv

thiago_sl
Hi list,
Is there a way in Iliad to export an ILTable content (or any
Collection) to CSV, like http://www.seaside.st/documentation/faq/how-to#12248692
?


Cheers,
Thiago
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

Bernat Romagosa
Hi Thiago,

I managed this a while ago in a Seaside project, a CSV file is just a plain text file with a particular format, it is very easy to create it by yourself.

Here's a (simplified) example:

generateCSVfileNamed: aFileName forProjectCollection: aProjectCollection
| aCSVfile |
aCSVfile := FileStream fileNamed: aFileName , '.csv'.

aCSVfile 
nextPutAll:
'"Deadline","Client","Project name"'
, String cr.

aProjectCollection
do: [ :eachProject | 
aCSVfile nextPutAll: ('"' , eachProject deadLine asString , '","' , eachProject client , '","' , eachProject projectName , '"' , String cr) ].
aCSVfile close

First we create the header row with all the column titles, then we fill the rows up. Hope it helps :)

Cheers,

2011/7/15 Thiago SL <[hidden email]>
Hi list,
Is there a way in Iliad to export an ILTable content (or any
Collection) to CSV, like http://www.seaside.st/documentation/faq/how-to#12248692
?


Cheers,
Thiago



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

Stefan Schmiedl
On Fri, 15 Jul 2011 15:38:17 +0200
Bernat Romagosa <[hidden email]> wrote:

> * aCSVfile nextPutAll: ('"' , eachProject deadLine asString , '","' ,
> eachProject client , '","' , eachProject projectName , '"' , String cr) ].*
> * aCSVfile close*
>
> First we create the header row with all the column titles, then we fill the
> rows up. Hope it helps :)
>
> Cheers,
>
> 2011/7/15 Thiago SL <[hidden email]>
>
> > Hi list,
> > Is there a way in Iliad to export an ILTable content (or any
> > Collection) to CSV, like
> > http://www.seaside.st/documentation/faq/how-to#12248692
> > ?

One thing I have learned (with pain) is that when dealing with CSV
it is important to know who your intended audience is.

Typical setup: .csv -> smart folder display software makes it look
like a spreadsheet -> user double clicks on icon -> spreadsheet
opens and all data ends up in the first column, because German
Excel only recognizes a semicolon ; as column separator.

Depending on the contents of your fields, the code above might
work. It will fail, if you have users entering things like

        12" tire

So you'll have to escape the text field delimiter to increase
the chances of producing a readable CSV file.

And things get really exciting (hah!) when the data can contain
line breaks. There are so-called CSV "parsers" out there that
go for "probably forgot to close the field" and start a new
line, and there are parsers out there that assume that the producer
knows what he's doing and embed the line break into the value.

If you work within well-defined boundaries, CSV is a simple
format that will get things done quickly. But if you target a
diverse audience, try to a avoid it.

Good luck,
s.
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

thiago_sl
Hi all!
Bernat  and Stefan, thank you so much for your help and suggestions!!
What i am trying to do is extend ILDataGridFooter.
My ILTableExtension render simple collections (Persons, Products,
Orders, etc.).
I want to put anchor(s) at footer to an easy 'print/export' function.

There are another 'ready-to-use' ways to do that (via jQuery plugins),
but want to 'hack' Iliad too.

Thiago S. Lino

On 15 jul, 13:44, Stefan Schmiedl <[hidden email]> wrote:

> On Fri, 15 Jul 2011 15:38:17 +0200
>
>
>
> Bernat Romagosa <[hidden email]> wrote:
> > * aCSVfile nextPutAll: ('"' , eachProject deadLine asString , '","' ,
> > eachProject client , '","' , eachProject projectName , '"' , String cr) ].*
> > * aCSVfile close*
>
> > First we create the header row with all the column titles, then we fill the
> > rows up. Hope it helps :)
>
> > Cheers,
>
> > 2011/7/15 Thiago SL <[hidden email]>
>
> > > Hi list,
> > > Is there a way in Iliad to export an ILTable content (or any
> > > Collection) to CSV, like
> > >http://www.seaside.st/documentation/faq/how-to#12248692
> > > ?
>
> One thing I have learned (with pain) is that when dealing with CSV
> it is important to know who your intended audience is.
>
> Typical setup: .csv -> smart folder display software makes it look
> like a spreadsheet -> user double clicks on icon -> spreadsheet
> opens and all data ends up in the first column, because German
> Excel only recognizes a semicolon ; as column separator.
>
> Depending on the contents of your fields, the code above might
> work. It will fail, if you have users entering things like
>
>         12" tire
>
> So you'll have to escape the text field delimiter to increase
> the chances of producing a readable CSV file.
>
> And things get really exciting (hah!) when the data can contain
> line breaks. There are so-called CSV "parsers" out there that
> go for "probably forgot to close the field" and start a new
> line, and there are parsers out there that assume that the producer
> knows what he's doing and embed the line break into the value.
>
> If you work within well-defined boundaries, CSV is a simple
> format that will get things done quickly. But if you target a
> diverse audience, try to a avoid it.
>
> Good luck,
> s.
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

Bernat Romagosa
Stefan is right, my code (and any csv handcrafting code) will only work if you know very well what kind of audience is it intended for. In my case I knew there wouldn't be many special cases, but this was an app for a pretty small company of only 10 employees and about 50 freelancers, so all issues (like German people trying to read the automatically generated csv files) could be dealt with one by one.

If your audience is not going to be that centralized, maybe you'd better check out other options or make sure these special cases will never happen (don't allow special characters in these text fields, warn German users beforehand, etc).

Good luck!

2011/7/15 Thiago SL <[hidden email]>
Hi all!
Bernat  and Stefan, thank you so much for your help and suggestions!!
What i am trying to do is extend ILDataGridFooter.
My ILTableExtension render simple collections (Persons, Products,
Orders, etc.).
I want to put anchor(s) at footer to an easy 'print/export' function.

There are another 'ready-to-use' ways to do that (via jQuery plugins),
but want to 'hack' Iliad too.

Thiago S. Lino

On 15 jul, 13:44, Stefan Schmiedl <[hidden email]> wrote:
> On Fri, 15 Jul 2011 15:38:17 +0200
>
>
>
> Bernat Romagosa <[hidden email]> wrote:
> > * aCSVfile nextPutAll: ('"' , eachProject deadLine asString , '","' ,
> > eachProject client , '","' , eachProject projectName , '"' , String cr) ].*
> > * aCSVfile close*
>
> > First we create the header row with all the column titles, then we fill the
> > rows up. Hope it helps :)
>
> > Cheers,
>
> > 2011/7/15 Thiago SL <[hidden email]>
>
> > > Hi list,
> > > Is there a way in Iliad to export an ILTable content (or any
> > > Collection) to CSV, like
> > >http://www.seaside.st/documentation/faq/how-to#12248692
> > > ?
>
> One thing I have learned (with pain) is that when dealing with CSV
> it is important to know who your intended audience is.
>
> Typical setup: .csv -> smart folder display software makes it look
> like a spreadsheet -> user double clicks on icon -> spreadsheet
> opens and all data ends up in the first column, because German
> Excel only recognizes a semicolon ; as column separator.
>
> Depending on the contents of your fields, the code above might
> work. It will fail, if you have users entering things like
>
>         12" tire
>
> So you'll have to escape the text field delimiter to increase
> the chances of producing a readable CSV file.
>
> And things get really exciting (hah!) when the data can contain
> line breaks. There are so-called CSV "parsers" out there that
> go for "probably forgot to close the field" and start a new
> line, and there are parsers out there that assume that the producer
> knows what he's doing and embed the line break into the value.
>
> If you work within well-defined boundaries, CSV is a simple
> format that will get things done quickly. But if you target a
> diverse audience, try to a avoid it.
>
> Good luck,
> s.



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

thiago_sl
Hi list,
after the file creation, i am trying to 'response' this file(open the
download dialog), without success.
I create a redirect response, and call 'application returnResponse:
resp ', without any effect .
Any suggestion?

thanks for patience and help.



MyILApplication >> respondInCSV: aCSVFileName
        | resp stream|
        stream :=  ( StandardFileStream readOnlyFileNamed:  aCSVFileName )
readStream .
        resp := ILResponse redirect
                contentType:'text/comma-separated-values';
                attachmentWithFileName: aCSVFileName;
                yourself.
        [ resp nextPutAll: stream binary contents  ]
                ensure: [stream close].
        self application returnResponse: resp
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

Bernat Romagosa
Hi Thiago,

I only managed to do this by writing the file to disk and serving it with Apache... but if you finally find out how to do it I'd love to hear :)

Unfortunately I don't know anything about generating custom responses in Iliad.. maybe someone else can help here...

Cheers,

2011/8/9 Thiago SL <[hidden email]>
Hi list,
after the file creation, i am trying to 'response' this file(open the
download dialog), without success.
I create a redirect response, and call 'application returnResponse:
resp ', without any effect .
Any suggestion?

thanks for patience and help.



MyILApplication >> respondInCSV: aCSVFileName
       | resp stream|
       stream :=  ( StandardFileStream readOnlyFileNamed:  aCSVFileName )
readStream .
       resp := ILResponse redirect
               contentType:'text/comma-separated-values';
               attachmentWithFileName: aCSVFileName;
               yourself.
       [ resp nextPutAll: stream binary contents  ]
               ensure: [stream close].
       self application returnResponse: resp



--
Bernat Romagosa.
Reply | Threaded
Open this post in threaded view
|

Re: Export to csv

thiago_sl
Hi Bernat,

no, i cant manage the response as i like  :(.
Serving that dynamic files via Apache is an option, but i really like
to do that with the response (for learning, too)

thanks for your help.

Thiago,

On 10 ago, 07:33, Bernat Romagosa <[hidden email]>
wrote:

> Hi Thiago,
>
> I only managed to do this by writing the file to disk and serving it with
> Apache... but if you finally find out how to do it I'd love to hear :)
>
> Unfortunately I don't know anything about generating custom responses in
> Iliad.. maybe someone else can help here...
>
> Cheers,
>
> 2011/8/9 Thiago SL <[hidden email]>
>
>
>
>
>
>
>
>
>
> > Hi list,
> > after the file creation, i am trying to 'response' this file(open the
> > download dialog), without success.
> > I create a redirect response, and call 'application returnResponse:
> > resp ', without any effect .
> > Any suggestion?
>
> > thanks for patience and help.
>
> > MyILApplication >> respondInCSV: aCSVFileName
> >        | resp stream|
> >        stream :=  ( StandardFileStream readOnlyFileNamed:  aCSVFileName )
> > readStream .
> >        resp := ILResponse redirect
> >                 contentType:'text/comma-separated-values';
> >                 attachmentWithFileName: aCSVFileName;
> >                yourself.
> >        [ resp nextPutAll: stream binary contents  ]
> >                ensure: [stream close].
> >        self application returnResponse: resp
>
> --
> Bernat Romagosa.