Table from CSV Data Block

How can one create a WikiText table from a copied CSV block, such as data pasted from an Excel sheet?

One can create an inputbox and then process the input by replacing the csv delimiters like comma or tab into pipe and then copy the result to clipboard. Below is a small UI that help to create a wikitext table from csv block. It accepts both tab and comma as delimiter.

Code

\procedure convert-csv()
<!-- create the setting row -->
CSV delimiter:&nbsp;
<$tiddler tiddler="$:/temp/convertxls">
<$radio field=delimiter value="tab" default="tab"> tab</$radio>
<$radio field=delimiter value="comma" default="tab"> comma</$radio>
</$tiddler>&nbsp;|&nbsp;
<$checkbox tiddler="$:/temp/convertxls" field="header" checked="yes" unchecked="no" default="no">&nbsp;Has header?</$checkbox><br/>

<!--Create the input textbox -->
<$macrocall $name="copy-to-clipboard-above-right" src=<<output>> />
<$edit-text tiddler="$:/temp/convertxls" tag=textarea class="tc-max-width"/>
<!-- Preview output checkbox-->
<$checkbox tiddler="$:/temp/convertxls" field="preview" checked="open" unchecked="closed" default="closed"> Prview?</$checkbox>

<!--Define required variables-->
<$let
  newline={{{ [charcode[10]] }}}
	th=     {{{ [<newline>addprefix[h]] }}}
  tab=    {{{ [charcode[9]] }}}
	delim=  {{{ [[$:/temp/convertxls]get[delimiter]match[comma]then[,]else<tab>]  }}}
	header= {{{ [[$:/temp/convertxls]get[header]match[yes]else[no]] }}}
  input=  {{$:/temp/convertxls}}
  output= {{{ [<input>split<newline>!is[blank]search-replace:g:<delim>,[|]addprefix[|]addsuffix[|]join<newline>] }}}
	output= {{{ [<header>match[yes]]:then[<output>search-replace<newline>,<th>]:else[<output>] }}}}  >
	<!-- Preview output-->
	<%if [[$:/temp/convertxls]get[preview]match[open]] %>
		<$transclude $variable="output" $mode="block"/>
	<%endif%>
</$let>
\end

<!-- Example-->
<<convert-csv>>

To give a try:

  • Download Table from CSV.json (1.6 KB)
  • Go to https://tiddlywiki.com
  • Drag and drop the downloaded code into into https://tiddlywiki.com
  • Paste a chunk of data from Excel, Google Sheet or a CSV file
  • Click on copy to clipboard and paste the result in any tiddler you like
  • You may click on preview and see the result before use

Screencast

msedge_img_593_20250508

References

5 Likes

If do not need the converted csv and just want to wikify a csv data block into a table, the best solution is Shiraz quick tables (csv-table). Look at:

https://kookma.github.io/TW-Shiraz/#Tutorial%20Quick%20Tables

1 Like

Well, @Mohammad, this is excellent work.
input= {{$:/temp/convertxls}} is where the pasted data goes before being processed?
And…

<$radio field=delimiter value="tab" default="tab"> tab</$radio>
<$radio field=delimiter value="comma" default="tab"> comma</$radio>

Could this work, I wonder:
<$radio field=delimiter value="/" default="tab"> comma</$radio> for a file path listing?

Use

<$radio field=delimiter value="slash" default="tab"> slash</$radio>

And then replace

delim=  {{{ [[$:/temp/convertxls]get[delimiter]match[comma]then[,]else<tab>]  }}}

with

	rdelim = {{{ [[$:/temp/convertxls]get[delimiter]] }}}
	delim=  {{{ [<rdelim>match[comma]then[,]][<rdelim>match[slash]then[/]] :else[<tab>] }}}

Here in delim you can process any number of delimiters.