Displaying CSV table from URL

Hi,

I found your software a few days ago an I really like it!
I’m still new to Xwiki, so please bear with me.

Is there a way to read a csv from an URL and display the content on a page.

The URL should consist of a base path that is configured on a central location and a property of the current page. E.g: http://example.com/myfiles/${mypage.csvname}.csv

Thanks for your help.

You can do it using the Velocity Macro and $xwiki.getURLContent(...).

Hi,

thank you very much, this already allows me to retrieve the “raw” csv data into the page. Is there a way to parse the data (in velocity ) into a xwiki table? I looked through the docs but could not find anything like this.
Or should I use groovy for this?

Thanks for your help.

I think it would be simpler indeed if you used groovy, python or ruby for the parsing. We don’t offer any specific API for CSV parsing that you could use from velocity.

Hi,
so I made some progress.
I created a macro page (CsvMacro) with a groovy macro based on MacroClass and a parameter (csvUrl).
If I execute the macro from another page with a hardcoded url as parameter everything is fine.
But how can I “call” the macro with a “dynamic parameter”, e.g. csvUrl=$rootUrl/$filename?

Thanks in advance.

You need to wrap the macro call in a script macro. The easiest is to use Velocity.

{{velocity}}
#set ($rootUrl = ...)
#set ($filename = ...)
{{csv url="$rootUrl/$filename" /}}
{{/velocity}}

Another option is to add support for Velocity code in the URL parameter, i.e. to parse the value of the URL macro parameter assuming that it contains Velocity. I prefer the first option though because it allows you to use any scripting language you want.

I use option one and it works great. Thanks. Using #set instead of #$set makes all the difference. :wink:
Still trying to get the hang of this “Velocity-stuff”…

May I suggest to add another section to this tutorial (http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingMacros/WikiMacroTutorial/) describing exactly this usecase?

Feel free to add it, it’s a wiki :wink:

done: http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingMacros/WikiMacroTutorial/#HParameters

Thanks!