Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhuanzhu committed Mar 28, 2016
1 parent 6e72ba6 commit 4fe83fa
Show file tree
Hide file tree
Showing 10 changed files with 815 additions and 0 deletions.
402 changes: 402 additions & 0 deletions dist/tableExport.js

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "table-export",
"version": "0.0.1",
"description": "table export",
"main": "src/index.js",
"scripts": {
"build": "browserify ./src/index.js > ./dist/tableExport.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/huanz/tableExport.git"
},
"keywords": [
"export"
],
"author": "bukas",
"license": "ISC",
"bugs": {
"url": "https://github.com/huanz/tableExport/issues"
},
"homepage": "https://github.com/huanz/tableExport#readme",
"devDependencies": {
"browserify": "^13.0.0"
}
}
27 changes: 27 additions & 0 deletions src/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var utils = require('./utils');

var fixCSVField = function(value) {
var fixedValue = value;
var addQuotes = (value.indexOf(',') !== -1) || (value.indexOf('\r') !== -1) || (value.indexOf('\n') !== -1);
var replaceDoubleQuotes = (value.indexOf('"') !== -1);

if (replaceDoubleQuotes) {
fixedValue = fixedValue.replace(/"/g, '""');
}
if (addQuotes || replaceDoubleQuotes) {
fixedValue = '"' + fixedValue + '"';
}
return fixedValue;
};


module.exports = function (table) {
var data = '\ufeff';
for (var i = 0, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
data = data + (j ? ',' : '') + fixCSVField(utils.getText(col));
}
data = data + '\r\n';
}
return data;
}
Empty file added src/image.js
Empty file.
35 changes: 35 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var saveAs = require('./saveAs');
var toCsv = require('./csv');
var toJSON = require('./json');
var toOffice = require('./office');

module.exports = global.tableExport = function (tableId, filename, type) {
var doc = document;
var table = doc.getElementById(tableId);
var charset = doc.characterSet;
var uri = {
json: 'application/json;charset=' + charset,
txt: 'csv/txt;charset=' + charset,
csv: 'csv/txt;charset=' + charset,
doc: 'application/msword',
xls: 'application/vnd.ms-excel',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
pdf: 'application/pdf'
};
var typeMap = {
json: toJSON,
txt: toCsv,
csv: toCsv,
doc: toOffice,
xls: toOffice
};
var data = typeMap[type](table, charset, type);
if (data) {
saveAs(new Blob([data], {
type: uri[type]
}), filename + '.' + type);
} else {
throw new Error('the supported types are: json, txt, csv, doc, xls');
}
};
26 changes: 26 additions & 0 deletions src/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var utils = require('./utils');
module.exports = function (table) {
var jsonHeaderArray = [];
var jsonArray = [];
if (table.tHead) {
for (var i = 0, col; col = table.tHead.rows[0].cells[i]; i++) {
jsonHeaderArray.push(utils.getText(col));
}
}
if (table.tBodies) {
for (var j = 0, tbody; tbody = table.tBodies[j]; j++) {
for (var k = 0, rowb; rowb = tbody.rows[k]; k++) {
var len = jsonArray.length;
jsonArray[len] = [];
for (var g = 0, colb; colb = rowb.cells[g]; g++) {
jsonArray[len].push(utils.getText(colb));
}
}
}
}

return JSON.stringify({
header: jsonHeaderArray,
data: jsonArray
});
}
31 changes: 31 additions & 0 deletions src/office.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var utils = require('./utils');
module.exports = function (table, charset, type) {
var tmpl = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:' + type + '" xmlns="http:https://www.w3.org/TR/REC-html40">';
tmpl += '<head><meta charset="' + charset + '" /><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>';
tmpl += '表格1</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->';
tmpl += '</head><body><table>{{table}}</table></body></html>';

var office = '';
var maph = [
['<thead><tr>', '</tr></thead>'],
['<tbody><tr>', '</tr></tbody>'],
['<tr>', '</tr>']
];
var mapb = [
['<th>', '</th>'],
['<td>', '</td>']
];
var flag = +!table.tHead;
var com = 1 - flag;

for (var i = 0, row; row = table.rows[i]; i++) {
flag = i > com ? 2 : flag;
office += maph[flag][0];
for (var j = 0, col; col = row.cells[j]; j++) {
office += mapb[+!!flag][0] + utils.getText(col) + mapb[+!!flag][1];
}
office += maph[flag][1];
flag++;
}
return tmpl.replace('{{table}}', office);
}
Empty file added src/pdf.js
Empty file.
Loading

0 comments on commit 4fe83fa

Please sign in to comment.