Spreadsheet to Table Plugin

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:

[table "This is an example table"]
gf	sfg	sfcx	dfg
asd	sd	dsfs	
xcv	fds		sgdf
dfg		dfg	dfg
	dfg	dfg	dfg
[/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.

<?php
/*
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
*/

add_filter('the_content','tabler_find',1);
add_filter('the_excerpt','tabler_find',1);

function tabler_find($content) {
	return preg_replace_callback('!\[table([^]]*)\](?:\r\n|\n|\r|)([\S\s]*?\t[\S\s]*?)(?:\r\n|\n|\r|)\[/table\]!ims', 'tabler_callback', $content);
}


function tabler_callback($matches) {

	$pos_table = $matches[2];

	if(strpos($pos_table,chr(13)) !== FALSE){
		$rows = explode(chr(13),$pos_table);
	}else{
		$rows = explode("\n",$pos_table);
	}

	if(is_array($rows)){
		if($rows > 1){
			$table .= '<table>';

		foreach($rows as $row){
		$i++;
			$cols = explode("\t",$row);

			if($i==1)
				$table .= '<thead>';

			$table .= '<tr>';

			foreach($cols as $col){
				$table .= '<td>'.$col.'</td>';
			}

			$table .= '</tr>';

			if($i==1){
				$table .= '</thead>';

				$table .= '<tbody>';
			}
		}
			$table .= '</tbody>';

			$table .= '</table>';
			
			return $table;
		}
	}
}

?>

8 thoughts on “Spreadsheet to Table Plugin

  1. 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.)

  2. 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

  3. 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…

  4. Pingback: Spreadsheet to Table Plugin - WP Plugin Archive

  5. What would we do minus the awesome ideas you reveal on this site? Who comes with the perseverance to deal with vital topics in the interests of common subscribers like me? My spouse and i and my girlfriends are very delighted to have your site among the types we generally visit. It is hoped you know how significantly we appreciate your working hard! Best wishes through us all.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>