Spreadsheet to Table Plugin

Posted on Saturday the 21st of April, 2007 at 4:48 pm in Plugins

This was an extremely quick project to turn spreadsheet data into an XHTML table. The plugin is quite simple to use, just copy some data from a spreadsheet ( or the visible part of an XHTML table) and paste it into a blog post between the tags [table] and [/table] (make sure that columns are tab separated and rows are on their own line). This is a no frills plugin. The first row is used as the column names and is added to a thead tag. Anything can be added to the [table] tag after “table” and it will become the table’s caption. EX [table "This is an example table"]

So this:

  1. [table "This is an example table"]
  2. gf sfg sfcx dfg
  3. asd sd dsfs
  4. xcv fds sgdf
  5. dfg dfg dfg
  6. dfg dfg dfg
  7. [/table]

Becomes:

“This is an example table”
gf sfg sfcx dfg
asd sd dsfs
xcv fds sgdf
dfg dfg dfg
dfg dfg dfg

Installation:

  1. Copy and Paste the contents of the texbox below into a .php file.
  2. Upload and activate.
  1. <?php
  2. /* Plugin Name: Tabeler Plugin URI: http://anthologyoi.com/plugins/ Description: This plugin allows you to copy data directly from a spreadsheet and paste it into your blog, and will automatically create a table for it. Version: 0.1 Author: Aaron Harun. Author URI: http://anthologyoi.com */
  3. add_filter('the_content','tabler_find',1);
  4. add_filter('the_excerpt','tabler_find',1);
  5. function tabler_find($content) {
  6. return preg_replace_callback('!\[table([^]]*)\](?:\r\n|\n|\r|)([\S\s]*? [\S\s]*?)(?:\r\n|\n|\r|)\[/table\]!ims', 'tabler_callback', $content);
  7. }
  8. function tabler_callback($matches) {
  9. $pos_table = $matches[2];
  10. if(strpos($pos_table,chr(13)) !== FALSE){
  11. $rows = explode(chr(13),$pos_table);
  12. }else{
  13. $rows = explode("\n",$pos_table);
  14. }
  15. if(is_array($rows)){
  16. if($rows > 1){
  17. $table .= '<table>';
  18. foreach($rows as $row){
  19. $i++;
  20. $cols = explode(" ",$row);
  21. if($i==1)
  22. $table .= '<thead>';
  23. $table .= '<tr>';
  24. foreach($cols as $col){
  25. $table .= '<td>'.$col.'</td>';
  26. }
  27. $table .= '</tr>';
  28. if($i==1){
  29. $table .= '</thead>';
  30. $table .= '<tbody>';
  31. }
  32. }
  33. $table .= '</tbody>';
  34. $table .= '</table>';
  35. return $table;
  36. }
  37. }
  38. }
  39. ?>
  40. Use the following to copy and paste the code.

Related posts

  1. Jevin posted the following on April 22, 2007 at 6:58 pm.

    Hi! Just in case you didn’t know. Lots of your pages are giving me Internal Server Errors. Namely, I want to get at the UTW Widget. Cheers!

    Reply to Jevin
    1. Aaron posted the following on April 22, 2007 at 7:14 pm.

      I’m sorry the site was down and I had to restore from a local backup. It is all fixed now.

      (Isn’t AJAX wonderful? It works through the errors.)

      Reply to Aaron
      1. Jevin posted the following on April 22, 2007 at 7:16 pm.

        Wow on the response time!!! Thanks!

        Reply to Jevin
  2. Genx posted the following on May 8, 2007 at 4:26 pm.

    Hello, and thanks for a great idea for a plugin, however i can’t make it work. I pasted the php code into tabeler.php and uploaded it to my /plugins directory and then activated the plugin in wordpress 2.1 but there is no table created when i use [table] and [/table] the page is blank, nothing is displayed.

    Cheers

    Reply to Genx
  3. Mosey posted the following on June 15, 2007 at 11:25 pm.

    I really like the concept behind this plugin :)

    Reply to Mosey
  4. Affen posted the following on October 26, 2007 at 12:29 pm.

    I can’t seem to get Tabeler to work. I followed your instructions and activated the plugin, but when I use the [table] and [/table] tags, they actually show up in the resultant post- even if I copy your example input directly…

    Reply to Affen
    1. Aaron posted the following on October 27, 2007 at 10:30 pm.

      I’ll take a look at the code sometime over the next week and see what is wrong.

      Reply to Aaron

Leave a reply

:) :D :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: ;) :!: :?: :idea: :arrow: :| :mrgreen: