Ability to select workbooks. Fixes #4

This commit is contained in:
Eric Wood 2013-08-24 16:24:03 -05:00
parent bd6fd498aa
commit c5b4fa8f40
2 changed files with 197 additions and 130 deletions

View File

@ -1,6 +1,15 @@
excelParser = {
!(function($) {
"use strict";
// Global variable with information on current workbooks. Don't judge me.
var workbooks = {};
// Another global to store LaTeX output. Don't judge me...again...
var latexOutput = {};
var excelParser = {
latexEscape: function(text) {
escapeRegExpr = function(str) {
var escapeRegExpr = function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};
@ -31,12 +40,12 @@ excelParser = {
var numCols = max;
var args = [];
for(var i=0; i < numCols; i++) {
for(i=0; i < numCols; i++) {
args[i] = 'l';
}
args = ' | ' + args.join(' | ') + ' | ';
var latex = "\\begin{tabular}{" + args + "}\n\\hline\n";
for(var i=0; i < table.length; i++) {
for(i=0; i < table.length; i++) {
var cols = table[i];
// TODO: replace "&" with "\&"
if(cols === undefined) { cols = []; }
@ -52,7 +61,7 @@ excelParser = {
latex += "\\end{tabular}\n";
$('#latex-output').val(latex);
return latex;
},
processSheet: function(data, stringTable) {
@ -63,15 +72,15 @@ excelParser = {
var rows = doc.find('sheetdata row');
$.each(rows, function(i,row) {
var rowNum = parseInt($(row).attr('r'));
var rowNum = parseInt($(row).attr('r'), 10);
// get columns
var cols = $(row).find('c');
var colVals = $.map(cols, function(col,j) {
var col = $(col);
col = $(col);
var val = excelParser.latexEscape(col.find('v').text());
if(col.attr('t') == 's') {
return excelParser.latexEscape(stringTable[parseInt(val)]);
return excelParser.latexEscape(stringTable[parseInt(val, 10)]);
} else {
return val;
}
@ -82,22 +91,53 @@ excelParser = {
return table;
},
handleSheet: function(entries, stringTable) {
handleSheets: function(entries, stringTable) {
// get the workbook.xml file, which contains the names of all workbooks
var workbookMeta = entries.filter(function(entry) {
return entry.filename === 'xl/workbook.xml';
})[0];
// filter out all files that aren't worksheets
var sheets = $.grep(entries, function(n,i) {
return (/^xl\/worksheets\/.*\.xml$/.test(n.filename)) && (!/^xl\/worksheets\/_rels/.test(n.filename));
var filter1 = /^xl\/worksheets\/.*\.xml$/;
var filter2 = /^xl\/worksheets\/_rels/;
return (filter1.test(n.filename)) && (!filter2.test(n.filename));
});
// for now, only process the first sheet
// TODO: give the user a choice of which sheet to process
var sheet = sheets[0];
// read the workbook meta data to get the names and crap
workbookMeta.getData(new zip.TextWriter(), function(text) {
var doc = $(text);
// extract the names of the workbooks and their IDs for use later on...
$.each(doc.find('sheets sheet'), function(i, tag) {
tag = $(tag);
var id = tag.attr('sheetId');
var name = tag.attr('name');
workbooks[id] = name;
});
excelParser.updateSelect();
// iterate over all sheets and convert them to LaTeX
$.each(sheets, function(_, sheet) {
// the ID of the spreadsheet can only be found in the filename apparently :P
var id = sheet.filename.match(/(\d)\.xml/)[1];
sheet.getData(new zip.TextWriter(), function(text) {
var table = excelParser.processSheet(text, stringTable);
excelParser.toLatex(table);
var latex = excelParser.toLatex(table);
latexOutput[id] = latex;
// I apologize for the hack :(
if(id === '1') {
$('#latex-output').val(latex);
}
});
});
});
},
handleFile: function(event) {
handleFiles: function(event) {
// prevent default browser behavior
event.stopPropagation();
event.preventDefault();
@ -119,16 +159,32 @@ excelParser = {
// get the string table
var stFile = $.grep(entries, function(n,i) {
return /^xl\/sharedStrings\.xml$/.test(n.filename);
var regexp = /^xl\/sharedStrings\.xml$/;
return regexp.test(n.filename);
})[0];
stFile.getData(new zip.TextWriter(), function(text) {
var stringTable = excelParser.parseStringTable(text);
excelParser.handleSheet(entries, stringTable);
excelParser.handleSheets(entries, stringTable);
});
return undefined;
});
});
return undefined;
},
updateSelect: function() {
var select = $('#workbook');
// clear existing option tags
select.empty();
for(var id in workbooks) {
var tag = $('<option value="' + id + '">' + workbooks[id] + '</option>');
tag.appendTo(select);
}
},
init: function() {
@ -141,7 +197,14 @@ excelParser = {
event.dataTransfer.dropEffect = 'copy';
});
$('body').bind('drop', excelParser.handleFile);
$('body').bind('drop', excelParser.handleFiles);
// when a new workbook is selected, do stuff!
$('#workbook').change(function(event) {
var select = $(event.target);
var output = latexOutput[select.val()];
$('#latex-output').val(output);
});
}
};
@ -149,3 +212,4 @@ $(function() {
zip.workerScriptsPath = 'zip/';
excelParser.init();
});
})(window.jQuery);

View File

@ -25,6 +25,9 @@
<h1 class="title">excel => LaTeX</h1>
<h4 class="info">Drag any .xlsx file onto the page to extract data and convert it into a LaTeX table</h4>
<div class="content">
<label for="workbook">Workbook:</label>
<select id="workbook" name="workbook"></select>
<textarea id="latex-output" readonly="readonly"></textarea>
<p>Mostly untested, so if you find a bug or have a feature request, <a href="https://github.com/eric-wood/excel2latex/issues">let me know!</a>
<p class="disclaimer">Note: this only works with .xlsx files. That means .xls files will <b>not</b> work.</p>
@ -32,7 +35,7 @@
<p>Lovingly hacked together by <a href="http://ericwood.org">Eric Wood</a></p>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="zip/zip.js"></script>
<script type="text/javascript" src="converter.js"></script>
</body>