[TWC] Help improve my inline CSV display script

113 views
Skip to first unread message

julien23

unread,
Dec 11, 2015, 6:38:39 AM12/11/15
to TiddlyWiki
Hi all

I wrote this script to display CSV from URL : [UrlContentScriptCsv]

<script>
var p;
 
if (document.all){
   
// For IE, create an ActiveX Object instance
   p
= new ActiveXObject("Microsoft.XMLHTTP");
 
}
 
else {
   
// For mozilla, create an instance of XMLHttpRequest.
   p
= new XMLHttpRequest();
 
}
p
.open("GET","$1",false);
p
.send(null);
var out = p.responseText.split("\n");
out
= out.join("|\n|");
out
= out.split(";");
wikify
(out.join("|"),place);
</script>

I use it for example in attachments : [FCBnc26_Outputs.csv]
!type
application
!file
..\look_FCBnc26_dev\office\FCBnc26_Outputs.csv

!Link
<<tiddler templateNewFileLink with: {{tiddler.title}}>>
!Content
<<tiddler UrlContentScriptCsv with:../look_FCBnc26_dev/office/FCBnc26_Outputs.csv>>


But
  • first line misses a "|" at the begening
  • last lines is a lonely "|"
I am not enought skilled to do that.

Can you please help me ?

Regards

Julien


Tobias Beer

unread,
Dec 11, 2015, 8:12:14 AM12/11/15
to TiddlyWiki
Hi Julien,
  • first line misses a "|" at the begening
  • last lines is a lonely "|"


Try...

var out = p.responseText.split("\n");
out = out.join("|\n");
out = out.split(";");

wikify
("|" + out.join("|"),place);

Best wishes,

Tobias.

julien23

unread,
Dec 11, 2015, 9:00:51 AM12/11/15
to TiddlyWiki
Hi Tobias

wikify("|" + out.join("|"),place);
has fixed the " first line misses a "|" at the begening "

but
out = out.join("|\n");
was a mistake. It removes the starting "|" for every other lines.

still trying/failing :)

++

JBo

Eric Shulman

unread,
Dec 11, 2015, 9:41:30 AM12/11/15
to TiddlyWiki
On Friday, December 11, 2015 at 3:38:39 AM UTC-8, julien23 wrote:
Hi all
I wrote this script to display CSV from URL : [UrlContentScriptCsv]

<script>
...

var out = p.responseText.split("\n");
out
= out.join("|\n|");
out
= out.split(";");
wikify
(out.join("|"),place);
</script>
  • first line misses a "|" at the begening
  • last lines is a lonely "|"
It seems that what you are trying to do is to make each line into a table row, where the input data is delimited by semi-colons.

To achieve this, there are three actions to apply:

* change all ";" to "|"
out = out.split(";").join("|");

* add "|" to beginning and ending of each line
out = "|" + out.split("\n").join("|\n|") + "|";

* if the input data includes a trailing newline, then there will be an extra "|" at the end of the output.  To avoid this, remove the trailing "\n"
out = out.replace(/\n$/,"");

Combining these three actions into one line:
out = "|" + out.split(";").join("|").replace(/\n$/,"").split("\n").join("|\n|") + "|";

That should do it.  Let me know if you get stuck.

enjoy,
-e
Eric Shulman
ELS Design Studios
TiddlyTools - "Small Tools for Big Ideas!"
InsideTiddlyWiki: The Missing Manuals

YOUR DONATIONS ARE VERY IMPORTANT!
HELP ME TO HELP YOU - MAKE A CONTRIBUTION TO MY "TIP JAR"...

Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions:

Tobias Beer

unread,
Dec 11, 2015, 10:21:29 AM12/11/15
to tiddl...@googlegroups.com
Hi Julien,
 
was a mistake. It removes the starting "|" for every other lines.

Of course. Now I can see that. Would have better been:

var out = "|" +
p.responseText
.split("\n")
.join("|\n|")
.split(";")
.join("|")
.replace(/\n|$/g,"\n");

wikify(out,place);

Best wishes,

Tobias.

stevesuny

unread,
Dec 11, 2015, 12:01:32 PM12/11/15
to TiddlyWiki
Hello Julien, if this works for you, could you post an example of the whole script you end up with? Thanks! //steve.


On Friday, December 11, 2015 at 10:21:29 AM UTC-5, Tobias Beer wrote:
Hi Julien,
 
was a mistake. It removes the starting "|" for every other lines.

Of course. Now I can see that. Would have better been:

var out = "|" +
p.responseText
.split("\n")
.join("|\n|")
.split(";")
.join("|")
.replace(/\n|$/,"\n");

wikify(out,place);

Best wishes,

Tobias.

Matabele

unread,
Dec 12, 2015, 1:22:39 AM12/12/15
to TiddlyWiki
Hi Julien

Seconded -- I would also be interested in the final version of the script :-)

regards

julien23

unread,
Dec 17, 2015, 12:09:17 PM12/17/15
to TiddlyWiki
Hi all

Thanks Eric and Tobias for help.

Here is my final code :

[UrlContentScriptCsv]
<script>
var p;
 
if (document.all){
   
// For IE, create an ActiveX Object instance
   p
= new ActiveXObject("Microsoft.XMLHTTP");
 
}
 
else {
   
// For mozilla, create an instance of XMLHttpRequest.
   p
= new XMLHttpRequest();
 
}
p
.open("GET","$1",false);
p
.send(null);
var out = p.responseText;

out
= out.replace(/\n$/,"")

out
= out.split(";").join("|");

out
= "|" + out.split("\n").join("|\n|") + "|";

wikify
(out.replace("|\n","|h\n"),place);
</script>



You can place it  into attachments like :

[plouf.csv]
!type
application
!file
..\matlab\plouf.csv

!Link
<<tiddler templateNewFileLink with: {{tiddler.title}}>>
!Content
<<tiddler UrlContentScriptCsv with:../matlab/plouf.csv>>


Works with
Win7 / FF43.0

++

JBo

Reply all
Reply to author
Forward
0 new messages