Table of Contents

Introduction

This template will aid you in exporting the raw data from a table. It will create a file or it will open a new window where you can copy the delimited text from.  I would advise that you upload the javascript attached to this page into your own wiki and change the reference in the template.

NOTE:  Attached is a new version of table2CSV.js ensure you download it even if you had it once before.  Also you now need the attached PHP file.

History

Version Date Author Description
2.0 10/29/2009 rberinger Added the ability to actually download a file
1.0 09/22/2009 rberinger Initial Release

Requirements

  • This template requires MindTouch version: Only tested on MindTouch Core (Lyons)
  • Also requires dlstring.php (Attached as dlstring.txt you must rename).

How do I install it?

Setting up the Template
  1. Create a template, call it "Template:Table2CSV" (or rename as you desire).  You must have UNSAFECONTENT permission for this to work.
  2. Create a "DekiScript" block on the template page (use the "Style" menu in the editor")
  3. Copy the code from the end of this page and paste it into the DekiScript block.  To copy, click "expand source", then mouse over the top right corner of the source code, and click the "view source" button.  This will pop up a window with the source code.  Select all, then copy to clipboard.
  4. Make sure there isn't an extra blank paragraph after the DekiScript block! Do this every time you edit!!!  
  5. Save.
 Setting up the PHP
  1. Create a new folder under: /var/www/dekiwiki/deki/dl
  2. Copy the dlstring.txt attached to this page into the folder you just created.
  3. Rename the file to dlstring.php (mv ./dlstring.txt ./dlstring.php)
  4. Make it executable: (sudo chmod +x dlstring.php)
  5. Done.

A quick note about the examples on this page

For all the examples on this page, the code is shown before the working example.  The code is shown with the syntax extension, and looks like this:

table2CSV{id: str?, text: str?, separator: str?, header: [ list? ], button: bool? }

This means that the actual code on the page should be enclosed in a DekiScript block.  If you want to copy the code from this page, then use the same procedure as described in steps 2-4 above. 

How do I use it?

 

Arguments

Name
Type
Default
Description
id str n/a ID or Class of table to export
text str Export Table Text to Name Button or Link
separator str , (comma) The column separator
header list n/a List of alternate column headers
button true/false false True will present button false will present Link
tofile true/false true True will create a file for download, False will open a window and show results

Examples

Here is the table(s) that we will be using for this example:  NOTICE that each has its own ID but both have the same class.

 <table cellspacing="1" cellpadding="1" border="1" width="100%" class="exporttable" id="exportme" style="table-layout: _cke_saved_style="table-layout: fixed;">
    <thead>
        <tr>
            <th scope="col">col1</th>
            <th scope="col">col2</th>
            <th scope="col">col3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>value 1-1</td>
            <td>value 1-2</td>
            <td>value 1-3</td>
        </tr>
        <tr>
            <td>value 2-1</td>
            <td>value 2-2</td>
            <td>value 2-3</td>
        </tr>
    </tbody>
</table>

<table cellspacing="1" cellpadding="1" border="1" width="100%" class="exporttable" id="exportthis" style="table-layout: fixed;">
    <thead>
        <tr>
            <th scope="col">col1-1</th>
            <th scope="col">col2-2</th>
            <th scope="col">col3-3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>value 1-1-1</td>
            <td>value 1-2-2</td>
            <td>value 1-3-3</td>
        </tr>
        <tr>
            <td>value 2-1-1</td>
            <td>value 2-2-2</td>
            <td>value 2-3-3</td>
        </tr>
    </tbody>
</table> 
/content/body/div[4]/div[2]/div[4]/pre[2], line 1, column 142: "=" expected (click for details)

This example will simply present a button that when click will show the text of the first table since it was passed the ID of the first table.

In a DekiScript Block:

table2CSV{ id: 'exportme',
            text: 'Show Values',
            button: true,
            tofile: false
        } 

Here is the result

 

This example will simply present a button that when click will show the text of the first table since it was passed the ID of the first table, and will change the separator and column header values.

In a DekiScript Block:

table2CSV{ id: 'exportme',
            text: 'Show Values',
            button: true,
            separator: '|',
            header: ['Column 1', 'Column 2', 'Column 3'],
            tofile: false
        } 

Here is the result

 

 

This example will present a link that when clicked will show the text of both tables since it was passed a class instead of an ID.

In a DekiScript Block:

table2CSV{ id: '.exporttable',
            text: 'Show Values',
            button: false,
            tofile: false
        } 

Here is the result

Show Values

 

Reference(s)

Discussion about this template should be directed to this forum thread.

 

Credits/Special Thanks

 The original web site where I found the .js file is here.  Sorry I don't know his/her name.  It ha

 

Template/Extension Source Code

Template Source Code
var classorid = ($id ?? $0 ?? '#exportthis');
var text = ($text ?? $1 ?? 'Export Table');
var separator = ($separator ?? $2 ?? ',');
var header = ($header ?? $3 ?? []);
var button = ($button ?? $4 ?? false);
var tofile = ($tofile ?? $5 ?? true);

var options = {separator: (separator)};

if(#header != 0) {
    let options ..= {header: (header)};
}

if(tofile) {
    let options ..= {delivery:'value'};
}



// var ctortext = 'when($this.click) $('..json.emit(options)..').table2CSV('..json.emit(options)..');';

if(button) {
    <input value=(text) type="button" ctor="when($this.click) GetTable()" />
} else {
    <a href="#" ctor="when($this.click) GetTable(); return false;">;text;</a>
}

<html><head>
<script type="text/javascript" src="http://developer.mindtouch.com/@api/deki/files/4807/=table2CSV.js" ></script>
<script type="text/javascript">"
var GetTable = function() {
                    var myOpts = "..json.emit(options)..";
                    var tbl = "..json.emit(classorid)..";
                    var siteURI = "..json.emit(site.uri).."
                    var fulluri = siteURI + 'deki/dl/dlstring.php?csv_output=';

                    if(myOpts.delivery == 'value') {
                        var csv = $(tbl).table2CSV(myOpts);
                        location.href = fulluri + encodeURIComponent(csv);
                    } else {
                        $(tbl).table2CSV(myOpts);
                    }
                }
"</script>
</head></html>
 PHP File Source Code
<?php
/*
This file will generate our CSV table. There is nothing to display on this page,
it is simply used to generate our CSV file and then exit. That way we won't be
re-directed after pressing the export to CSV button on the previous page.
*/

/*
First we'll generate an output variable called out. It'll have all of our text
for the CSV file.
*/
$out = '';

/*Next we'll check to see if our variables posted and if they did we'll simply
append them to out.
*/
if (isset($_GET['csv_output'])) {
$out .= $_GET['csv_output'];
}

/*Now we're ready to create a file. This method generates a filename based on
 the current date & time.
*/
$filename = $file."_".date("Y-m-d_H-i",time());

//Generate the CSV file header
header("Content-type: application/vnd.ms-excel;");
//header("Content-type: text/plain; charset=utf-8");
header("Content-disposition: attachment" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");

//Print the contents of out to the generated file.
print stripslashes($out);

//Exit the script
exit;
?>

Disclaimers

None.

Tag page (Edit tags)

Files 2

FileSizeDateAttached by 
dlstring.txt
No description
1091 bytes19:47, 28 Oct 2009rberingerActions
table2CSV.js
No description
2.42 kB19:47, 28 Oct 2009rberingerActions
Viewing 1 of 1 comments: view all
Very nice Feature. I am searching for a similar application for exporting tables into a vCard (RFC2426). With that it would be simple to edit some contacts in the wiki but simple download a .vct file for import in Outlook.
Do you think it is a very problem to program such a thing?
Posted 14:03, 9 Mar 2010
Viewing 1 of 1 comments: view all
You must login to post a comment.