You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tableExport.js 98 KiB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. /**
  2. * @preserve tableExport.jquery.plugin
  3. *
  4. * Version 1.10.11
  5. *
  6. * Copyright (c) 2015-2019 hhurz, https://github.com/hhurz
  7. *
  8. * Original Work Copyright (c) 2014 Giri Raj
  9. *
  10. * Licensed under the MIT License
  11. **/
  12. 'use strict';
  13. (function ($) {
  14. $.fn.tableExport = function (options) {
  15. var defaults = {
  16. csvEnclosure: '"',
  17. csvSeparator: ',',
  18. csvUseBOM: true,
  19. date: {
  20. html: 'dd/mm/yyyy' // Date format in html source. Supported placeholders: dd, mm, yy, yyyy and a arbitrary single separator character
  21. },
  22. displayTableName: false, // Deprecated
  23. escape: false, // Deprecated
  24. exportHiddenCells: false, // true = speed up export of large tables with hidden cells (hidden cells will be exported !)
  25. fileName: 'tableExport',
  26. htmlContent: false,
  27. htmlHyperlink: 'content', // Export the cell 'content' or the 'href' link of an <a> tag. Will be ignored if onCellHtmlHyperlink is defined
  28. ignoreColumn: [],
  29. ignoreRow: [],
  30. jsonScope: 'all', // One of 'head', 'data', 'all'
  31. jspdf: { // jsPDF / jsPDF-AutoTable related options
  32. orientation: 'p',
  33. unit: 'pt',
  34. format: 'a4', // One of jsPDF page formats or 'bestfit' for automatic paper format selection
  35. margins: {left: 20, right: 10, top: 10, bottom: 10},
  36. onDocCreated: null,
  37. autotable: {
  38. styles: {
  39. cellPadding: 2,
  40. rowHeight: 12,
  41. fontSize: 8,
  42. fillColor: 255, // Color value or 'inherit' to use css background-color from html table
  43. textColor: 50, // Color value or 'inherit' to use css color from html table
  44. fontStyle: 'normal', // 'normal', 'bold', 'italic', 'bolditalic' or 'inherit' to use css font-weight and font-style from html table
  45. overflow: 'ellipsize', // 'visible', 'hidden', 'ellipsize' or 'linebreak'
  46. halign: 'inherit', // 'left', 'center', 'right' or 'inherit' to use css horizontal cell alignment from html table
  47. valign: 'middle' // 'top', 'middle', or 'bottom'
  48. },
  49. headerStyles: {
  50. fillColor: [52, 73, 94],
  51. textColor: 255,
  52. fontStyle: 'bold',
  53. halign: 'inherit', // 'left', 'center', 'right' or 'inherit' to use css horizontal header cell alignment from html table
  54. valign: 'middle' // 'top', 'middle', or 'bottom'
  55. },
  56. alternateRowStyles: {
  57. fillColor: 245
  58. },
  59. tableExport: {
  60. doc: null, // jsPDF doc object. If set, an already created doc object will be used to export to
  61. onAfterAutotable: null,
  62. onBeforeAutotable: null,
  63. onAutotableText: null,
  64. onTable: null,
  65. outputImages: true
  66. }
  67. }
  68. },
  69. mso: { // MS Excel and MS Word related options
  70. fileFormat: 'xlshtml', // 'xlshtml' = Excel 2000 html format
  71. // 'xmlss' = XML Spreadsheet 2003 file format (XMLSS)
  72. // 'xlsx' = Excel 2007 Office Open XML format
  73. onMsoNumberFormat: null, // Excel 2000 html format only. See readme.md for more information about msonumberformat
  74. pageFormat: 'a4', // Page format used for page orientation
  75. pageOrientation: 'portrait', // portrait, landscape (xlshtml format only)
  76. rtl: false, // true = Set worksheet option 'DisplayRightToLeft'
  77. styles: [], // E.g. ['border-bottom', 'border-top', 'border-left', 'border-right']
  78. worksheetName: '',
  79. xslx: { // Specific Excel 2007 XML format settings:
  80. formatId: { // XLSX format (id) used to format excel cells. See readme.md: data-tableexport-xlsxformatid
  81. date: 14, // formatId or format string (e.g. 'm/d/yy') or function(cell, row, col) {return formatId}
  82. numbers: 2 // formatId or format string (e.g. '\"T\"\ #0.00') or function(cell, row, col) {return formatId}
  83. }
  84. }
  85. },
  86. numbers: {
  87. html: {
  88. decimalMark: '.', // Decimal mark in html source
  89. thousandsSeparator: ',' // Thousands separator in html source
  90. },
  91. output: { // Set 'output: false' to keep number format of html source in resulting output
  92. decimalMark: '.', // Decimal mark in resulting output
  93. thousandsSeparator: ',' // Thousands separator in resulting output
  94. }
  95. },
  96. onAfterSaveToFile: null, // function(data, fileName)
  97. onBeforeSaveToFile: null, // saveIt = function(data, fileName, type, charset, encoding): Return false to abort save process
  98. onCellData: null, // cellText = function($cell, row, col, href, cellText, cellType)
  99. onCellHtmlData: null, // cellText = function($cell, row, col, htmlContent)
  100. onCellHtmlHyperlink: null, // cellText = function($cell, row, col, href, cellText)
  101. onIgnoreRow: null, // ignoreRow = function($tr, row): Return true to prevent export of the row
  102. outputMode: 'file', // 'file', 'string', 'base64' or 'window' (experimental)
  103. pdfmake: {
  104. enabled: false, // true: use pdfmake instead of jspdf and jspdf-autotable (experimental)
  105. docDefinition: {
  106. pageOrientation: 'portrait', // 'portrait' or 'landscape'
  107. defaultStyle: {
  108. font: 'Roboto' // Default is 'Roboto', for an arabic font set this option to 'Mirza' and include mirza_fonts.js
  109. }
  110. },
  111. fonts: {}
  112. },
  113. preserve: {
  114. leadingWS: false, // preserve leading white spaces
  115. trailingWS: false // preserve trailing white spaces
  116. },
  117. preventInjection: true, // Prepend a single quote to cell strings that start with =,+,- or @ to prevent formula injection
  118. sql: {
  119. tableEnclosure: '`', // If table or column names contain any characters except letters, numbers, and
  120. columnEnclosure: '`' // underscores usually the name must be delimited by enclosing it in back quotes (`)
  121. },
  122. tbodySelector: 'tr',
  123. tfootSelector: 'tr', // Set empty ('') to prevent export of tfoot rows
  124. theadSelector: 'tr',
  125. tableName: 'Table',
  126. type: 'csv' // Export format: 'csv', 'tsv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png' or 'pdf'
  127. };
  128. var pageFormats = { // Size in pt of various paper formats. Adopted from jsPDF.
  129. 'a0': [2383.94, 3370.39], 'a1': [1683.78, 2383.94], 'a2': [1190.55, 1683.78],
  130. 'a3': [841.89, 1190.55], 'a4': [595.28, 841.89], 'a5': [419.53, 595.28],
  131. 'a6': [297.64, 419.53], 'a7': [209.76, 297.64], 'a8': [147.40, 209.76],
  132. 'a9': [104.88, 147.40], 'a10': [73.70, 104.88],
  133. 'b0': [2834.65, 4008.19], 'b1': [2004.09, 2834.65], 'b2': [1417.32, 2004.09],
  134. 'b3': [1000.63, 1417.32], 'b4': [708.66, 1000.63], 'b5': [498.90, 708.66],
  135. 'b6': [354.33, 498.90], 'b7': [249.45, 354.33], 'b8': [175.75, 249.45],
  136. 'b9': [124.72, 175.75], 'b10': [87.87, 124.72],
  137. 'c0': [2599.37, 3676.54],
  138. 'c1': [1836.85, 2599.37], 'c2': [1298.27, 1836.85], 'c3': [918.43, 1298.27],
  139. 'c4': [649.13, 918.43], 'c5': [459.21, 649.13], 'c6': [323.15, 459.21],
  140. 'c7': [229.61, 323.15], 'c8': [161.57, 229.61], 'c9': [113.39, 161.57],
  141. 'c10': [79.37, 113.39],
  142. 'dl': [311.81, 623.62],
  143. 'letter': [612, 792], 'government-letter': [576, 756], 'legal': [612, 1008],
  144. 'junior-legal': [576, 360], 'ledger': [1224, 792], 'tabloid': [792, 1224],
  145. 'credit-card': [153, 243]
  146. };
  147. var FONT_ROW_RATIO = 1.15;
  148. var el = this;
  149. var DownloadEvt = null;
  150. var $hrows = [];
  151. var $rows = [];
  152. var rowIndex = 0;
  153. var trData = '';
  154. var colNames = [];
  155. var ranges = [];
  156. var blob;
  157. var $hiddenTableElements = [];
  158. var checkCellVisibilty = false;
  159. $.extend(true, defaults, options);
  160. // Adopt deprecated options
  161. if (defaults.type === 'xlsx') {
  162. defaults.mso.fileFormat = defaults.type;
  163. defaults.type = 'excel';
  164. }
  165. if (typeof defaults.excelFileFormat !== 'undefined' && defaults.mso.fileFormat === 'undefined')
  166. defaults.mso.fileFormat = defaults.excelFileFormat;
  167. if (typeof defaults.excelPageFormat !== 'undefined' && defaults.mso.pageFormat === 'undefined')
  168. defaults.mso.pageFormat = defaults.excelPageFormat;
  169. if (typeof defaults.excelPageOrientation !== 'undefined' && defaults.mso.pageOrientation === 'undefined')
  170. defaults.mso.pageOrientation = defaults.excelPageOrientation;
  171. if (typeof defaults.excelRTL !== 'undefined' && defaults.mso.rtl === 'undefined')
  172. defaults.mso.rtl = defaults.excelRTL;
  173. if (typeof defaults.excelstyles !== 'undefined' && defaults.mso.styles === 'undefined')
  174. defaults.mso.styles = defaults.excelstyles;
  175. if (typeof defaults.onMsoNumberFormat !== 'undefined' && defaults.mso.onMsoNumberFormat === 'undefined')
  176. defaults.mso.onMsoNumberFormat = defaults.onMsoNumberFormat;
  177. if (typeof defaults.worksheetName !== 'undefined' && defaults.mso.worksheetName === 'undefined')
  178. defaults.mso.worksheetName = defaults.worksheetName;
  179. // Check values of some options
  180. defaults.mso.pageOrientation = (defaults.mso.pageOrientation.substr(0, 1) === 'l') ? 'landscape' : 'portrait';
  181. defaults.date.html = defaults.date.html || '';
  182. if ( defaults.date.html.length ) {
  183. var patt = [];
  184. patt['dd'] = '(3[01]|[12][0-9]|0?[1-9])';
  185. patt['mm'] = '(1[012]|0?[1-9])';
  186. patt['yyyy'] = '((?:1[6-9]|2[0-2])\\d{2})';
  187. patt['yy'] = '(\\d{2})';
  188. var separator = defaults.date.html.match(/[^a-zA-Z0-9]/)[0];
  189. var formatItems = defaults.date.html.toLowerCase().split(separator);
  190. defaults.date.regex = '^\\s*';
  191. defaults.date.regex += patt[formatItems[0]];
  192. defaults.date.regex += '(.)'; // separator group
  193. defaults.date.regex += patt[formatItems[1]];
  194. defaults.date.regex += '\\2'; // identical separator group
  195. defaults.date.regex += patt[formatItems[2]];
  196. defaults.date.regex += '\\s*$';
  197. // e.g. '^\\s*(3[01]|[12][0-9]|0?[1-9])(.)(1[012]|0?[1-9])\\2((?:1[6-9]|2[0-2])\\d{2})\\s*$'
  198. defaults.date.pattern = new RegExp(defaults.date.regex, 'g');
  199. var f = formatItems.indexOf("dd")+1;
  200. defaults.date.match_d = f + (f > 1 ? 1 : 0);
  201. f = formatItems.indexOf("mm")+1;
  202. defaults.date.match_m = f + (f > 1 ? 1 : 0);
  203. f = (formatItems.indexOf("yyyy") >= 0 ? formatItems.indexOf("yyyy") : formatItems.indexOf("yy"))+1;
  204. defaults.date.match_y = f + (f > 1 ? 1 : 0);
  205. }
  206. colNames = GetColumnNames(el);
  207. if ( defaults.type === 'csv' || defaults.type === 'tsv' || defaults.type === 'txt' ) {
  208. var csvData = "";
  209. var rowlength = 0;
  210. ranges = [];
  211. rowIndex = 0;
  212. var csvString = function (cell, rowIndex, colIndex) {
  213. var result = '';
  214. if ( cell !== null ) {
  215. var dataString = parseString(cell, rowIndex, colIndex);
  216. var csvValue = (dataString === null || dataString === '') ? '' : dataString.toString();
  217. if ( defaults.type === 'tsv' ) {
  218. if ( dataString instanceof Date )
  219. dataString.toLocaleString();
  220. // According to http://www.iana.org/assignments/media-types/text/tab-separated-values
  221. // are fields that contain tabs not allowable in tsv encoding
  222. result = replaceAll(csvValue, '\t', ' ');
  223. }
  224. else {
  225. // Takes a string and encapsulates it (by default in double-quotes) if it
  226. // contains the csv field separator, spaces, or linebreaks.
  227. if ( dataString instanceof Date )
  228. result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
  229. else {
  230. result = preventInjection(csvValue);
  231. result = replaceAll(result, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);
  232. if ( result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result) )
  233. result = defaults.csvEnclosure + result + defaults.csvEnclosure;
  234. }
  235. }
  236. }
  237. return result;
  238. };
  239. var CollectCsvData = function ($rows, rowselector, length) {
  240. $rows.each(function () {
  241. trData = "";
  242. ForEachVisibleCell(this, rowselector, rowIndex, length + $rows.length,
  243. function (cell, row, col) {
  244. trData += csvString(cell, row, col) + (defaults.type === 'tsv' ? '\t' : defaults.csvSeparator);
  245. });
  246. trData = $.trim(trData).substring(0, trData.length - 1);
  247. if ( trData.length > 0 ) {
  248. if ( csvData.length > 0 )
  249. csvData += "\n";
  250. csvData += trData;
  251. }
  252. rowIndex++;
  253. });
  254. return $rows.length;
  255. };
  256. rowlength += CollectCsvData($(el).find('thead').first().find(defaults.theadSelector), 'th,td', rowlength);
  257. findTableElements($(el),'tbody').each(function () {
  258. rowlength += CollectCsvData(findTableElements($(this), defaults.tbodySelector), 'td,th', rowlength);
  259. });
  260. if ( defaults.tfootSelector.length )
  261. CollectCsvData($(el).find('tfoot').first().find(defaults.tfootSelector), 'td,th', rowlength);
  262. csvData += "\n";
  263. //output
  264. if ( defaults.outputMode === 'string' )
  265. return csvData;
  266. if ( defaults.outputMode === 'base64' )
  267. return base64encode(csvData);
  268. if ( defaults.outputMode === 'window' ) {
  269. downloadFile(false, 'data:text/' + (defaults.type === 'csv' ? 'csv' : 'plain') + ';charset=utf-8,', csvData);
  270. return;
  271. }
  272. saveToFile ( csvData,
  273. defaults.fileName + '.' + defaults.type,
  274. "text/" + (defaults.type === 'csv' ? 'csv' : 'plain'),
  275. "utf-8",
  276. "",
  277. (defaults.type === 'csv' && defaults.csvUseBOM) );
  278. } else if ( defaults.type === 'sql' ) {
  279. // Header
  280. rowIndex = 0;
  281. ranges = [];
  282. var tdData = "INSERT INTO " + defaults.sql.tableEnclosure + defaults.tableName + defaults.sql.tableEnclosure + " (";
  283. $hrows = collectHeadRows ($(el));
  284. $($hrows).each(function () {
  285. ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
  286. function (cell, row, col) {
  287. tdData += defaults.sql.columnEnclosure + parseString(cell, row, col) + defaults.sql.columnEnclosure + ",";
  288. });
  289. rowIndex++;
  290. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  291. });
  292. tdData += ") VALUES ";
  293. // Data
  294. $rows = collectRows ($(el));
  295. $($rows).each(function () {
  296. trData = "";
  297. ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
  298. function (cell, row, col) {
  299. trData += "'" + parseString(cell, row, col) + "',";
  300. });
  301. if ( trData.length > 3 ) {
  302. tdData += "(" + trData;
  303. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  304. tdData += "),";
  305. }
  306. rowIndex++;
  307. });
  308. tdData = $.trim(tdData).substring(0, tdData.length - 1);
  309. tdData += ";";
  310. // Output
  311. if ( defaults.outputMode === 'string' )
  312. return tdData;
  313. if ( defaults.outputMode === 'base64' )
  314. return base64encode(tdData);
  315. saveToFile ( tdData, defaults.fileName + '.sql', "application/sql", "utf-8", "", false );
  316. } else if ( defaults.type === 'json' ) {
  317. var jsonHeaderArray = [];
  318. ranges = [];
  319. $hrows = collectHeadRows ($(el));
  320. $($hrows).each(function () {
  321. var jsonArrayTd = [];
  322. ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
  323. function (cell, row, col) {
  324. jsonArrayTd.push(parseString(cell, row, col));
  325. });
  326. jsonHeaderArray.push(jsonArrayTd);
  327. });
  328. // Data
  329. var jsonArray = [];
  330. $rows = collectRows ($(el));
  331. $($rows).each(function () {
  332. var jsonObjectTd = {};
  333. var colIndex = 0;
  334. ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
  335. function (cell, row, col) {
  336. if ( jsonHeaderArray.length ) {
  337. jsonObjectTd[jsonHeaderArray[jsonHeaderArray.length - 1][colIndex]] = parseString(cell, row, col);
  338. } else {
  339. jsonObjectTd[colIndex] = parseString(cell, row, col);
  340. }
  341. colIndex++;
  342. });
  343. if ( $.isEmptyObject(jsonObjectTd) === false )
  344. jsonArray.push(jsonObjectTd);
  345. rowIndex++;
  346. });
  347. var sdata = "";
  348. if ( defaults.jsonScope === 'head' )
  349. sdata = JSON.stringify(jsonHeaderArray);
  350. else if ( defaults.jsonScope === 'data' )
  351. sdata = JSON.stringify(jsonArray);
  352. else // all
  353. sdata = JSON.stringify({header: jsonHeaderArray, data: jsonArray});
  354. if ( defaults.outputMode === 'string' )
  355. return sdata;
  356. if ( defaults.outputMode === 'base64' )
  357. return base64encode(sdata);
  358. saveToFile ( sdata, defaults.fileName + '.json', "application/json", "utf-8", "base64", false );
  359. } else if ( defaults.type === 'xml' ) {
  360. rowIndex = 0;
  361. ranges = [];
  362. var xml = '<?xml version="1.0" encoding="utf-8"?>';
  363. xml += '<tabledata><fields>';
  364. // Header
  365. $hrows = collectHeadRows ($(el));
  366. $($hrows).each(function () {
  367. ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
  368. function (cell, row, col) {
  369. xml += "<field>" + parseString(cell, row, col) + "</field>";
  370. });
  371. rowIndex++;
  372. });
  373. xml += '</fields><data>';
  374. // Data
  375. var rowCount = 1;
  376. $rows = collectRows ($(el));
  377. $($rows).each(function () {
  378. var colCount = 1;
  379. trData = "";
  380. ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
  381. function (cell, row, col) {
  382. trData += "<column-" + colCount + ">" + parseString(cell, row, col) + "</column-" + colCount + ">";
  383. colCount++;
  384. });
  385. if ( trData.length > 0 && trData !== "<column-1></column-1>" ) {
  386. xml += '<row id="' + rowCount + '">' + trData + '</row>';
  387. rowCount++;
  388. }
  389. rowIndex++;
  390. });
  391. xml += '</data></tabledata>';
  392. // Output
  393. if ( defaults.outputMode === 'string' )
  394. return xml;
  395. if ( defaults.outputMode === 'base64' )
  396. return base64encode(xml);
  397. saveToFile ( xml, defaults.fileName + '.xml', "application/xml", "utf-8", "base64", false );
  398. }
  399. else if ( defaults.type === 'excel' && defaults.mso.fileFormat === 'xmlss' ) {
  400. var docDatas = [];
  401. var docNames = [];
  402. $(el).filter(function () {
  403. return isVisible($(this));
  404. }).each(function () {
  405. var $table = $(this);
  406. var ssName = '';
  407. if ( typeof defaults.mso.worksheetName === 'string' && defaults.mso.worksheetName.length )
  408. ssName = defaults.mso.worksheetName + ' ' + (docNames.length + 1);
  409. else if ( typeof defaults.mso.worksheetName[docNames.length] !== 'undefined' )
  410. ssName = defaults.mso.worksheetName[docNames.length];
  411. if ( ! ssName.length )
  412. ssName = $table.find('caption').text() || '';
  413. if ( ! ssName.length )
  414. ssName = 'Table ' + (docNames.length + 1);
  415. ssName = $.trim(ssName.replace(/[\\\/[\]*:?'"]/g,'').substring(0,31));
  416. docNames.push($('<div />').text(ssName).html());
  417. if ( defaults.exportHiddenCells === false ) {
  418. $hiddenTableElements = $table.find("tr, th, td").filter(":hidden");
  419. checkCellVisibilty = $hiddenTableElements.length > 0;
  420. }
  421. rowIndex = 0;
  422. colNames = GetColumnNames(this);
  423. docData = '<Table>\r';
  424. function CollectXmlssData ($rows, rowselector, length) {
  425. var spans = [];
  426. $($rows).each(function () {
  427. var ssIndex = 0;
  428. var nCols = 0;
  429. trData = "";
  430. ForEachVisibleCell(this, 'td,th', rowIndex, length + $rows.length,
  431. function (cell, row, col) {
  432. if ( cell !== null ) {
  433. var style = "";
  434. var data = parseString(cell, row, col);
  435. var type = "String";
  436. if ( jQuery.isNumeric(data) !== false ) {
  437. type = "Number";
  438. }
  439. else {
  440. var number = parsePercent(data);
  441. if ( number !== false ) {
  442. data = number;
  443. type = "Number";
  444. style += ' ss:StyleID="pct1"';
  445. }
  446. }
  447. if ( type !== "Number" )
  448. data = data.replace(/\n/g, '<br>');
  449. var colspan = getColspan (cell);
  450. var rowspan = getRowspan (cell);
  451. // Skip spans
  452. $.each(spans, function () {
  453. var range = this;
  454. if ( rowIndex >= range.s.r && rowIndex <= range.e.r && nCols >= range.s.c && nCols <= range.e.c ) {
  455. for ( var i = 0; i <= range.e.c - range.s.c; ++i ) {
  456. nCols++;
  457. ssIndex++;
  458. }
  459. }
  460. });
  461. // Handle Row Span
  462. if ( rowspan || colspan ) {
  463. rowspan = rowspan || 1;
  464. colspan = colspan || 1;
  465. spans.push({
  466. s: {r: rowIndex, c: nCols},
  467. e: {r: rowIndex + rowspan - 1, c: nCols + colspan - 1}
  468. });
  469. }
  470. // Handle Colspan
  471. if ( colspan > 1 ) {
  472. style += ' ss:MergeAcross="' + (colspan-1) + '"';
  473. nCols += (colspan - 1);
  474. }
  475. if ( rowspan > 1 ) {
  476. style += ' ss:MergeDown="' + (rowspan-1) + '" ss:StyleID="rsp1"';
  477. }
  478. if ( ssIndex > 0 ) {
  479. style += ' ss:Index="' + (nCols+1) + '"';
  480. ssIndex = 0;
  481. }
  482. trData += '<Cell' + style + '><Data ss:Type="' + type + '">' +
  483. $('<div />').text(data).html() +
  484. '</Data></Cell>\r';
  485. nCols++;
  486. }
  487. });
  488. if ( trData.length > 0 )
  489. docData += '<Row ss:AutoFitHeight="0">\r' + trData + '</Row>\r';
  490. rowIndex++;
  491. });
  492. return $rows.length;
  493. }
  494. var rowLength = CollectXmlssData (collectHeadRows ($table), 'th,td', 0);
  495. CollectXmlssData (collectRows ($table), 'td,th', rowLength);
  496. docData += '</Table>\r';
  497. docDatas.push(docData);
  498. });
  499. var count = {};
  500. var firstOccurences = {};
  501. var item, itemCount;
  502. for (var n = 0, c = docNames.length; n < c; n++)
  503. {
  504. item = docNames[n];
  505. itemCount = count[item];
  506. itemCount = count[item] = (itemCount == null ? 1 : itemCount + 1);
  507. if( itemCount === 2 )
  508. docNames[firstOccurences[item]] = docNames[firstOccurences[item]].substring(0,29) + "-1";
  509. if( count[ item ] > 1 )
  510. docNames[n] = docNames[n].substring(0,29) + "-" + count[item];
  511. else
  512. firstOccurences[item] = n;
  513. }
  514. var CreationDate = new Date().toISOString();
  515. var xmlssDocFile = '<?xml version="1.0" encoding="UTF-8"?>\r' +
  516. '<?mso-application progid="Excel.Sheet"?>\r' +
  517. '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\r' +
  518. ' xmlns:o="urn:schemas-microsoft-com:office:office"\r' +
  519. ' xmlns:x="urn:schemas-microsoft-com:office:excel"\r' +
  520. ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\r' +
  521. ' xmlns:html="http://www.w3.org/TR/REC-html40">\r' +
  522. '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">\r' +
  523. ' <Created>' + CreationDate + '</Created>\r' +
  524. '</DocumentProperties>\r' +
  525. '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">\r' +
  526. ' <AllowPNG/>\r' +
  527. '</OfficeDocumentSettings>\r' +
  528. '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">\r' +
  529. ' <WindowHeight>9000</WindowHeight>\r' +
  530. ' <WindowWidth>13860</WindowWidth>\r' +
  531. ' <WindowTopX>0</WindowTopX>\r' +
  532. ' <WindowTopY>0</WindowTopY>\r' +
  533. ' <ProtectStructure>False</ProtectStructure>\r' +
  534. ' <ProtectWindows>False</ProtectWindows>\r' +
  535. '</ExcelWorkbook>\r' +
  536. '<Styles>\r' +
  537. ' <Style ss:ID="Default" ss:Name="Normal">\r' +
  538. ' <Alignment ss:Vertical="Bottom"/>\r' +
  539. ' <Borders/>\r' +
  540. ' <Font/>\r' +
  541. ' <Interior/>\r' +
  542. ' <NumberFormat/>\r' +
  543. ' <Protection/>\r' +
  544. ' </Style>\r' +
  545. ' <Style ss:ID="rsp1">\r' +
  546. ' <Alignment ss:Vertical="Center"/>\r' +
  547. ' </Style>\r' +
  548. ' <Style ss:ID="pct1">\r' +
  549. ' <NumberFormat ss:Format="Percent"/>\r' +
  550. ' </Style>\r' +
  551. '</Styles>\r';
  552. for ( var j = 0; j < docDatas.length; j++ ) {
  553. xmlssDocFile += '<Worksheet ss:Name="' + docNames[j] + '" ss:RightToLeft="' + (defaults.mso.rtl ? '1' : '0') + '">\r' +
  554. docDatas[j];
  555. if (defaults.mso.rtl) {
  556. xmlssDocFile += '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">\r' +
  557. '<DisplayRightToLeft/>\r' +
  558. '</WorksheetOptions>\r';
  559. }
  560. else
  561. xmlssDocFile += '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"/>\r';
  562. xmlssDocFile += '</Worksheet>\r';
  563. }
  564. xmlssDocFile += '</Workbook>\r';
  565. if ( defaults.outputMode === 'string' )
  566. return xmlssDocFile;
  567. if ( defaults.outputMode === 'base64' )
  568. return base64encode(xmlssDocFile);
  569. saveToFile ( xmlssDocFile, defaults.fileName + '.xml', "application/xml", "utf-8", "base64", false );
  570. }
  571. else if ( defaults.type === 'excel' && defaults.mso.fileFormat === 'xlsx' ) {
  572. var sheetNames = [];
  573. var workbook = XLSX.utils.book_new();
  574. // Multiple worksheets and .xlsx file extension #202
  575. $(el).filter(function () {
  576. return isVisible($(this));
  577. }).each(function () {
  578. var $table = $(this);
  579. var ws = xlsxTableToSheet(this);
  580. var sheetName = '';
  581. if ( typeof defaults.mso.worksheetName === 'string' && defaults.mso.worksheetName.length )
  582. sheetName = defaults.mso.worksheetName + ' ' + (sheetNames.length + 1);
  583. else if ( typeof defaults.mso.worksheetName[sheetNames.length] !== 'undefined' )
  584. sheetName = defaults.mso.worksheetName[sheetNames.length];
  585. if ( ! sheetName.length )
  586. sheetName = $table.find('caption').text() || '';
  587. if ( ! sheetName.length )
  588. sheetName = 'Table ' + (sheetNames.length + 1);
  589. sheetName = $.trim(sheetName.replace(/[\\\/[\]*:?'"]/g,'').substring(0,31));
  590. sheetNames.push(sheetName);
  591. XLSX.utils.book_append_sheet(workbook, ws, sheetName);
  592. });
  593. // add worksheet to workbook
  594. var wbout = XLSX.write(workbook, {type: 'binary', bookType: defaults.mso.fileFormat, bookSST: false});
  595. saveToFile ( xlsxWorkbookToArrayBuffer(wbout),
  596. defaults.fileName + '.' + defaults.mso.fileFormat,
  597. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  598. "UTF-8", "", false );
  599. }
  600. else if ( defaults.type === 'excel' || defaults.type === 'xls' || defaults.type === 'word' || defaults.type === 'doc' ) {
  601. var MSDocType = (defaults.type === 'excel' || defaults.type === 'xls') ? 'excel' : 'word';
  602. var MSDocExt = (MSDocType === 'excel') ? 'xls' : 'doc';
  603. var MSDocSchema = 'xmlns:x="urn:schemas-microsoft-com:office:' + MSDocType + '"';
  604. var docData = '';
  605. var docName = '';
  606. $(el).filter(function () {
  607. return isVisible($(this));
  608. }).each(function () {
  609. var $table = $(this);
  610. if (docName === '') {
  611. docName = defaults.mso.worksheetName || $table.find('caption').text() || 'Table';
  612. docName = $.trim(docName.replace(/[\\\/[\]*:?'"]/g, '').substring(0, 31));
  613. }
  614. if ( defaults.exportHiddenCells === false ) {
  615. $hiddenTableElements = $table.find("tr, th, td").filter(":hidden");
  616. checkCellVisibilty = $hiddenTableElements.length > 0;
  617. }
  618. rowIndex = 0;
  619. ranges = [];
  620. colNames = GetColumnNames(this);
  621. // Header
  622. docData += '<table><thead>';
  623. $hrows = collectHeadRows ($table);
  624. $($hrows).each(function () {
  625. var $row = $(this);
  626. trData = "";
  627. ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
  628. function (cell, row, col) {
  629. if ( cell !== null ) {
  630. var thstyle = '';
  631. trData += '<th';
  632. if ( defaults.mso.styles.length ) {
  633. var cellstyles = document.defaultView.getComputedStyle(cell, null);
  634. var rowstyles = document.defaultView.getComputedStyle($row[0], null);
  635. for ( var cssStyle in defaults.mso.styles ) {
  636. var thcss = cellstyles[defaults.mso.styles[cssStyle]];
  637. if ( thcss === '' )
  638. thcss = rowstyles[defaults.mso.styles[cssStyle]];
  639. if ( thcss !== '' && thcss !== '0px none rgb(0, 0, 0)' && thcss !== 'rgba(0, 0, 0, 0)' ) {
  640. thstyle += (thstyle === '') ? 'style="' : ';';
  641. thstyle += defaults.mso.styles[cssStyle] + ':' + thcss;
  642. }
  643. }
  644. }
  645. if ( thstyle !== '' )
  646. trData += ' ' + thstyle + '"';
  647. var tdcolspan = getColspan (cell);
  648. if ( tdcolspan > 0 )
  649. trData += ' colspan="' + tdcolspan + '"';
  650. var tdrowspan = getRowspan (cell);
  651. if ( tdrowspan > 0 )
  652. trData += ' rowspan="' + tdrowspan + '"';
  653. trData += '>' + parseString(cell, row, col) + '</th>';
  654. }
  655. });
  656. if ( trData.length > 0 )
  657. docData += '<tr>' + trData + '</tr>';
  658. rowIndex++;
  659. });
  660. docData += '</thead><tbody>';
  661. // Data
  662. $rows = collectRows ($table);
  663. $($rows).each(function () {
  664. var $row = $(this);
  665. trData = "";
  666. ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
  667. function (cell, row, col) {
  668. if ( cell !== null ) {
  669. var tdvalue = parseString(cell, row, col);
  670. var tdstyle = '';
  671. var tdcss = $(cell).attr("data-tableexport-msonumberformat");
  672. if ( typeof tdcss === 'undefined' && typeof defaults.mso.onMsoNumberFormat === 'function' )
  673. tdcss = defaults.mso.onMsoNumberFormat(cell, row, col);
  674. if ( typeof tdcss !== 'undefined' && tdcss !== '' )
  675. tdstyle = 'style="mso-number-format:\'' + tdcss + '\'';
  676. if ( defaults.mso.styles.length ) {
  677. var cellstyles = document.defaultView.getComputedStyle(cell, null);
  678. var rowstyles = document.defaultView.getComputedStyle($row[0], null);
  679. for ( var cssStyle in defaults.mso.styles ) {
  680. tdcss = cellstyles[defaults.mso.styles[cssStyle]];
  681. if ( tdcss === '' )
  682. tdcss = rowstyles[defaults.mso.styles[cssStyle]];
  683. if ( tdcss !== '' && tdcss !== '0px none rgb(0, 0, 0)' && tdcss !== 'rgba(0, 0, 0, 0)' ) {
  684. tdstyle += (tdstyle === '') ? 'style="' : ';';
  685. tdstyle += defaults.mso.styles[cssStyle] + ':' + tdcss;
  686. }
  687. }
  688. }
  689. trData += '<td';
  690. if ( tdstyle !== '' )
  691. trData += ' ' + tdstyle + '"';
  692. var tdcolspan = getColspan (cell);
  693. if ( tdcolspan > 0 )
  694. trData += ' colspan="' + tdcolspan + '"';
  695. var tdrowspan = getRowspan (cell);
  696. if ( tdrowspan > 0 )
  697. trData += ' rowspan="' + tdrowspan + '"';
  698. if ( typeof tdvalue === 'string' && tdvalue !== '' ) {
  699. tdvalue = preventInjection(tdvalue);
  700. tdvalue = tdvalue.replace(/\n/g, '<br>');
  701. }
  702. trData += '>' + tdvalue + '</td>';
  703. }
  704. });
  705. if ( trData.length > 0 )
  706. docData += '<tr>' + trData + '</tr>';
  707. rowIndex++;
  708. });
  709. if ( defaults.displayTableName )
  710. docData += '<tr><td></td></tr><tr><td></td></tr><tr><td>' + parseString($('<p>' + defaults.tableName + '</p>')) + '</td></tr>';
  711. docData += '</tbody></table>';
  712. });
  713. //noinspection XmlUnusedNamespaceDeclaration
  714. var docFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' + MSDocSchema + ' xmlns="http://www.w3.org/TR/REC-html40">';
  715. docFile += '<meta http-equiv="content-type" content="application/vnd.ms-' + MSDocType + '; charset=UTF-8">';
  716. docFile += "<head>";
  717. if (MSDocType === 'excel') {
  718. docFile += "<!--[if gte mso 9]>";
  719. docFile += "<xml>";
  720. docFile += "<x:ExcelWorkbook>";
  721. docFile += "<x:ExcelWorksheets>";
  722. docFile += "<x:ExcelWorksheet>";
  723. docFile += "<x:Name>";
  724. docFile += docName;
  725. docFile += "</x:Name>";
  726. docFile += "<x:WorksheetOptions>";
  727. docFile += "<x:DisplayGridlines/>";
  728. if (defaults.mso.rtl)
  729. docFile += "<x:DisplayRightToLeft/>";
  730. docFile += "</x:WorksheetOptions>";
  731. docFile += "</x:ExcelWorksheet>";
  732. docFile += "</x:ExcelWorksheets>";
  733. docFile += "</x:ExcelWorkbook>";
  734. docFile += "</xml>";
  735. docFile += "<![endif]-->";
  736. }
  737. docFile += "<style>";
  738. docFile += "@page { size:" + defaults.mso.pageOrientation + "; mso-page-orientation:" + defaults.mso.pageOrientation + "; }";
  739. docFile += "@page Section1 {size:" + pageFormats[defaults.mso.pageFormat][0] + "pt " + pageFormats[defaults.mso.pageFormat][1] + "pt";
  740. docFile += "; margin:1.0in 1.25in 1.0in 1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}";
  741. docFile += "div.Section1 {page:Section1;}";
  742. docFile += "@page Section2 {size:" + pageFormats[defaults.mso.pageFormat][1] + "pt " + pageFormats[defaults.mso.pageFormat][0] + "pt";
  743. docFile += ";mso-page-orientation:" + defaults.mso.pageOrientation + ";margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}";
  744. docFile += "div.Section2 {page:Section2;}";
  745. docFile += "br {mso-data-placement:same-cell;}";
  746. docFile += "</style>";
  747. docFile += "</head>";
  748. docFile += "<body>";
  749. docFile += "<div class=\"Section" + ((defaults.mso.pageOrientation === 'landscape') ? "2" : "1") + "\">";
  750. docFile += docData;
  751. docFile += "</div>";
  752. docFile += "</body>";
  753. docFile += "</html>";
  754. if ( defaults.outputMode === 'string' )
  755. return docFile;
  756. if ( defaults.outputMode === 'base64' )
  757. return base64encode(docFile);
  758. saveToFile ( docFile, defaults.fileName + '.' + MSDocExt, "application/vnd.ms-" + MSDocType, "", "base64", false );
  759. }
  760. else if ( defaults.type === 'png' ) {
  761. html2canvas($(el)[0]).then(
  762. function (canvas) {
  763. var image = canvas.toDataURL();
  764. var byteString = atob(image.substring(22)); // remove data stuff
  765. var buffer = new ArrayBuffer(byteString.length);
  766. var intArray = new Uint8Array(buffer);
  767. for ( var i = 0; i < byteString.length; i++ )
  768. intArray[i] = byteString.charCodeAt(i);
  769. if ( defaults.outputMode === 'string' )
  770. return byteString;
  771. if ( defaults.outputMode === 'base64' )
  772. return base64encode(image);
  773. if ( defaults.outputMode === 'window' ) {
  774. window.open(image);
  775. return;
  776. }
  777. saveToFile ( buffer, defaults.fileName + '.png', "image/png", "", "", false );
  778. });
  779. } else if ( defaults.type === 'pdf' ) {
  780. if ( defaults.pdfmake.enabled === true ) {
  781. // pdf output using pdfmake
  782. // https://github.com/bpampuch/pdfmake
  783. var widths = [];
  784. var body = [];
  785. rowIndex = 0;
  786. ranges = [];
  787. /**
  788. * @return {number}
  789. */
  790. var CollectPdfmakeData = function ($rows, colselector, length) {
  791. var rlength = 0;
  792. $($rows).each(function () {
  793. var r = [];
  794. ForEachVisibleCell(this, colselector, rowIndex, length,
  795. function (cell, row, col) {
  796. if ( typeof cell !== 'undefined' && cell !== null ) {
  797. var colspan = getColspan (cell);
  798. var rowspan = getRowspan (cell);
  799. var cellValue = parseString(cell, row, col) || " ";
  800. if ( colspan > 1 || rowspan > 1 ) {
  801. colspan = colspan || 1;
  802. rowspan = rowspan || 1;
  803. r.push({colSpan: colspan, rowSpan: rowspan, text: cellValue});
  804. }
  805. else
  806. r.push(cellValue);
  807. }
  808. else
  809. r.push(" ");
  810. });
  811. if ( r.length )
  812. body.push(r);
  813. if ( rlength < r.length )
  814. rlength = r.length;
  815. rowIndex++;
  816. });
  817. return rlength;
  818. };
  819. $hrows = collectHeadRows ($(this));
  820. var colcount = CollectPdfmakeData($hrows, 'th,td', $hrows.length);
  821. for ( var i = widths.length; i < colcount; i++ )
  822. widths.push("*");
  823. // Data
  824. $rows = collectRows ($(this));
  825. CollectPdfmakeData($rows, 'th,td', $hrows.length + $rows.length);
  826. var docDefinition = {
  827. content: [{
  828. table: {
  829. headerRows: $hrows.length,
  830. widths: widths,
  831. body: body
  832. }
  833. }]
  834. };
  835. $.extend(true, docDefinition, defaults.pdfmake.docDefinition);
  836. pdfMake.fonts = {
  837. Roboto: {
  838. normal: 'Roboto-Regular.ttf',
  839. bold: 'Roboto-Medium.ttf',
  840. italics: 'Roboto-Italic.ttf',
  841. bolditalics: 'Roboto-MediumItalic.ttf'
  842. }
  843. };
  844. $.extend(true, pdfMake.fonts, defaults.pdfmake.fonts);
  845. pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
  846. saveToFile ( buffer, defaults.fileName + '.pdf', "application/pdf", "", "", false );
  847. });
  848. }
  849. else if ( defaults.jspdf.autotable === false ) {
  850. // pdf output using jsPDF's core html support
  851. var addHtmlOptions = {
  852. dim: {
  853. w: getPropertyUnitValue($(el).first().get(0), 'width', 'mm'),
  854. h: getPropertyUnitValue($(el).first().get(0), 'height', 'mm')
  855. },
  856. pagesplit: false
  857. };
  858. var doc = new jsPDF(defaults.jspdf.orientation, defaults.jspdf.unit, defaults.jspdf.format);
  859. doc.addHTML($(el).first(),
  860. defaults.jspdf.margins.left,
  861. defaults.jspdf.margins.top,
  862. addHtmlOptions,
  863. function () {
  864. jsPdfOutput(doc, false);
  865. });
  866. //delete doc;
  867. }
  868. else {
  869. // pdf output using jsPDF AutoTable plugin
  870. // https://github.com/simonbengtsson/jsPDF-AutoTable
  871. var teOptions = defaults.jspdf.autotable.tableExport;
  872. // When setting jspdf.format to 'bestfit' tableExport tries to choose
  873. // the minimum required paper format and orientation in which the table
  874. // (or tables in multitable mode) completely fits without column adjustment
  875. if ( typeof defaults.jspdf.format === 'string' && defaults.jspdf.format.toLowerCase() === 'bestfit' ) {
  876. var rk = '', ro = '';
  877. var mw = 0;
  878. $(el).each(function () {
  879. if ( isVisible($(this)) ) {
  880. var w = getPropertyUnitValue($(this).get(0), 'width', 'pt');
  881. if ( w > mw ) {
  882. if ( w > pageFormats.a0[0] ) {
  883. rk = 'a0';
  884. ro = 'l';
  885. }
  886. for ( var key in pageFormats ) {
  887. if ( pageFormats.hasOwnProperty(key) ) {
  888. if ( pageFormats[key][1] > w ) {
  889. rk = key;
  890. ro = 'l';
  891. if ( pageFormats[key][0] > w )
  892. ro = 'p';
  893. }
  894. }
  895. }
  896. mw = w;
  897. }
  898. }
  899. });
  900. defaults.jspdf.format = (rk === '' ? 'a4' : rk);
  901. defaults.jspdf.orientation = (ro === '' ? 'w' : ro);
  902. }
  903. // The jsPDF doc object is stored in defaults.jspdf.autotable.tableExport,
  904. // thus it can be accessed from any callback function
  905. if ( teOptions.doc == null ) {
  906. teOptions.doc = new jsPDF(defaults.jspdf.orientation,
  907. defaults.jspdf.unit,
  908. defaults.jspdf.format);
  909. teOptions.wScaleFactor = 1;
  910. teOptions.hScaleFactor = 1;
  911. if ( typeof defaults.jspdf.onDocCreated === 'function' )
  912. defaults.jspdf.onDocCreated(teOptions.doc);
  913. }
  914. if ( teOptions.outputImages === true )
  915. teOptions.images = {};
  916. if ( typeof teOptions.images !== 'undefined' ) {
  917. $(el).filter(function () {
  918. return isVisible($(this));
  919. }).each(function () {
  920. var rowCount = 0;
  921. ranges = [];
  922. if ( defaults.exportHiddenCells === false ) {
  923. $hiddenTableElements = $(this).find("tr, th, td").filter(":hidden");
  924. checkCellVisibilty = $hiddenTableElements.length > 0;
  925. }
  926. $hrows = collectHeadRows ($(this));
  927. $rows = collectRows ($(this));
  928. $($rows).each(function () {
  929. ForEachVisibleCell(this, 'td,th', $hrows.length + rowCount, $hrows.length + $rows.length,
  930. function (cell) {
  931. collectImages(cell, $(cell).children(), teOptions);
  932. });
  933. rowCount++;
  934. });
  935. });
  936. $hrows = [];
  937. $rows = [];
  938. }
  939. loadImages(teOptions, function () {
  940. $(el).filter(function () {
  941. return isVisible($(this));
  942. }).each(function () {
  943. var colKey;
  944. rowIndex = 0;
  945. ranges = [];
  946. if ( defaults.exportHiddenCells === false ) {
  947. $hiddenTableElements = $(this).find("tr, th, td").filter(":hidden");
  948. checkCellVisibilty = $hiddenTableElements.length > 0;
  949. }
  950. colNames = GetColumnNames(this);
  951. teOptions.columns = [];
  952. teOptions.rows = [];
  953. teOptions.teCells = {};
  954. // onTable: optional callback function for every matching table that can be used
  955. // to modify the tableExport options or to skip the output of a particular table
  956. // if the table selector targets multiple tables
  957. if ( typeof teOptions.onTable === 'function' )
  958. if ( teOptions.onTable($(this), defaults) === false )
  959. return true; // continue to next iteration step (table)
  960. // each table works with an own copy of AutoTable options
  961. defaults.jspdf.autotable.tableExport = null; // avoid deep recursion error
  962. var atOptions = $.extend(true, {}, defaults.jspdf.autotable);
  963. defaults.jspdf.autotable.tableExport = teOptions;
  964. atOptions.margin = {};
  965. $.extend(true, atOptions.margin, defaults.jspdf.margins);
  966. atOptions.tableExport = teOptions;
  967. // Fix jsPDF Autotable's row height calculation
  968. if ( typeof atOptions.beforePageContent !== 'function' ) {
  969. atOptions.beforePageContent = function (data) {
  970. if ( data.pageCount === 1 ) {
  971. var all = data.table.rows.concat(data.table.headerRow);
  972. $.each(all, function () {
  973. var row = this;
  974. if ( row.height > 0 ) {
  975. row.height += (2 - FONT_ROW_RATIO) / 2 * row.styles.fontSize;
  976. data.table.height += (2 - FONT_ROW_RATIO) / 2 * row.styles.fontSize;
  977. }
  978. });
  979. }
  980. };
  981. }
  982. if ( typeof atOptions.createdHeaderCell !== 'function' ) {
  983. // apply some original css styles to pdf header cells
  984. atOptions.createdHeaderCell = function (cell, data) {
  985. // jsPDF AutoTable plugin v2.0.14 fix: each cell needs its own styles object
  986. cell.styles = $.extend({}, data.row.styles);
  987. if ( typeof teOptions.columns [data.column.dataKey] !== 'undefined' ) {
  988. var col = teOptions.columns [data.column.dataKey];
  989. if ( typeof col.rect !== 'undefined' ) {
  990. var rh;
  991. cell.contentWidth = col.rect.width;
  992. if ( typeof teOptions.heightRatio === 'undefined' || teOptions.heightRatio === 0 ) {
  993. if ( data.row.raw [data.column.dataKey].rowspan )
  994. rh = data.row.raw [data.column.dataKey].rect.height / data.row.raw [data.column.dataKey].rowspan;
  995. else
  996. rh = data.row.raw [data.column.dataKey].rect.height;
  997. teOptions.heightRatio = cell.styles.rowHeight / rh;
  998. }
  999. rh = data.row.raw [data.column.dataKey].rect.height * teOptions.heightRatio;
  1000. if ( rh > cell.styles.rowHeight )
  1001. cell.styles.rowHeight = rh;
  1002. }
  1003. cell.styles.halign = (atOptions.headerStyles.halign === 'inherit') ? 'center' : atOptions.headerStyles.halign;
  1004. cell.styles.valign = atOptions.headerStyles.valign;
  1005. if ( typeof col.style !== 'undefined' && col.style.hidden !== true ) {
  1006. if ( atOptions.headerStyles.halign === 'inherit' )
  1007. cell.styles.halign = col.style.align;
  1008. if ( atOptions.styles.fillColor === 'inherit' )
  1009. cell.styles.fillColor = col.style.bcolor;
  1010. if ( atOptions.styles.textColor === 'inherit' )
  1011. cell.styles.textColor = col.style.color;
  1012. if ( atOptions.styles.fontStyle === 'inherit' )
  1013. cell.styles.fontStyle = col.style.fstyle;
  1014. }
  1015. }
  1016. };
  1017. }
  1018. if ( typeof atOptions.createdCell !== 'function' ) {
  1019. // apply some original css styles to pdf table cells
  1020. atOptions.createdCell = function (cell, data) {
  1021. var tecell = teOptions.teCells [data.row.index + ":" + data.column.dataKey];
  1022. cell.styles.halign = (atOptions.styles.halign === 'inherit') ? 'center' : atOptions.styles.halign;
  1023. cell.styles.valign = atOptions.styles.valign;
  1024. if ( typeof tecell !== 'undefined' && typeof tecell.style !== 'undefined' && tecell.style.hidden !== true ) {
  1025. if ( atOptions.styles.halign === 'inherit' )
  1026. cell.styles.halign = tecell.style.align;
  1027. if ( atOptions.styles.fillColor === 'inherit' )
  1028. cell.styles.fillColor = tecell.style.bcolor;
  1029. if ( atOptions.styles.textColor === 'inherit' )
  1030. cell.styles.textColor = tecell.style.color;
  1031. if ( atOptions.styles.fontStyle === 'inherit' )
  1032. cell.styles.fontStyle = tecell.style.fstyle;
  1033. }
  1034. };
  1035. }
  1036. if ( typeof atOptions.drawHeaderCell !== 'function' ) {
  1037. atOptions.drawHeaderCell = function (cell, data) {
  1038. var colopt = teOptions.columns [data.column.dataKey];
  1039. if ( (colopt.style.hasOwnProperty("hidden") !== true || colopt.style.hidden !== true) &&
  1040. colopt.rowIndex >= 0 )
  1041. return prepareAutoTableText(cell, data, colopt);
  1042. else
  1043. return false; // cell is hidden
  1044. };
  1045. }
  1046. if ( typeof atOptions.drawCell !== 'function' ) {
  1047. atOptions.drawCell = function (cell, data) {
  1048. var tecell = teOptions.teCells [data.row.index + ":" + data.column.dataKey];
  1049. var draw2canvas = (typeof tecell !== 'undefined' && tecell.isCanvas);
  1050. if ( draw2canvas !== true ) {
  1051. if ( prepareAutoTableText(cell, data, tecell) ) {
  1052. teOptions.doc.rect(cell.x, cell.y, cell.width, cell.height, cell.styles.fillStyle);
  1053. if ( typeof tecell !== 'undefined' &&
  1054. typeof tecell.elements !== 'undefined' && tecell.elements.length ) {
  1055. var hScale = cell.height / tecell.rect.height;
  1056. if ( hScale > teOptions.hScaleFactor )
  1057. teOptions.hScaleFactor = hScale;
  1058. teOptions.wScaleFactor = cell.width / tecell.rect.width;
  1059. var ySave = cell.textPos.y;
  1060. drawAutotableElements(cell, tecell.elements, teOptions);
  1061. cell.textPos.y = ySave;
  1062. drawAutotableText(cell, tecell.elements, teOptions);
  1063. }
  1064. else
  1065. drawAutotableText(cell, {}, teOptions);
  1066. }
  1067. }
  1068. else {
  1069. var container = tecell.elements[0];
  1070. var imgId = $(container).attr("data-tableexport-canvas");
  1071. var r = container.getBoundingClientRect();
  1072. cell.width = r.width * teOptions.wScaleFactor;
  1073. cell.height = r.height * teOptions.hScaleFactor;
  1074. data.row.height = cell.height;
  1075. jsPdfDrawImage (cell, container, imgId, teOptions);
  1076. }
  1077. return false;
  1078. };
  1079. }
  1080. // collect header and data rows
  1081. teOptions.headerrows = [];
  1082. $hrows = collectHeadRows ($(this));
  1083. $($hrows).each(function () {
  1084. colKey = 0;
  1085. teOptions.headerrows[rowIndex] = [];
  1086. ForEachVisibleCell(this, 'th,td', rowIndex, $hrows.length,
  1087. function (cell, row, col) {
  1088. var obj = getCellStyles(cell);
  1089. obj.title = parseString(cell, row, col);
  1090. obj.key = colKey++;
  1091. obj.rowIndex = rowIndex;
  1092. teOptions.headerrows[rowIndex].push(obj);
  1093. });
  1094. rowIndex++;
  1095. });
  1096. if ( rowIndex > 0 ) {
  1097. // iterate through last row
  1098. var lastrow = rowIndex - 1;
  1099. while ( lastrow >= 0 ) {
  1100. $.each(teOptions.headerrows[lastrow], function () {
  1101. var obj = this;
  1102. if ( lastrow > 0 && this.rect === null )
  1103. obj = teOptions.headerrows[lastrow - 1][this.key];
  1104. if ( obj !== null && obj.rowIndex >= 0 &&
  1105. (obj.style.hasOwnProperty("hidden") !== true || obj.style.hidden !== true) )
  1106. teOptions.columns.push(obj);
  1107. });
  1108. lastrow = (teOptions.columns.length > 0) ? -1 : lastrow - 1;
  1109. }
  1110. }
  1111. var rowCount = 0;
  1112. $rows = [];
  1113. $rows = collectRows ($(this));
  1114. $($rows).each(function () {
  1115. var rowData = [];
  1116. colKey = 0;
  1117. ForEachVisibleCell(this, 'td,th', rowIndex, $hrows.length + $rows.length,
  1118. function (cell, row, col) {
  1119. var obj;
  1120. if ( typeof teOptions.columns[colKey] === 'undefined' ) {
  1121. // jsPDF-Autotable needs columns. Thus define hidden ones for tables without thead
  1122. obj = {
  1123. title: '',
  1124. key: colKey,
  1125. style: {
  1126. hidden: true
  1127. }
  1128. };
  1129. teOptions.columns.push(obj);
  1130. }
  1131. if ( typeof cell !== 'undefined' && cell !== null ) {
  1132. obj = getCellStyles(cell);
  1133. obj.isCanvas = cell.hasAttribute("data-tableexport-canvas");
  1134. obj.elements = obj.isCanvas ? $(cell) : $(cell).children();
  1135. teOptions.teCells [rowCount + ":" + colKey++] = obj;
  1136. }
  1137. else {
  1138. obj = $.extend(true, {}, teOptions.teCells [rowCount + ":" + (colKey - 1)]);
  1139. obj.colspan = -1;
  1140. teOptions.teCells [rowCount + ":" + colKey++] = obj;
  1141. }
  1142. rowData.push(parseString(cell, row, col));
  1143. });
  1144. if ( rowData.length ) {
  1145. teOptions.rows.push(rowData);
  1146. rowCount++;
  1147. }
  1148. rowIndex++;
  1149. });
  1150. // onBeforeAutotable: optional callback function before calling
  1151. // jsPDF AutoTable that can be used to modify the AutoTable options
  1152. if ( typeof teOptions.onBeforeAutotable === 'function' )
  1153. teOptions.onBeforeAutotable($(this), teOptions.columns, teOptions.rows, atOptions);
  1154. teOptions.doc.autoTable(teOptions.columns, teOptions.rows, atOptions);
  1155. // onAfterAutotable: optional callback function after returning
  1156. // from jsPDF AutoTable that can be used to modify the AutoTable options
  1157. if ( typeof teOptions.onAfterAutotable === 'function' )
  1158. teOptions.onAfterAutotable($(this), atOptions);
  1159. // set the start position for the next table (in case there is one)
  1160. defaults.jspdf.autotable.startY = teOptions.doc.autoTableEndPosY() + atOptions.margin.top;
  1161. });
  1162. jsPdfOutput(teOptions.doc, (typeof teOptions.images !== 'undefined' && jQuery.isEmptyObject(teOptions.images) === false));
  1163. if ( typeof teOptions.headerrows !== 'undefined' )
  1164. teOptions.headerrows.length = 0;
  1165. if ( typeof teOptions.columns !== 'undefined' )
  1166. teOptions.columns.length = 0;
  1167. if ( typeof teOptions.rows !== 'undefined' )
  1168. teOptions.rows.length = 0;
  1169. delete teOptions.doc;
  1170. teOptions.doc = null;
  1171. });
  1172. }
  1173. }
  1174. function collectHeadRows ($table) {
  1175. var result = [];
  1176. findTableElements($table,'thead').each(function () {
  1177. result.push.apply(result, findTableElements($(this), defaults.theadSelector).toArray());
  1178. });
  1179. return result;
  1180. }
  1181. function collectRows ($table) {
  1182. var result = [];
  1183. findTableElements($table,'tbody').each(function () {
  1184. result.push.apply(result, findTableElements($(this), defaults.tbodySelector).toArray());
  1185. });
  1186. if ( defaults.tfootSelector.length ) {
  1187. findTableElements($table,'tfoot').each(function () {
  1188. result.push.apply(result, findTableElements($(this), defaults.tfootSelector).toArray());
  1189. });
  1190. }
  1191. return result;
  1192. }
  1193. function findTableElements ($parent, selector) {
  1194. var parentSelector = $parent[0].tagName;
  1195. var parentLevel = $parent.parents(parentSelector).length;
  1196. return $parent.find(selector).filter (function () {
  1197. return parentLevel === $(this).closest(parentSelector).parents(parentSelector).length;
  1198. });
  1199. }
  1200. function GetColumnNames (table) {
  1201. var result = [];
  1202. $(table).find('thead').first().find('th').each(function (index, el) {
  1203. if ( $(el).attr("data-field") !== undefined )
  1204. result[index] = $(el).attr("data-field");
  1205. else
  1206. result[index] = index.toString();
  1207. });
  1208. return result;
  1209. }
  1210. function isVisible ($element) {
  1211. var isRow = typeof $element[0].rowIndex !== 'undefined';
  1212. var isCell = isRow === false && typeof $element[0].cellIndex !== 'undefined';
  1213. var isElementVisible = (isCell || isRow) ? isTableElementVisible($element) : $element.is(':visible');
  1214. var tableexportDisplay = $element.attr("data-tableexport-display");
  1215. if (isCell && tableexportDisplay !== 'none' && tableexportDisplay !== 'always') {
  1216. $element = $($element[0].parentNode);
  1217. isRow = typeof $element[0].rowIndex !== 'undefined';
  1218. tableexportDisplay = $element.attr("data-tableexport-display");
  1219. }
  1220. if (isRow && tableexportDisplay !== 'none' && tableexportDisplay !== 'always') {
  1221. tableexportDisplay = $element.closest('table').attr("data-tableexport-display");
  1222. }
  1223. return tableexportDisplay !== 'none' && (isElementVisible === true || tableexportDisplay === 'always');
  1224. }
  1225. function isTableElementVisible ($element) {
  1226. var hiddenEls = [];
  1227. if ( checkCellVisibilty ) {
  1228. hiddenEls = $hiddenTableElements.filter (function () {
  1229. var found = false;
  1230. if (this.nodeType === $element[0].nodeType) {
  1231. if (typeof this.rowIndex !== 'undefined' && this.rowIndex === $element[0].rowIndex)
  1232. found = true;
  1233. else if (typeof this.cellIndex !== 'undefined' && this.cellIndex === $element[0].cellIndex &&
  1234. typeof this.parentNode.rowIndex !== 'undefined' &&
  1235. typeof $element[0].parentNode.rowIndex !== 'undefined' &&
  1236. this.parentNode.rowIndex === $element[0].parentNode.rowIndex)
  1237. found = true;
  1238. }
  1239. return found;
  1240. });
  1241. }
  1242. return (checkCellVisibilty === false || hiddenEls.length === 0);
  1243. }
  1244. function isColumnIgnored ($cell, rowLength, colIndex) {
  1245. var result = false;
  1246. if (isVisible($cell)) {
  1247. if ( defaults.ignoreColumn.length > 0 ) {
  1248. if ( $.inArray(colIndex, defaults.ignoreColumn) !== -1 ||
  1249. $.inArray(colIndex - rowLength, defaults.ignoreColumn) !== -1 ||
  1250. (colNames.length > colIndex && typeof colNames[colIndex] !== 'undefined' &&
  1251. $.inArray(colNames[colIndex], defaults.ignoreColumn) !== -1) )
  1252. result = true;
  1253. }
  1254. }
  1255. else
  1256. result = true;
  1257. return result;
  1258. }
  1259. function ForEachVisibleCell (tableRow, selector, rowIndex, rowCount, cellcallback) {
  1260. if ( typeof (cellcallback) === 'function' ) {
  1261. var ignoreRow = false;
  1262. if (typeof defaults.onIgnoreRow === 'function')
  1263. ignoreRow = defaults.onIgnoreRow($(tableRow), rowIndex);
  1264. if (ignoreRow === false &&
  1265. (defaults.ignoreRow.length === 0 ||
  1266. ($.inArray(rowIndex, defaults.ignoreRow) === -1 &&
  1267. $.inArray(rowIndex - rowCount, defaults.ignoreRow) === -1)) &&
  1268. isVisible($(tableRow))) {
  1269. var $cells = findTableElements($(tableRow), selector);
  1270. var cellCount = 0;
  1271. $cells.each(function (colIndex) {
  1272. var $cell = $(this);
  1273. var c;
  1274. var colspan = getColspan (this);
  1275. var rowspan = getRowspan (this);
  1276. // Skip ranges
  1277. $.each(ranges, function () {
  1278. var range = this;
  1279. if ( rowIndex >= range.s.r && rowIndex <= range.e.r && cellCount >= range.s.c && cellCount <= range.e.c ) {
  1280. for ( c = 0; c <= range.e.c - range.s.c; ++c )
  1281. cellcallback(null, rowIndex, cellCount++);
  1282. }
  1283. });
  1284. if ( isColumnIgnored($cell, $cells.length, colIndex) === false ) {
  1285. // Handle Row Span
  1286. if ( rowspan || colspan ) {
  1287. rowspan = rowspan || 1;
  1288. colspan = colspan || 1;
  1289. ranges.push({
  1290. s: {r: rowIndex, c: cellCount},
  1291. e: {r: rowIndex + rowspan - 1, c: cellCount + colspan - 1}
  1292. });
  1293. }
  1294. // Handle Value
  1295. cellcallback(this, rowIndex, cellCount++);
  1296. }
  1297. // Handle Colspan
  1298. if ( colspan )
  1299. for ( c = 0; c < colspan - 1; ++c )
  1300. cellcallback(null, rowIndex, cellCount++);
  1301. });
  1302. // Skip ranges
  1303. $.each(ranges, function () {
  1304. var range = this;
  1305. if ( rowIndex >= range.s.r && rowIndex <= range.e.r && cellCount >= range.s.c && cellCount <= range.e.c ) {
  1306. for ( c = 0; c <= range.e.c - range.s.c; ++c )
  1307. cellcallback(null, rowIndex, cellCount++);
  1308. }
  1309. });
  1310. }
  1311. }
  1312. }
  1313. function jsPdfDrawImage (cell, container, imgId, teOptions) {
  1314. if ( typeof teOptions.images !== 'undefined' ) {
  1315. var image = teOptions.images[imgId];
  1316. if ( typeof image !== 'undefined' ) {
  1317. var r = container.getBoundingClientRect();
  1318. var arCell = cell.width / cell.height;
  1319. var arImg = r.width / r.height;
  1320. var imgWidth = cell.width;
  1321. var imgHeight = cell.height;
  1322. var px2pt = 0.264583 * 72 / 25.4;
  1323. var uy = 0;
  1324. if ( arImg <= arCell ) {
  1325. imgHeight = Math.min(cell.height, r.height);
  1326. imgWidth = r.width * imgHeight / r.height;
  1327. }
  1328. else if ( arImg > arCell ) {
  1329. imgWidth = Math.min(cell.width, r.width);
  1330. imgHeight = r.height * imgWidth / r.width;
  1331. }
  1332. imgWidth *= px2pt;
  1333. imgHeight *= px2pt;
  1334. if ( imgHeight < cell.height )
  1335. uy = (cell.height - imgHeight) / 2;
  1336. try {
  1337. teOptions.doc.addImage(image.src, cell.textPos.x, cell.y + uy, imgWidth, imgHeight);
  1338. }
  1339. catch (e) {
  1340. // TODO: IE -> convert png to jpeg
  1341. }
  1342. cell.textPos.x += imgWidth;
  1343. }
  1344. }
  1345. }
  1346. function jsPdfOutput (doc, hasimages) {
  1347. if ( defaults.outputMode === 'string' )
  1348. return doc.output();
  1349. if ( defaults.outputMode === 'base64' )
  1350. return base64encode(doc.output());
  1351. if ( defaults.outputMode === 'window' ) {
  1352. window.URL = window.URL || window.webkitURL;
  1353. window.open(window.URL.createObjectURL(doc.output("blob")));
  1354. return;
  1355. }
  1356. try {
  1357. var blob = doc.output('blob');
  1358. saveAs(blob, defaults.fileName + '.pdf');
  1359. }
  1360. catch (e) {
  1361. downloadFile(defaults.fileName + '.pdf',
  1362. 'data:application/pdf' + (hasimages ? '' : ';base64') + ',',
  1363. hasimages ? doc.output('blob') : doc.output());
  1364. }
  1365. }
  1366. function prepareAutoTableText (cell, data, cellopt) {
  1367. var cs = 0;
  1368. if ( typeof cellopt !== 'undefined' )
  1369. cs = cellopt.colspan;
  1370. if ( cs >= 0 ) {
  1371. // colspan handling
  1372. var cellWidth = cell.width;
  1373. var textPosX = cell.textPos.x;
  1374. var i = data.table.columns.indexOf(data.column);
  1375. for ( var c = 1; c < cs; c++ ) {
  1376. var column = data.table.columns[i + c];
  1377. cellWidth += column.width;
  1378. }
  1379. if ( cs > 1 ) {
  1380. if ( cell.styles.halign === 'right' )
  1381. textPosX = cell.textPos.x + cellWidth - cell.width;
  1382. else if ( cell.styles.halign === 'center' )
  1383. textPosX = cell.textPos.x + (cellWidth - cell.width) / 2;
  1384. }
  1385. cell.width = cellWidth;
  1386. cell.textPos.x = textPosX;
  1387. if ( typeof cellopt !== 'undefined' && cellopt.rowspan > 1 )
  1388. cell.height = cell.height * cellopt.rowspan;
  1389. // fix jsPDF's calculation of text position
  1390. if ( cell.styles.valign === 'middle' || cell.styles.valign === 'bottom' ) {
  1391. var splittedText = typeof cell.text === 'string' ? cell.text.split(/\r\n|\r|\n/g) : cell.text;
  1392. var lineCount = splittedText.length || 1;
  1393. if ( lineCount > 2 )
  1394. cell.textPos.y -= ((2 - FONT_ROW_RATIO) / 2 * data.row.styles.fontSize) * (lineCount - 2) / 3;
  1395. }
  1396. return true;
  1397. }
  1398. else
  1399. return false; // cell is hidden (colspan = -1), don't draw it
  1400. }
  1401. function collectImages (cell, elements, teOptions) {
  1402. if ( typeof cell !== 'undefined' && cell !== null ) {
  1403. if ( cell.hasAttribute("data-tableexport-canvas") ) {
  1404. var imgId = new Date().getTime();
  1405. $(cell).attr("data-tableexport-canvas", imgId);
  1406. teOptions.images[imgId] = {
  1407. url: '[data-tableexport-canvas="'+imgId+'"]',
  1408. src: null
  1409. };
  1410. }
  1411. else if (elements !== 'undefined' && elements != null) {
  1412. elements.each(function () {
  1413. if ($(this).is("img")) {
  1414. var imgId = strHashCode(this.src);
  1415. teOptions.images[imgId] = {
  1416. url: this.src,
  1417. src: this.src
  1418. };
  1419. }
  1420. collectImages(cell, $(this).children(), teOptions);
  1421. });
  1422. }
  1423. }
  1424. }
  1425. function loadImages (teOptions, callback) {
  1426. var imageCount = 0;
  1427. var pendingCount = 0;
  1428. function done () {
  1429. callback(imageCount);
  1430. }
  1431. function loadImage (image) {
  1432. if (image.url) {
  1433. if (!image.src) {
  1434. var $imgContainer = $(image.url);
  1435. if ($imgContainer.length) {
  1436. imageCount = ++pendingCount;
  1437. html2canvas($imgContainer[0]).then(function(canvas) {
  1438. image.src = canvas.toDataURL("image/png");
  1439. if ( !--pendingCount )
  1440. done();
  1441. });
  1442. }
  1443. }
  1444. else {
  1445. var img = new Image();
  1446. imageCount = ++pendingCount;
  1447. img.crossOrigin = 'Anonymous';
  1448. img.onerror = img.onload = function () {
  1449. if ( img.complete ) {
  1450. if ( img.src.indexOf('data:image/') === 0 ) {
  1451. img.width = image.width || img.width || 0;
  1452. img.height = image.height || img.height || 0;
  1453. }
  1454. if ( img.width + img.height ) {
  1455. var canvas = document.createElement("canvas");
  1456. var ctx = canvas.getContext("2d");
  1457. canvas.width = img.width;
  1458. canvas.height = img.height;
  1459. ctx.drawImage(img, 0, 0);
  1460. image.src = canvas.toDataURL("image/png");
  1461. }
  1462. }
  1463. if ( !--pendingCount )
  1464. done();
  1465. };
  1466. img.src = image.url;
  1467. }
  1468. }
  1469. }
  1470. if ( typeof teOptions.images !== 'undefined' ) {
  1471. for ( var i in teOptions.images )
  1472. if ( teOptions.images.hasOwnProperty(i) )
  1473. loadImage(teOptions.images[i]);
  1474. }
  1475. return pendingCount || done();
  1476. }
  1477. function drawAutotableElements (cell, elements, teOptions) {
  1478. elements.each(function () {
  1479. if ( $(this).is("div") ) {
  1480. var bcolor = rgb2array(getStyle(this, 'background-color'), [255, 255, 255]);
  1481. var lcolor = rgb2array(getStyle(this, 'border-top-color'), [0, 0, 0]);
  1482. var lwidth = getPropertyUnitValue(this, 'border-top-width', defaults.jspdf.unit);
  1483. var r = this.getBoundingClientRect();
  1484. var ux = this.offsetLeft * teOptions.wScaleFactor;
  1485. var uy = this.offsetTop * teOptions.hScaleFactor;
  1486. var uw = r.width * teOptions.wScaleFactor;
  1487. var uh = r.height * teOptions.hScaleFactor;
  1488. teOptions.doc.setDrawColor.apply(undefined, lcolor);
  1489. teOptions.doc.setFillColor.apply(undefined, bcolor);
  1490. teOptions.doc.setLineWidth(lwidth);
  1491. teOptions.doc.rect(cell.x + ux, cell.y + uy, uw, uh, lwidth ? "FD" : "F");
  1492. }
  1493. else if ( $(this).is("img") ) {
  1494. var imgId = strHashCode(this.src);
  1495. jsPdfDrawImage (cell, this, imgId, teOptions);
  1496. }
  1497. drawAutotableElements(cell, $(this).children(), teOptions);
  1498. });
  1499. }
  1500. function drawAutotableText (cell, texttags, teOptions) {
  1501. if ( typeof teOptions.onAutotableText === 'function' ) {
  1502. teOptions.onAutotableText(teOptions.doc, cell, texttags);
  1503. }
  1504. else {
  1505. var x = cell.textPos.x;
  1506. var y = cell.textPos.y;
  1507. var style = {halign: cell.styles.halign, valign: cell.styles.valign};
  1508. if ( texttags.length ) {
  1509. var tag = texttags[0];
  1510. while ( tag.previousSibling )
  1511. tag = tag.previousSibling;
  1512. var b = false, i = false;
  1513. while ( tag ) {
  1514. var txt = tag.innerText || tag.textContent || "";
  1515. var leadingspace = (txt.length && txt[0] === " ") ? " " : "";
  1516. var trailingspace = (txt.length > 1 && txt[txt.length - 1] === " ") ? " " : "";
  1517. if (defaults.preserve.leadingWS !== true)
  1518. txt = leadingspace + trimLeft(txt);
  1519. if (defaults.preserve.trailingWS !== true)
  1520. txt = trimRight(txt) + trailingspace;
  1521. if ( $(tag).is("br") ) {
  1522. x = cell.textPos.x;
  1523. y += teOptions.doc.internal.getFontSize();
  1524. }
  1525. if ( $(tag).is("b") )
  1526. b = true;
  1527. else if ( $(tag).is("i") )
  1528. i = true;
  1529. if ( b || i )
  1530. teOptions.doc.setFontType((b && i) ? "bolditalic" : b ? "bold" : "italic");
  1531. var w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
  1532. if ( w ) {
  1533. if ( cell.styles.overflow === 'linebreak' &&
  1534. x > cell.textPos.x && (x + w) > (cell.textPos.x + cell.width) ) {
  1535. var chars = ".,!%*;:=-";
  1536. if ( chars.indexOf(txt.charAt(0)) >= 0 ) {
  1537. var s = txt.charAt(0);
  1538. w = teOptions.doc.getStringUnitWidth(s) * teOptions.doc.internal.getFontSize();
  1539. if ( (x + w) <= (cell.textPos.x + cell.width) ) {
  1540. teOptions.doc.autoTableText(s, x, y, style);
  1541. txt = txt.substring(1, txt.length);
  1542. }
  1543. w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
  1544. }
  1545. x = cell.textPos.x;
  1546. y += teOptions.doc.internal.getFontSize();
  1547. }
  1548. if ( cell.styles.overflow !== 'visible' ) {
  1549. while ( txt.length && (x + w) > (cell.textPos.x + cell.width) ) {
  1550. txt = txt.substring(0, txt.length - 1);
  1551. w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
  1552. }
  1553. }
  1554. teOptions.doc.autoTableText(txt, x, y, style);
  1555. x += w;
  1556. }
  1557. if ( b || i ) {
  1558. if ( $(tag).is("b") )
  1559. b = false;
  1560. else if ( $(tag).is("i") )
  1561. i = false;
  1562. teOptions.doc.setFontType((!b && !i) ? "normal" : b ? "bold" : "italic");
  1563. }
  1564. tag = tag.nextSibling;
  1565. }
  1566. cell.textPos.x = x;
  1567. cell.textPos.y = y;
  1568. }
  1569. else {
  1570. teOptions.doc.autoTableText(cell.text, cell.textPos.x, cell.textPos.y, style);
  1571. }
  1572. }
  1573. }
  1574. function escapeRegExp (string) {
  1575. return string == null ? "" : string.toString().replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  1576. }
  1577. function replaceAll (string, find, replace) {
  1578. return string == null ? "" : string.toString().replace(new RegExp(escapeRegExp(find), 'g'), replace);
  1579. }
  1580. function trimLeft (string) {
  1581. return string == null ? "" : string.toString().replace(/^\s+/, "");
  1582. }
  1583. function trimRight (string) {
  1584. return string == null ? "" : string.toString().replace(/\s+$/, "");
  1585. }
  1586. function parseDate(s) {
  1587. defaults.date.pattern.lastIndex = 0;
  1588. var match = defaults.date.pattern.exec(s);
  1589. if (match == null)
  1590. return false;
  1591. var y = +match[defaults.date.match_y];
  1592. if(y < 0 || y > 8099) return false;
  1593. var m = match[defaults.date.match_m]*1;
  1594. var d = match[defaults.date.match_d]*1;
  1595. if(isNaN(d)) return false;
  1596. var o = new Date(y, m - 1, d);
  1597. if (o.getFullYear() === y && o.getMonth() === (m - 1) && o.getDate() === d)
  1598. return o;
  1599. else
  1600. return false;
  1601. }
  1602. function parseNumber (value) {
  1603. value = value || "0";
  1604. if ('' !== defaults.numbers.html.thousandsSeparator)
  1605. value = replaceAll(value, defaults.numbers.html.thousandsSeparator, '');
  1606. if ('.' !== defaults.numbers.html.decimalMark)
  1607. value = replaceAll(value, defaults.numbers.html.decimalMark, '.');
  1608. return typeof value === "number" || jQuery.isNumeric(value) !== false ? value : false;
  1609. }
  1610. function parsePercent (value) {
  1611. if ( value.indexOf("%") > -1 ) {
  1612. value = parseNumber(value.replace(/%/g, ""));
  1613. if ( value !== false )
  1614. value = value / 100;
  1615. }
  1616. else
  1617. value = false;
  1618. return value;
  1619. }
  1620. function parseString (cell, rowIndex, colIndex, cellInfo) {
  1621. var result = '';
  1622. var cellType = 'text';
  1623. if ( cell !== null ) {
  1624. var $cell = $(cell);
  1625. var htmlData;
  1626. if ( $cell[0].hasAttribute("data-tableexport-canvas") ) {
  1627. htmlData = '';
  1628. }
  1629. else if ( $cell[0].hasAttribute("data-tableexport-value") ) {
  1630. htmlData = $cell.attr("data-tableexport-value");
  1631. htmlData = htmlData ? htmlData + '' : '';
  1632. }
  1633. else {
  1634. htmlData = $cell.html();
  1635. if ( typeof defaults.onCellHtmlData === 'function' )
  1636. htmlData = defaults.onCellHtmlData($cell, rowIndex, colIndex, htmlData);
  1637. else if ( htmlData !== '' ) {
  1638. var html = $.parseHTML(htmlData);
  1639. var inputidx = 0;
  1640. var selectidx = 0;
  1641. htmlData = '';
  1642. $.each(html, function () {
  1643. if ( $(this).is("input") )
  1644. htmlData += $cell.find('input').eq(inputidx++).val();
  1645. else if ( $(this).is("select") )
  1646. htmlData += $cell.find('select option:selected').eq(selectidx++).text();
  1647. else if ( $(this).is("br") )
  1648. htmlData += "<br>";
  1649. else {
  1650. if ( typeof $(this).html() === 'undefined' )
  1651. htmlData += $(this).text();
  1652. else if ( jQuery().bootstrapTable === undefined ||
  1653. ($(this).hasClass('fht-cell') === false && // BT 4
  1654. $(this).hasClass('filterControl') === false &&
  1655. $cell.parents('.detail-view').length === 0) )
  1656. htmlData += $(this).html();
  1657. if ( $(this).is("a") ) {
  1658. var href = $cell.find('a').attr('href') || '';
  1659. if ( typeof defaults.onCellHtmlHyperlink === 'function' )
  1660. result = defaults.onCellHtmlHyperlink($cell, rowIndex, colIndex, href, htmlData);
  1661. else if ( defaults.htmlHyperlink === 'href' )
  1662. result = href;
  1663. else // 'content'
  1664. result = htmlData;
  1665. htmlData = '';
  1666. }
  1667. }
  1668. });
  1669. }
  1670. }
  1671. if ( htmlData && htmlData !== '' && defaults.htmlContent === true ) {
  1672. result = $.trim(htmlData);
  1673. }
  1674. else if ( htmlData && htmlData !== '' ) {
  1675. var cellFormat = $cell.attr("data-tableexport-cellformat");
  1676. if ( cellFormat !== '' ) {
  1677. var text = htmlData.replace(/\n/g, '\u2028').replace(/(<\s*br([^>]*)>)/gi, '\u2060');
  1678. var obj = $('<div/>').html(text).contents();
  1679. var number = false;
  1680. text = '';
  1681. $.each(obj.text().split("\u2028"), function (i, v) {
  1682. if ( i > 0 )
  1683. text += " ";
  1684. if (defaults.preserve.leadingWS !== true)
  1685. v = trimLeft(v);
  1686. text += (defaults.preserve.trailingWS !== true) ? trimRight(v) : v;
  1687. });
  1688. $.each(text.split("\u2060"), function (i, v) {
  1689. if ( i > 0 )
  1690. result += "\n";
  1691. if (defaults.preserve.leadingWS !== true)
  1692. v = trimLeft(v);
  1693. if (defaults.preserve.trailingWS !== true)
  1694. v = trimRight(v);
  1695. result += v.replace(/\u00AD/g, ""); // remove soft hyphens
  1696. });
  1697. result = result.replace(/\u00A0/g, " "); // replace nbsp's with spaces
  1698. if ( defaults.type === 'json' ||
  1699. (defaults.type === 'excel' && defaults.mso.fileFormat === 'xmlss') ||
  1700. defaults.numbers.output === false ) {
  1701. number = parseNumber(result);
  1702. if ( number !== false ) {
  1703. cellType = 'number';
  1704. result = Number(number);
  1705. }
  1706. }
  1707. else if ( defaults.numbers.html.decimalMark !== defaults.numbers.output.decimalMark ||
  1708. defaults.numbers.html.thousandsSeparator !== defaults.numbers.output.thousandsSeparator ) {
  1709. number = parseNumber(result);
  1710. if ( number !== false ) {
  1711. var frac = ("" + number.substr(number < 0 ? 1 : 0)).split('.');
  1712. if ( frac.length === 1 )
  1713. frac[1] = "";
  1714. var mod = frac[0].length > 3 ? frac[0].length % 3 : 0;
  1715. cellType = 'number';
  1716. result = (number < 0 ? "-" : "") +
  1717. (defaults.numbers.output.thousandsSeparator ? ((mod ? frac[0].substr(0, mod) + defaults.numbers.output.thousandsSeparator : "") + frac[0].substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + defaults.numbers.output.thousandsSeparator)) : frac[0]) +
  1718. (frac[1].length ? defaults.numbers.output.decimalMark + frac[1] : "");
  1719. }
  1720. }
  1721. }
  1722. else
  1723. result = htmlData;
  1724. }
  1725. if ( defaults.escape === true ) {
  1726. //noinspection JSDeprecatedSymbols
  1727. result = escape(result);
  1728. }
  1729. if ( typeof defaults.onCellData === 'function' ) {
  1730. result = defaults.onCellData($cell, rowIndex, colIndex, result, cellType);
  1731. }
  1732. }
  1733. if (cellInfo !== undefined)
  1734. cellInfo.type = cellType;
  1735. return result;
  1736. }
  1737. function preventInjection (str) {
  1738. if ( str.length > 0 && defaults.preventInjection === true ) {
  1739. var chars = "=+-@";
  1740. if ( chars.indexOf(str.charAt(0)) >= 0 )
  1741. return ( "'" + str );
  1742. }
  1743. return str;
  1744. }
  1745. //noinspection JSUnusedLocalSymbols
  1746. function hyphenate (a, b, c) {
  1747. return b + "-" + c.toLowerCase();
  1748. }
  1749. function rgb2array (rgb_string, default_result) {
  1750. var re = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
  1751. var bits = re.exec(rgb_string);
  1752. var result = default_result;
  1753. if ( bits )
  1754. result = [parseInt(bits[1]), parseInt(bits[2]), parseInt(bits[3])];
  1755. return result;
  1756. }
  1757. function getCellStyles (cell) {
  1758. var a = getStyle(cell, 'text-align');
  1759. var fw = getStyle(cell, 'font-weight');
  1760. var fs = getStyle(cell, 'font-style');
  1761. var f = '';
  1762. if ( a === 'start' )
  1763. a = getStyle(cell, 'direction') === 'rtl' ? 'right' : 'left';
  1764. if ( fw >= 700 )
  1765. f = 'bold';
  1766. if ( fs === 'italic' )
  1767. f += fs;
  1768. if ( f === '' )
  1769. f = 'normal';
  1770. var result = {
  1771. style: {
  1772. align: a,
  1773. bcolor: rgb2array(getStyle(cell, 'background-color'), [255, 255, 255]),
  1774. color: rgb2array(getStyle(cell, 'color'), [0, 0, 0]),
  1775. fstyle: f
  1776. },
  1777. colspan: getColspan (cell),
  1778. rowspan: getRowspan (cell)
  1779. };
  1780. if ( cell !== null ) {
  1781. var r = cell.getBoundingClientRect();
  1782. result.rect = {
  1783. width: r.width,
  1784. height: r.height
  1785. };
  1786. }
  1787. return result;
  1788. }
  1789. function getColspan (cell) {
  1790. var result = $(cell).attr("data-tableexport-colspan");
  1791. if ( typeof result === 'undefined' && $(cell).is("[colspan]") )
  1792. result = $(cell).attr('colspan');
  1793. return (parseInt(result) || 0);
  1794. }
  1795. function getRowspan (cell) {
  1796. var result = $(cell).attr("data-tableexport-rowspan");
  1797. if ( typeof result === 'undefined' && $(cell).is("[rowspan]") )
  1798. result = $(cell).attr('rowspan');
  1799. return (parseInt(result) || 0);
  1800. }
  1801. // get computed style property
  1802. function getStyle (target, prop) {
  1803. try {
  1804. if ( window.getComputedStyle ) { // gecko and webkit
  1805. prop = prop.replace(/([a-z])([A-Z])/, hyphenate); // requires hyphenated, not camel
  1806. return window.getComputedStyle(target, null).getPropertyValue(prop);
  1807. }
  1808. if ( target.currentStyle ) { // ie
  1809. return target.currentStyle[prop];
  1810. }
  1811. return target.style[prop];
  1812. }
  1813. catch (e) {
  1814. }
  1815. return "";
  1816. }
  1817. function getUnitValue (parent, value, unit) {
  1818. var baseline = 100; // any number serves
  1819. var temp = document.createElement("div"); // create temporary element
  1820. temp.style.overflow = "hidden"; // in case baseline is set too low
  1821. temp.style.visibility = "hidden"; // no need to show it
  1822. parent.appendChild(temp); // insert it into the parent for em, ex and %
  1823. temp.style.width = baseline + unit;
  1824. var factor = baseline / temp.offsetWidth;
  1825. parent.removeChild(temp); // clean up
  1826. return (value * factor);
  1827. }
  1828. function getPropertyUnitValue (target, prop, unit) {
  1829. var value = getStyle(target, prop); // get the computed style value
  1830. var numeric = value.match(/\d+/); // get the numeric component
  1831. if ( numeric !== null ) {
  1832. numeric = numeric[0]; // get the string
  1833. return getUnitValue(target.parentElement, numeric, unit);
  1834. }
  1835. return 0;
  1836. }
  1837. function xlsxWorkbookToArrayBuffer (s) {
  1838. var buf = new ArrayBuffer(s.length);
  1839. var view = new Uint8Array(buf);
  1840. for ( var i = 0; i !== s.length; ++i ) view[i] = s.charCodeAt(i) & 0xFF;
  1841. return buf;
  1842. }
  1843. function xlsxTableToSheet(table) {
  1844. var ws = ({});
  1845. var rows = table.getElementsByTagName('tr');
  1846. var sheetRows = 10000000;
  1847. var range = {s:{r:0,c:0},e:{r:0,c:0}};
  1848. var merges = [], midx = 0;
  1849. var rowinfo = [];
  1850. var _R = 0, R = 0, _C, C, RS, CS;
  1851. var elt;
  1852. var ssfTable = XLSX.SSF.get_table();
  1853. for(; _R < rows.length && R < sheetRows; ++_R) {
  1854. var row = rows[_R];
  1855. var ignoreRow = false;
  1856. if (typeof defaults.onIgnoreRow === 'function')
  1857. ignoreRow = defaults.onIgnoreRow($(row), _R);
  1858. if (!(ignoreRow === false &&
  1859. $.inArray(rowIndex, defaults.ignoreRow) === -1 &&
  1860. $.inArray(rowIndex - rowCount, defaults.ignoreRow) === -1 &&
  1861. isVisible($(row)))) {
  1862. continue;
  1863. }
  1864. var elts = (row.children);
  1865. var _CLength = 0;
  1866. for(_C = 0; _C < elts.length; ++_C) {
  1867. elt = elts[_C];
  1868. CS = +getColspan(elt) || 1;
  1869. _CLength += CS;
  1870. }
  1871. var CSOffset = 0;
  1872. for(_C = C = 0; _C < elts.length; ++_C) {
  1873. elt = elts[_C];
  1874. CS = +getColspan(elt) || 1;
  1875. var col = _C + CSOffset;
  1876. if (isColumnIgnored($(elt), _CLength, col + (col < C ? C-col : 0)))
  1877. continue;
  1878. CSOffset += CS-1;
  1879. for(midx = 0; midx < merges.length; ++midx) {
  1880. var m = merges[midx];
  1881. if(m.s.c == C && m.s.r <= R && R <= m.e.r) {
  1882. C = m.e.c+1;
  1883. midx = -1;
  1884. }
  1885. }
  1886. if((RS = +getRowspan(elt))>0 || CS>1)
  1887. merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
  1888. var cellInfo = { type: '' };
  1889. var v = parseString(elt,_R,_C + CSOffset, cellInfo);
  1890. var o = {t:'s', v:v};
  1891. var _t = '';
  1892. var ssfId = parseInt($(elt).attr("data-tableexport-xlsxformatid") || 0);
  1893. if (ssfId === 0 &&
  1894. typeof defaults.mso.xslx.formatId.numbers === 'function')
  1895. ssfId = defaults.mso.xslx.formatId.numbers($(elt),_R,_C + CSOffset);
  1896. if (ssfId === 0 &&
  1897. typeof defaults.mso.xslx.formatId.date === 'function')
  1898. ssfId = defaults.mso.xslx.formatId.date($(elt),_R,_C + CSOffset);
  1899. if (ssfId === 49 || ssfId === '@')
  1900. _t = 's';
  1901. else if (cellInfo.type === 'number' ||
  1902. (ssfId > 0 && ssfId < 14) || (ssfId > 36 && ssfId < 41) || ssfId === 48)
  1903. _t = 'n';
  1904. else if (cellInfo.type === 'date' ||
  1905. (ssfId > 13 && ssfId < 37) || (ssfId > 44 && ssfId < 48) || ssfId === 56)
  1906. _t = 'd';
  1907. if(v != null) {
  1908. if(v.length === 0)
  1909. o.t = _t || 'z';
  1910. else if(v.trim().length === 0 || _t === 's') {
  1911. }
  1912. else if (cellInfo.type === 'function')
  1913. o = {f: v};
  1914. else if(v === 'TRUE')
  1915. o = {t:'b', v:true};
  1916. else if(v === 'FALSE')
  1917. o = {t:'b', v:false};
  1918. else if(_t === 'n' || !isNaN(xlsxToNumber(v, defaults.numbers.output))) { // yes, defaults.numbers.output is right
  1919. if (ssfId === 0 && typeof defaults.mso.xslx.formatId.numbers !== 'function')
  1920. ssfId = defaults.mso.xslx.formatId.numbers;
  1921. o = {t:'n',
  1922. v:xlsxToNumber(v, defaults.numbers.output),
  1923. z:(typeof ssfId === 'string') ? ssfId : (ssfId in ssfTable ? ssfTable[ssfId] : '0.00')
  1924. };
  1925. }
  1926. else if(_t === 'd' || parseDate(v) !== false) {
  1927. if (ssfId === 0 && typeof defaults.mso.xslx.formatId.date !== 'function')
  1928. ssfId = defaults.mso.xslx.formatId.date;
  1929. o = {t:'d',
  1930. v:parseDate(v),
  1931. z:(typeof ssfId === 'string') ? ssfId : (ssfId in ssfTable ? ssfTable[ssfId] : 'm/d/yy')
  1932. };
  1933. }
  1934. else if(_t === '' && $(elt).find('a').length) {
  1935. v = defaults.htmlHyperlink !== 'href' ? v : '';
  1936. o = {f: '=HYPERLINK("' + $(elt).find('a').attr('href') + (v.length ? '","' + v : '') + '")'};
  1937. }
  1938. }
  1939. ws[xlsxEncodeCell({c:C, r:R})] = o;
  1940. if(range.e.c < C)
  1941. range.e.c = C;
  1942. C += CS;
  1943. }
  1944. ++R;
  1945. }
  1946. if(merges.length) ws['!merges'] = merges;
  1947. if(rowinfo.length) ws['!rows'] = rowinfo;
  1948. range.e.r = R - 1;
  1949. ws['!ref'] = xlsxEncodeRange(range);
  1950. if(R >= sheetRows)
  1951. ws['!fullref'] = xlsxEncodeRange((range.e.r = rows.length-_R+R-1,range));
  1952. return ws;
  1953. }
  1954. function xlsxEncodeRow(row) { return "" + (row + 1); }
  1955. function xlsxEncodeCol(col) { var s=""; for(++col; col; col=Math.floor((col-1)/26)) s = String.fromCharCode(((col-1)%26) + 65) + s; return s; }
  1956. function xlsxEncodeCell(cell) { return xlsxEncodeCol(cell.c) + xlsxEncodeRow(cell.r); }
  1957. function xlsxEncodeRange(cs,ce) {
  1958. if(typeof ce === 'undefined' || typeof ce === 'number') {
  1959. return xlsxEncodeRange(cs.s, cs.e);
  1960. }
  1961. if(typeof cs !== 'string') cs = xlsxEncodeCell((cs));
  1962. if(typeof ce !== 'string') ce = xlsxEncodeCell((ce));
  1963. return cs === ce ? cs : cs + ":" + ce;
  1964. }
  1965. function xlsxToNumber(s, numbersFormat) {
  1966. var v = Number(s);
  1967. if(!isNaN(v)) return v;
  1968. var wt = 1;
  1969. var ss = s;
  1970. if ('' !== numbersFormat.thousandsSeparator)
  1971. ss = ss.replace(new RegExp('([\\d])' + numbersFormat.thousandsSeparator + '([\\d])','g'),"$1$2");
  1972. if ('.' !== numbersFormat.decimalMark)
  1973. ss = ss.replace(new RegExp('([\\d])' + numbersFormat.decimalMark + '([\\d])','g'),"$1.$2");
  1974. ss = ss.replace(/[$]/g,"").replace(/[%]/g, function() { wt *= 100; return "";});
  1975. if(!isNaN(v = Number(ss))) return v / wt;
  1976. ss = ss.replace(/[(](.*)[)]/,function($$, $1) { wt = -wt; return $1;});
  1977. if(!isNaN(v = Number(ss))) return v / wt;
  1978. return v;
  1979. }
  1980. function strHashCode (str) {
  1981. var hash = 0, i, chr, len;
  1982. if ( str.length === 0 ) return hash;
  1983. for ( i = 0, len = str.length; i < len; i++ ) {
  1984. chr = str.charCodeAt(i);
  1985. hash = ((hash << 5) - hash) + chr;
  1986. hash |= 0; // Convert to 32bit integer
  1987. }
  1988. return hash;
  1989. }
  1990. function saveToFile (data, fileName, type, charset, encoding, bom) {
  1991. var saveIt = true;
  1992. if ( typeof defaults.onBeforeSaveToFile === 'function' ) {
  1993. saveIt = defaults.onBeforeSaveToFile(data, fileName, type, charset, encoding);
  1994. if ( typeof saveIt !== 'boolean' )
  1995. saveIt = true;
  1996. }
  1997. if (saveIt) {
  1998. try {
  1999. blob = new Blob([data], {type: type + ';charset=' + charset});
  2000. saveAs (blob, fileName, bom === false);
  2001. if ( typeof defaults.onAfterSaveToFile === 'function' )
  2002. defaults.onAfterSaveToFile(data, fileName);
  2003. }
  2004. catch (e) {
  2005. downloadFile (fileName,
  2006. 'data:' + type +
  2007. (charset.length ? ';charset=' + charset : '') +
  2008. (encoding.length ? ';' + encoding : '') + ',',
  2009. (bom ? ('\ufeff' + data) : data));
  2010. }
  2011. }
  2012. }
  2013. function downloadFile (filename, header, data) {
  2014. var ua = window.navigator.userAgent;
  2015. if ( filename !== false && window.navigator.msSaveOrOpenBlob ) {
  2016. //noinspection JSUnresolvedFunction
  2017. window.navigator.msSaveOrOpenBlob(new Blob([data]), filename);
  2018. }
  2019. else if ( filename !== false && (ua.indexOf("MSIE ") > 0 || !!ua.match(/Trident.*rv\:11\./)) ) {
  2020. // Internet Explorer (<= 9) workaround by Darryl (https://github.com/dawiong/tableExport.jquery.plugin)
  2021. // based on sampopes answer on http://stackoverflow.com/questions/22317951
  2022. // ! Not working for json and pdf format !
  2023. var frame = document.createElement("iframe");
  2024. if ( frame ) {
  2025. document.body.appendChild(frame);
  2026. frame.setAttribute("style", "display:none");
  2027. frame.contentDocument.open("txt/plain", "replace");
  2028. frame.contentDocument.write(data);
  2029. frame.contentDocument.close();
  2030. frame.contentWindow.focus();
  2031. var extension = filename.substr((filename.lastIndexOf('.') +1));
  2032. switch(extension) {
  2033. case 'doc': case 'json': case 'png': case 'pdf': case 'xls': case 'xlsx':
  2034. filename += ".txt";
  2035. break;
  2036. }
  2037. frame.contentDocument.execCommand("SaveAs", true, filename);
  2038. document.body.removeChild(frame);
  2039. }
  2040. }
  2041. else {
  2042. var DownloadLink = document.createElement('a');
  2043. if ( DownloadLink ) {
  2044. var blobUrl = null;
  2045. DownloadLink.style.display = 'none';
  2046. if ( filename !== false )
  2047. DownloadLink.download = filename;
  2048. else
  2049. DownloadLink.target = '_blank';
  2050. if ( typeof data === 'object' ) {
  2051. window.URL = window.URL || window.webkitURL;
  2052. var binaryData = [];
  2053. binaryData.push(data);
  2054. blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: header}));
  2055. DownloadLink.href = blobUrl;
  2056. }
  2057. else if ( header.toLowerCase().indexOf("base64,") >= 0 )
  2058. DownloadLink.href = header + base64encode(data);
  2059. else
  2060. DownloadLink.href = header + encodeURIComponent(data);
  2061. document.body.appendChild(DownloadLink);
  2062. if ( document.createEvent ) {
  2063. if ( DownloadEvt === null )
  2064. DownloadEvt = document.createEvent('MouseEvents');
  2065. DownloadEvt.initEvent('click', true, false);
  2066. DownloadLink.dispatchEvent(DownloadEvt);
  2067. }
  2068. else if ( document.createEventObject )
  2069. DownloadLink.fireEvent('onclick');
  2070. else if ( typeof DownloadLink.onclick === 'function' )
  2071. DownloadLink.onclick();
  2072. setTimeout(function(){
  2073. if ( blobUrl )
  2074. window.URL.revokeObjectURL(blobUrl);
  2075. document.body.removeChild(DownloadLink);
  2076. if ( typeof defaults.onAfterSaveToFile === 'function' )
  2077. defaults.onAfterSaveToFile(data, filename);
  2078. }, 100);
  2079. }
  2080. }
  2081. }
  2082. function utf8Encode (text) {
  2083. if (typeof text === 'string') {
  2084. text = text.replace(/\x0d\x0a/g, "\x0a");
  2085. var utftext = "";
  2086. for ( var n = 0; n < text.length; n++ ) {
  2087. var c = text.charCodeAt(n);
  2088. if ( c < 128 ) {
  2089. utftext += String.fromCharCode(c);
  2090. }
  2091. else if ( (c > 127) && (c < 2048) ) {
  2092. utftext += String.fromCharCode((c >> 6) | 192);
  2093. utftext += String.fromCharCode((c & 63) | 128);
  2094. }
  2095. else {
  2096. utftext += String.fromCharCode((c >> 12) | 224);
  2097. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2098. utftext += String.fromCharCode((c & 63) | 128);
  2099. }
  2100. }
  2101. return utftext;
  2102. }
  2103. return text;
  2104. }
  2105. function base64encode (input) {
  2106. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  2107. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  2108. var output = "";
  2109. var i = 0;
  2110. input = utf8Encode(input);
  2111. while ( i < input.length ) {
  2112. chr1 = input.charCodeAt(i++);
  2113. chr2 = input.charCodeAt(i++);
  2114. chr3 = input.charCodeAt(i++);
  2115. enc1 = chr1 >> 2;
  2116. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  2117. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  2118. enc4 = chr3 & 63;
  2119. if ( isNaN(chr2) ) {
  2120. enc3 = enc4 = 64;
  2121. } else if ( isNaN(chr3) ) {
  2122. enc4 = 64;
  2123. }
  2124. output = output +
  2125. keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  2126. keyStr.charAt(enc3) + keyStr.charAt(enc4);
  2127. }
  2128. return output;
  2129. }
  2130. return this;
  2131. }
  2132. })(jQuery);