-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yuhuanzhu
committed
Mar 28, 2016
1 parent
6e72ba6
commit 4fe83fa
Showing
10 changed files
with
815 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.