SheetJS Behavior
To install this JPSWiki extension, you will need Admin rights; and have the Haddock Template activated.
SheetJS is an Apache License 2.0 parser and writer for various spreadsheet formats. It is a pure-JS cleanroom implementation from official specifications, related documents, and test files.
SheetJS Behavior is a JSPWiki javascript extension which allows to display various Excel spreadsheet formats and CSV files.
Find below the install instructions. See SheetJS for how-to-use examples.

How to installation the SheetJS extension#

Step 1: Create JS file#

Create a javascript behavior file SheetJS.js with the following code snippet and save it in <your-wiki-site>/scripts folder. This script automatically pulls the library from the Content delivery network at http://cdnjs.com/libraries/xlsx.

!function(a,b){function j(a){var b,d,f,i="wikitable",j=a.innerHTML;a.className.test(/table(\-\S+)/i)&&(i+=RegExp.$1.replace(/\-/g," table-"));if(j.test(/^\s*<a class="attachment" href="([^"]+)">([^<]+)<\/a>/i))b=RegExp.$1,d=RegExp.$2,g(b,function(b){var f=c.read(b,{type:"binary"});if(!f)return e("parsing wb");var g=f.Sheets[d]||f.Sheets[f.SheetNames[0]];if(!g)return e("ws not found");h(g,a,i)});else return e("no worksheet data found")}function i(a,b){var c=1,e=d.decode_range(a["!ref"]),f=e.s.c,g=e.e.c+1,h=[];b=b.replace(/<tr><td/g,function(){return"<tr><td class='row'>"+c++ +"</td><td"});while(f<g)h.push("<td class='col'>"+d.encode_col(f++)+"</td>");return b.replace(/<tr>/,"<tr><td class='row col'></td>"+h.join("")+"</tr><tr>")}function h(a,c,e){var f=d.sheet_to_html(a,{header:"",footer:""});f=f.replace(/<table>/,'<table class="'+e+'">'),f=i(a,f),c.className=b.substr(1)+"-done",c.grab(("div"+b+"-result").slick({html:f}),"after")}function g(a,b){var c=new XMLHttpRequest;c.open("GET",a),c.responseType="arraybuffer",c.onload=function(a){b(f(c))},c.onerror=function(b){e("loading wb",a,b)},c.send()}function f(a){return String.fromCharCode.apply(null,new Uint8Array(a.response))}function e(a,b,c){console.log("ERR",a,b,c)}var c,d;a.once(b,function(){"script".slick({src:"//cdnjs.cloudflare.com/ajax/libs/xlsx/0.11.3/xlsx.full.min.js",events:{load:function(e){c=XLSX,d=c.utils,a.add(b,j).update()}} }).inject(document.head)})}(Wiki,".worksheet");

Step 2: Update JSP files#

Add following lines to <your-wiki-site>/templates/haddock/commonheader.jsp.

  <!-- existing line -->
  <script async src="<wiki:Link format='url' jsp='scripts/haddock.js'/>"></script>
  <!-- add this line -->
  <script src="<wiki:Link format='url' jsp='scripts/SheetJS.js'/>" ></script>
And do the same in <your-wiki-site>/templates/reader/ViewTemplate.js.

You are now ready to display Spreadsheets and CSV files from your wiki-pages !
No need to restart your JSPWiki server.


Category.Haddock Behavior