rpt_excel_util.js 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2017/4/1.
  4. */
  5. const JV = require('../rpt_component/jpc_value_define');
  6. const fs = require('fs');
  7. const JSZip = require('jszip');
  8. const strUtil = require('../public/stringUtil');
  9. const jpcCmnHelper = require('../rpt_component/helper/jpc_helper_common');
  10. const DPI = jpcCmnHelper.getScreenDPI()[0];
  11. const fsUtil = require('../public/fsUtil');
  12. const dftHeadXml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  13. const uuidV1 = require('uuid/v1');
  14. const atob = require('atob');
  15. function writeContentTypes(sheets, isSinglePage, hasSignature) {
  16. const rst = [];
  17. rst.push(dftHeadXml + '\r\n');
  18. rst.push('<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">');
  19. rst.push('<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>');
  20. rst.push('<Default Extension="xml" ContentType="application/xml"/>');
  21. if (hasSignature) {
  22. rst.push('<Default Extension="png" ContentType="image/png"/>');
  23. }
  24. rst.push('<Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>');
  25. rst.push('<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>');
  26. rst.push('<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>');
  27. rst.push('<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>');
  28. rst.push('<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>');
  29. if (isSinglePage) {
  30. rst.push('<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  31. if (hasSignature) {
  32. rst.push('<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  33. }
  34. } else {
  35. for (let i = 0; i < sheets.length; i++) {
  36. rst.push('<Override PartName="/xl/worksheets/sheet' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>');
  37. if (hasSignature) {
  38. rst.push('<Override PartName="/xl/drawings/drawing' + (i + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>');
  39. }
  40. }
  41. }
  42. rst.push('<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>');
  43. rst.push('</Types>');
  44. return rst;
  45. }
  46. function writeRootRels() {
  47. const rst = [];
  48. rst.push(dftHeadXml + '\r\n');
  49. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  50. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>');
  51. rst.push('<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>');
  52. rst.push('<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>');
  53. rst.push('</Relationships>');
  54. return rst;
  55. }
  56. function writeApp(sheets, isSinglePage) {
  57. const rst = [];
  58. rst.push(dftHeadXml + '\r\n');
  59. rst.push('<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">');
  60. rst.push('<Application>Microsoft Excel</Application>');
  61. rst.push('<DocSecurity>0</DocSecurity>');
  62. rst.push('<ScaleCrop>false</ScaleCrop>');
  63. rst.push('<HeadingPairs>');
  64. rst.push('<vt:vector size="2" baseType="variant">');
  65. rst.push('<vt:variant><vt:lpstr>工作表</vt:lpstr></vt:variant>');
  66. if (isSinglePage) rst.push('<vt:variant><vt:i4>1</vt:i4></vt:variant>');
  67. else rst.push('<vt:variant><vt:i4>' + sheets.length + '</vt:i4></vt:variant>');
  68. rst.push('</vt:vector>');
  69. rst.push('</HeadingPairs>');
  70. rst.push('<TitlesOfParts>');
  71. if (isSinglePage) {
  72. rst.push('<vt:vector size="1" baseType="lpstr">');
  73. rst.push('<vt:lpstr>' + sheets[0].sheetName + '</vt:lpstr>');
  74. } else {
  75. rst.push('<vt:vector size="' + sheets.length + '" baseType="lpstr">');
  76. for (let i = 0; i < sheets.length; i++) {
  77. rst.push('<vt:lpstr>' + sheets[i].sheetName + '</vt:lpstr>');
  78. }
  79. }
  80. rst.push('</vt:vector>');
  81. rst.push('</TitlesOfParts>');
  82. rst.push('<Company>SmartCost</Company>');
  83. rst.push('<LinksUpToDate>false</LinksUpToDate>');
  84. rst.push('<SharedDoc>false</SharedDoc>');
  85. rst.push('<HyperlinksChanged>false</HyperlinksChanged>');
  86. rst.push('<AppVersion>12.0000</AppVersion>');
  87. rst.push('</Properties>');
  88. return rst;
  89. }
  90. function writeCore() {
  91. const rst = [];
  92. const p_fillZero = function(val) {
  93. let rst = val;
  94. if (val < 10) {
  95. rst = '0' + val;
  96. }
  97. return rst;
  98. };
  99. rst.push(dftHeadXml + '\r\n');
  100. rst.push('<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">');
  101. rst.push('<dc:creator>SmartCost</dc:creator>');
  102. rst.push('<cp:lastModifiedBy>SmartCost</cp:lastModifiedBy>');
  103. const dt = new Date();
  104. dt.setDate(dt.getDate() - 8 / 24); // it's GMT time, so please add the server offset time ( -8 hours )
  105. const dtStr = dt.getFullYear() + '-' + p_fillZero(dt.getMonth() + 1) + '-' + p_fillZero(dt.getDate()) + 'T' +
  106. p_fillZero(dt.getHours()) + ':' + p_fillZero(dt.getMinutes()) + ':' + p_fillZero(dt.getSeconds()) + 'Z';
  107. rst.push('<dcterms:created xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:created>');
  108. rst.push('<dcterms:modified xsi:type="dcterms:W3CDTF">' + dtStr + '</dcterms:modified>');
  109. rst.push('</cp:coreProperties>');
  110. return rst;
  111. }
  112. function writeXlWorkBook(sheets, isSinglePage) {
  113. const rst = [];
  114. rst.push(dftHeadXml + '\r\n');
  115. rst.push('<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  116. rst.push('<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>');
  117. rst.push('<workbookPr defaultThemeVersion="124226"/>');
  118. rst.push('<bookViews><workbookView xWindow="360" yWindow="345" windowWidth="14655" windowHeight="4305"/></bookViews>');
  119. rst.push('<sheets>');
  120. if (isSinglePage) {
  121. rst.push('<sheet name="' + sheets[0].sheetName + '" sheetId="1" r:id="rId1"/>');
  122. } else {
  123. for (let i = 0; i < sheets.length; i++) {
  124. rst.push('<sheet name="' + sheets[i].sheetName + '" sheetId="' + (i + 1) + '" r:id="rId' + (i + 1) + '"/>');
  125. }
  126. }
  127. rst.push('</sheets>');
  128. rst.push('<calcPr calcId="124519"/>');
  129. rst.push('</workbook>');
  130. return rst;
  131. }
  132. function writeXlRels(sheets, isSinglePage) {
  133. const rst = [];
  134. let idx = 1;
  135. rst.push(dftHeadXml + '\r\n');
  136. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  137. if (isSinglePage) {
  138. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>');
  139. idx++;
  140. } else {
  141. for (let i = 0; i < sheets.length; i++) {
  142. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet' + (i + 1) + '.xml"/>');
  143. idx++;
  144. }
  145. }
  146. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>');
  147. idx++;
  148. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>');
  149. idx++;
  150. rst.push('<Relationship Id="rId' + idx + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>');
  151. rst.push('</Relationships>');
  152. return rst;
  153. }
  154. function writeTheme() {
  155. const rst = fs.readFileSync(__dirname + '/excel_base_files/theme1.xml', 'utf8', 'r');
  156. return rst;
  157. }
  158. function writeStyles(stylesObj, hasSignature) {
  159. const rst = [];
  160. rst.push(dftHeadXml + '\r\n');
  161. rst.push('<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">');
  162. // 1. push fonts
  163. rst.push('<fonts count="' + stylesObj.fonts.length + '">');
  164. for (let i = 0; i < stylesObj.fonts.length; i++) {
  165. const font = stylesObj.fonts[i];
  166. rst.push('<font>');
  167. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]])) {
  168. rst.push('<b/>');
  169. }
  170. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]])) {
  171. rst.push('<i/>');
  172. }
  173. if (strUtil.convertStrToBoolean(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]])) {
  174. rst.push('<u/>');
  175. }
  176. rst.push('<sz val="' + font.size + '"/>');
  177. rst.push('<color indexed="' + font.colorIdx + '"/>');
  178. rst.push('<name val="' + font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] + '"/>');
  179. rst.push('<charset val="' + font.charset + '"/>');
  180. rst.push('</font>');
  181. }
  182. rst.push('</fonts>');
  183. // 2. push default fills
  184. rst.push('<fills count="2"><fill><patternFill patternType="none" /></fill><fill><patternFill patternType="gray125" /></fill></fills>');
  185. // 3. push borders
  186. rst.push('<borders count="' + stylesObj.borders.length + '">');
  187. const private_setBorder = function(border, borderDirection) {
  188. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 0) {
  189. rst.push('<' + borderDirection.toLowerCase() + '/>');
  190. } else {
  191. let bW = 'thin';
  192. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) === 2) bW = 'medium';
  193. if (parseInt(border[borderDirection][JV.PROP_LINE_WEIGHT]) > 2) bW = 'thick';
  194. rst.push('<' + borderDirection.toLowerCase() + ' style="' + bW + '"><color indexed="64"/></' + borderDirection.toLowerCase() + '>');
  195. }
  196. };
  197. for (let i = 0; i < stylesObj.borders.length; i++) {
  198. const border = stylesObj.borders[i];
  199. rst.push('<border>');
  200. private_setBorder(border, JV.PROP_LEFT);
  201. private_setBorder(border, JV.PROP_RIGHT);
  202. private_setBorder(border, JV.PROP_TOP);
  203. private_setBorder(border, JV.PROP_BOTTOM);
  204. rst.push('<diagonal />');
  205. rst.push('</border>');
  206. }
  207. rst.push('</borders>');
  208. // 4. push cellStyleXfs
  209. rst.push('<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"><alignment vertical="center"/></xf></cellStyleXfs>');
  210. // 5. push cellXfs
  211. rst.push('<cellXfs count="' + stylesObj.cellXfs.length + '">');
  212. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  213. const excelStyle = stylesObj.cellXfs[i];
  214. rst.push('<xf numFmtId="0" fontId="' + excelStyle.fontId + '" fillId="0" borderId="' + excelStyle.borderId + '" xfId="0">');
  215. // pageData[JV.NODE_FONT_COLLECTION] excelStyle.fontId
  216. let alignStr = '<alignment';
  217. let textRotation = 0;
  218. let newHorizontal = excelStyle[JV.CONTROL_PROPS[2]];
  219. let newVertical = excelStyle[JV.CONTROL_PROPS[3]];
  220. if (excelStyle[JV.CONTROL_PROPS[5]] && !strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_CLOSE_OUTPUT]])) {
  221. newVertical = excelStyle[JV.CONTROL_PROPS[5]];
  222. }
  223. if (parseInt(excelStyle.fontAngle) !== 0) {
  224. let tmpH = newHorizontal;
  225. let tmpV = newVertical;
  226. if (excelStyle.fontAngle > 0) {
  227. textRotation = 180;
  228. if (newHorizontal === 'left') {
  229. tmpV = 'top';
  230. } else if (newHorizontal === 'right') {
  231. tmpV = 'bottom';
  232. } else {
  233. tmpV = 'center';
  234. }
  235. if (newVertical === 'top') {
  236. tmpH = 'right';
  237. } else if (newVertical === 'bottom') {
  238. tmpH = 'left';
  239. } else if (newVertical === 'justify') {
  240. tmpH = 'justify';
  241. } else {
  242. tmpH = 'center';
  243. }
  244. } else {
  245. textRotation = 90;
  246. if (newHorizontal === 'left') {
  247. tmpV = 'bottom';
  248. } else if (newHorizontal === 'right') {
  249. tmpV = 'top';
  250. } else {
  251. tmpV = 'center';
  252. }
  253. if (newVertical === 'top') {
  254. tmpH = 'left';
  255. } else if (newVertical === 'bottom') {
  256. tmpH = 'right';
  257. } else {
  258. tmpH = 'center';
  259. }
  260. }
  261. newHorizontal = tmpH;
  262. newVertical = tmpV;
  263. }
  264. alignStr += ' horizontal="' + newHorizontal + '" vertical="' + newVertical + '"';
  265. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[0]])) {
  266. alignStr += ' shrinkToFit="1"';
  267. }
  268. if (strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[4]]) && !strUtil.convertStrToBoolean(excelStyle[JV.CONTROL_PROPS[6]])) {
  269. alignStr += ' wrapText="1"';
  270. }
  271. if (textRotation !== 0) {
  272. alignStr += ' textRotation="' + textRotation + '"';
  273. }
  274. alignStr += '/>';
  275. rst.push(alignStr);
  276. rst.push('</xf>');
  277. }
  278. rst.push('</cellXfs>');
  279. // 6. others (xfl style / dxfs / tableStyles)
  280. rst.push('<cellStyles count="1"><cellStyle name="常规" xfId="0" builtinId="0"/></cellStyles>');
  281. rst.push('<dxfs count="0"/>');
  282. rst.push('<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>');
  283. if (hasSignature) {
  284. rst.push('<extLst>');
  285. rst.push('<ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">');
  286. rst.push('<x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>');
  287. rst.push('</ext>');
  288. rst.push('<ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">');
  289. rst.push('<x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>');
  290. rst.push('</ext>');
  291. rst.push('</extLst>');
  292. }
  293. rst.push('</styleSheet>');
  294. return rst;
  295. }
  296. function writeSharedString(sharedStrList) {
  297. const rst = [];
  298. const pri_func_write = function(cellVal) {
  299. if (cellVal !== null) {
  300. if ((typeof cellVal === 'string') && cellVal.indexOf(' ') === 0) {
  301. rst.push('<si><t xml:space="preserve">' + cellVal + '</t></si>');
  302. } else {
  303. rst.push('<si><t>' + cellVal + '</t></si>');
  304. }
  305. }
  306. };
  307. if (sharedStrList && sharedStrList.length > 0) {
  308. rst.push(dftHeadXml + '\r\n');
  309. rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
  310. const regExp = new RegExp('<', 'gm');
  311. for (let i = 0; i < sharedStrList.length; i++) {
  312. if (typeof sharedStrList[i] === 'string') {
  313. // 转换特殊字符,如 < , 则需要转义一下
  314. sharedStrList[i] = sharedStrList[i].replace(regExp, '&lt;');
  315. if (sharedStrList[i].indexOf('|') >= 0) {
  316. // rst.push('<si><t>' + sharedStrList[i].split('|').join('\r\n') + '</t></si>');
  317. // rst.push('<si><t>' + sharedStrList[i].split('|').join('\n') + '</t></si>');
  318. pri_func_write(sharedStrList[i].split('|').join('\n'));
  319. } else {
  320. // rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  321. pri_func_write(sharedStrList[i]);
  322. }
  323. // rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
  324. } else {
  325. // rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
  326. pri_func_write(sharedStrList[i]);
  327. }
  328. }
  329. rst.push('</sst>');
  330. }
  331. return rst;
  332. }
  333. function _setupPgBrks(pageData) {
  334. pageData.pageBreaks = [];
  335. let pgBrkIdx = 0;
  336. const pgBrk = [];
  337. function _insertMaxBottom(pCells, targetBottomArr, chkTop) {
  338. if (pCells) {
  339. for (const cell of pCells) {
  340. let cb = parseFloat(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  341. if (targetBottomArr.indexOf(cb) < 0) {
  342. targetBottomArr.push(cb);
  343. }
  344. if (chkTop) {
  345. cb = parseFloat(cell[JV.PROP_AREA][JV.PROP_TOP]);
  346. if (targetBottomArr.indexOf(cb) < 0) {
  347. targetBottomArr.push(cb);
  348. }
  349. }
  350. }
  351. }
  352. }
  353. // for (const page of pageData.items) {
  354. // for (let i = 0; i < pageData.items.length - 1; i++) {
  355. for (let i = 0; i < pageData.items.length; i++) {
  356. const page = pageData.items[i];
  357. const maxBottomArr = [];
  358. _insertMaxBottom(page[JV.PROP_CELLS], maxBottomArr);
  359. _insertMaxBottom(page[JV.PROP_SIGNATURE_CELLS], maxBottomArr);
  360. _insertMaxBottom(page[JV.PROP_SIGNATURE_DATE_CELLS], maxBottomArr);
  361. _insertMaxBottom(page[JV.PROP_SIGNATURE_AUDIT_CELLS], maxBottomArr);
  362. _insertMaxBottom(page[JV.PROP_WATERMARK_CELLS], maxBottomArr, true);
  363. pgBrkIdx += maxBottomArr.length;
  364. pgBrk.push(pgBrkIdx);
  365. }
  366. pageData.pageBreaks.push(pgBrk);
  367. }
  368. function writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr) {
  369. const rst = [];
  370. const private_pushDftFont = function() {
  371. const font = {};
  372. if (!(stylesObj.fonts)) {
  373. stylesObj.fonts = [];
  374. }
  375. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = '宋体'; // font name
  376. font.size = 12;
  377. font.charset = 134;
  378. font.colorIdx = '8';
  379. stylesObj.fonts.push(font);
  380. };
  381. const private_buildFirstDftStyle = function() {
  382. stylesObj.cellXfs = [];
  383. stylesObj.borders = [];
  384. const fontId = 0;
  385. const borderId = 0;
  386. const border = {};
  387. border[JV.PROP_LEFT] = {};
  388. border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  389. border[JV.PROP_RIGHT] = {};
  390. border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  391. border[JV.PROP_TOP] = {};
  392. border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  393. border[JV.PROP_BOTTOM] = {};
  394. border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  395. stylesObj.borders.push(border);
  396. const cellControl = pageData[JV.NODE_CONTROL_COLLECTION].Default;
  397. const sheetControl = {};
  398. sheetControl.fontId = fontId;
  399. sheetControl.borderId = borderId;
  400. sheetControl.fontAngle = 0;
  401. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  402. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  403. }
  404. stylesObj.cellXfs.push(sheetControl);
  405. };
  406. private_pushDftFont();
  407. private_buildFirstDftStyle();
  408. let sheetIdx = 0;
  409. if (isSinglePage) {
  410. rst.push(writeSheet(pageData, null, paperSize, sharedStrList, stylesObj, null, hasSignature, sheetIdx, signSheetIdxArr, true, -1));
  411. } else {
  412. for (let i = 0; i < pageData.items.length; i++) {
  413. let appointedMergeBand = null;
  414. if (custSheetMergeBands && custSheetMergeBands.length > i) {
  415. appointedMergeBand = custSheetMergeBands[i];
  416. }
  417. rst.push(writeSheet(pageData, pageData.items[i], paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr, i === 0, i));
  418. sheetIdx++;
  419. }
  420. }
  421. return rst;
  422. }
  423. function getProperSignatureArea(cell, control) {
  424. // 约定默认长宽比例是2:1,图片分辨率是600*300
  425. const rst = [0, 0, 0, 0]; // left, top, right, bottom
  426. if (cell && cell[JV.PROP_AREA]) {
  427. let width = cell[JV.PROP_AREA][JV.PROP_RIGHT] - cell[JV.PROP_AREA][JV.PROP_LEFT],
  428. height = cell[JV.PROP_AREA][JV.PROP_BOTTOM] - cell[JV.PROP_AREA][JV.PROP_TOP];
  429. if (width > height * 2) {
  430. width = height * 2;
  431. } else {
  432. height = width / 2;
  433. }
  434. switch (control[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  435. case 'left':
  436. rst[0] = cell[JV.PROP_AREA][JV.PROP_LEFT];
  437. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  438. rst[2] = rst[0] + width;
  439. rst[3] = rst[1] + height;
  440. break;
  441. case 'right':
  442. rst[2] = cell[JV.PROP_AREA][JV.PROP_RIGHT];
  443. rst[3] = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  444. rst[0] = rst[2] - width;
  445. rst[1] = rst[3] - height;
  446. break;
  447. default:
  448. // center
  449. rst[0] = (cell[JV.PROP_AREA][JV.PROP_LEFT] + cell[JV.PROP_AREA][JV.PROP_RIGHT] - width) / 2;
  450. rst[1] = cell[JV.PROP_AREA][JV.PROP_TOP];
  451. rst[2] = rst[0] + width;
  452. rst[3] = rst[1] + height;
  453. break;
  454. }
  455. }
  456. // rst[0] = rst[0] + JpcCanvasOutput.offsetX;
  457. // rst[2] = rst[2] + JpcCanvasOutput.offsetX;
  458. // rst[1] = rst[1] + JpcCanvasOutput.offsetY;
  459. // rst[3] = rst[3] + JpcCanvasOutput.offsetY;
  460. return rst;
  461. }
  462. function preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos) {
  463. let cell;
  464. let pos;
  465. const private_array_sort = function(i1, i2) {
  466. return (i1 - i2);
  467. };
  468. const private_set_cell_pos = function(theCell, theXPos, theYPos) {
  469. pos = theCell[JV.PROP_AREA][JV.PROP_LEFT];
  470. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  471. pos = theCell[JV.PROP_AREA][JV.PROP_RIGHT];
  472. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  473. pos = theCell[JV.PROP_AREA][JV.PROP_TOP];
  474. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  475. pos = theCell[JV.PROP_AREA][JV.PROP_BOTTOM];
  476. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  477. };
  478. const private_set_cell_pos2 = function(area, theXPos, theYPos) {
  479. pos = area[0];
  480. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  481. pos = area[2];
  482. if (theXPos.indexOf(pos) < 0) theXPos.push(pos);
  483. pos = area[1];
  484. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  485. pos = area[3];
  486. if (theYPos.indexOf(pos) < 0) theYPos.push(pos);
  487. };
  488. const self_analyze_sheet_pos = function(theShtData, theXPos, theYPos) {
  489. for (let i = 0; i < theShtData[JV.PROP_CELLS].length; i++) {
  490. cell = theShtData[JV.PROP_CELLS][i];
  491. private_set_cell_pos(cell, theXPos, theYPos);
  492. }
  493. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_CELLS].length; i++) {
  494. cell = theShtData[JV.PROP_SIGNATURE_CELLS][i];
  495. private_set_cell_pos(cell, theXPos, theYPos);
  496. let cellControl;
  497. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  498. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  499. } else {
  500. cellControl = cell[JV.PROP_CONTROL];
  501. }
  502. const area = getProperSignatureArea(cell, cellControl);
  503. private_set_cell_pos2(area, theXPos, theYPos);
  504. }
  505. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_DATE_CELLS].length; i++) {
  506. cell = theShtData[JV.PROP_SIGNATURE_DATE_CELLS][i];
  507. private_set_cell_pos(cell, theXPos, theYPos);
  508. }
  509. for (let i = 0; i < theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS].length; i++) {
  510. cell = theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS][i];
  511. private_set_cell_pos(cell, theXPos, theYPos);
  512. }
  513. for (let i = 0; i < theShtData[JV.PROP_WATERMARK_CELLS].length; i++) {
  514. cell = theShtData[JV.PROP_WATERMARK_CELLS][i];
  515. // private_set_cell_pos(cell, theXPos, theYPos);
  516. let cellControl;
  517. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  518. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  519. } else {
  520. cellControl = cell[JV.PROP_CONTROL];
  521. }
  522. const area = getProperSignatureArea(cell, cellControl);
  523. private_set_cell_pos2(area, theXPos, theYPos);
  524. }
  525. };
  526. xPos.push(0);
  527. if (sheetData) {
  528. // current sheet data
  529. // console.log('preAnalyzePos not single');
  530. yPos.push(0);
  531. self_analyze_sheet_pos(sheetData, xPos, yPos);
  532. xPos.sort(private_array_sort);
  533. yPos.sort(private_array_sort);
  534. } else {
  535. // total data in one sheet
  536. const marginBottomPos = Math.round((pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54) * DPI);
  537. // console.log('preAnalyzePos isSingle start');
  538. for (const shtItemData of pageData.items) {
  539. const tmpPos = [];
  540. tmpPos.push(0);
  541. self_analyze_sheet_pos(shtItemData, xPos, tmpPos);
  542. tmpPos.sort(private_array_sort);
  543. if (marginBottomPos - tmpPos[tmpPos.length - 1] > 10) {
  544. // 此逻辑是为了防止打印跨页(假设有些报表模板高度设置离底部margin还好远,导出excel后预览时会发现跨页现象(即下一页的某几行数据会挪到前一页来预览))
  545. tmpPos.push(marginBottomPos - 10);
  546. }
  547. // console.log('preAnalyzePos push to yMultiPos');
  548. yMultiPos.push(tmpPos);
  549. }
  550. xPos.sort(private_array_sort);
  551. yPos.splice(0, yPos.length);
  552. for (const p of yMultiPos[0]) {
  553. yPos.push(p);
  554. }
  555. }
  556. }
  557. function writeSheet(pageData, sheetData, paperSize, sharedStrList, stylesObj, appointedMergeBand, hasSignature, sheetIdx, signSheetIdxArr, isFirstSheet, pgBrkIdx) {
  558. const rst = [];
  559. const xPos = [];
  560. const yPos = [];
  561. const yMultiPos = [];
  562. let currentMergeBorder = null;
  563. const headerStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  564. let currentPageMergePos = null; // 在 JV.PAGING_OPTION_INFINITY 场合应用
  565. const private_getCellIdxStr = function(idx) {
  566. let rst = 'A';
  567. if (idx < 26) {
  568. rst = headerStr[idx];
  569. } else if (idx < 26 * 26 + 26) {
  570. const ti = Math.floor(idx / 26);
  571. const tj = idx % 26;
  572. rst = headerStr[ti - 1] + headerStr[tj];
  573. } else if (idx < 26 * 26 * 26 + 26) {
  574. const ti = Math.floor(idx / (26 * 26));
  575. const tj = Math.floor((idx - ti * 26 * 26) / 26);
  576. const tk = idx % 26;
  577. rst = headerStr[ti - 1] + headerStr[tj - 1] + headerStr[tk];
  578. }
  579. return rst;
  580. };
  581. const private_getSharedStrIdx = function(val) {
  582. let strVal = val;
  583. if (val === null || val === undefined || (typeof val === 'number' && isNaN(val))) {
  584. strVal = '';
  585. }
  586. let rst = sharedStrList.indexOf(strVal);
  587. if (rst < 0) {
  588. sharedStrList.push(strVal);
  589. rst = sharedStrList.length - 1;
  590. }
  591. return rst;
  592. };
  593. const private_getFontId = function(cell) {
  594. let rst = 0;
  595. let hasFont = false;
  596. if (!(stylesObj.fonts)) {
  597. stylesObj.fonts = [];
  598. // for (let i = 0; i < sheetData.font_collection)
  599. }
  600. let sheetFont = null;
  601. if (typeof cell[JV.PROP_FONT] === 'string') {
  602. sheetFont = pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]];
  603. } else {
  604. sheetFont = cell[JV.PROP_FONT];
  605. }
  606. for (let i = 0; i < stylesObj.fonts.length; i++) {
  607. const font = stylesObj.fonts[i];
  608. if (sheetFont) {
  609. if (font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] && font.size === Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4)
  610. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]
  611. && font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] === sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]) {
  612. hasFont = true;
  613. rst = i;
  614. break;
  615. }
  616. } else {
  617. break;
  618. }
  619. }
  620. if (!hasFont) {
  621. const font = {};
  622. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]; // font name
  623. // font.size = Math.round(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  624. font.size = Math.floor(sheetFont[JV.FONT_PROPS[1]] * 3 / 4);
  625. font.charset = 134;
  626. font.colorIdx = '8';
  627. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]]; // font bold
  628. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]]; // font italic
  629. font[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = sheetFont[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]]; // font underline
  630. stylesObj.fonts.push(font);
  631. rst = stylesObj.fonts.length - 1;
  632. }
  633. return rst;
  634. };
  635. const private_chkAndGetMergeLine = function(cell, sheetBorder, borderStr, needFurtherChk) {
  636. let rst = 0;
  637. const mergeBorder = currentMergeBorder;
  638. let mergeBand = pageData[JV.BAND_PROP_MERGE_BAND];
  639. if (appointedMergeBand !== null) {
  640. mergeBand = appointedMergeBand;
  641. }
  642. if (sheetBorder[borderStr] && sheetBorder[borderStr][JV.PROP_LINE_WEIGHT] !== undefined) {
  643. rst = sheetBorder[borderStr][JV.PROP_LINE_WEIGHT];
  644. }
  645. if (currentPageMergePos) {
  646. const side = currentPageMergePos[borderStr];
  647. if (side.indexOf(cell[JV.PROP_AREA][borderStr]) >= 0) {
  648. if (needFurtherChk) {
  649. const topSide = currentPageMergePos[JV.PROP_TOP];
  650. const bottomSide = currentPageMergePos[JV.PROP_BOTTOM];
  651. for (let i = 0; i < topSide.length; i++) {
  652. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= topSide[i]) {
  653. if (cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= bottomSide[i]) {
  654. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  655. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  656. break;
  657. }
  658. } else {
  659. break;
  660. }
  661. }
  662. } else {
  663. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  664. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  665. }
  666. }
  667. } else {
  668. if (mergeBorder && mergeBorder[borderStr] && cell[JV.PROP_AREA][borderStr] === mergeBorder[borderStr]) {
  669. const destStyle = pageData[JV.NODE_STYLE_COLLECTION][mergeBand[JV.PROP_STYLE][JV.PROP_ID]];
  670. if (needFurtherChk) {
  671. if (cell[JV.PROP_AREA][JV.PROP_TOP] >= mergeBorder[JV.PROP_TOP] &&
  672. cell[JV.PROP_AREA][JV.PROP_BOTTOM] <= mergeBorder[JV.PROP_BOTTOM]) {
  673. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  674. }
  675. } else {
  676. rst = destStyle[borderStr][JV.PROP_LINE_WEIGHT];
  677. }
  678. }
  679. }
  680. return parseInt(rst);
  681. };
  682. const private_checkBorder = function(cell, border, sheetBorder) {
  683. let rst = true;
  684. const borderLineWidths = [];
  685. const sheetBorderLineWidths = [];
  686. borderLineWidths.push(border[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]);
  687. borderLineWidths.push(border[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]);
  688. borderLineWidths.push(border[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]);
  689. borderLineWidths.push(border[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]);
  690. if (sheetBorder[JV.PROP_LEFT] && sheetBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT]) {
  691. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true));
  692. } else {
  693. sheetBorderLineWidths.push(0);
  694. }
  695. if (sheetBorder[JV.PROP_RIGHT] && sheetBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT]) {
  696. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true));
  697. } else {
  698. sheetBorderLineWidths.push(0);
  699. }
  700. if (sheetBorder[JV.PROP_TOP] && sheetBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT]) {
  701. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false));
  702. } else {
  703. sheetBorderLineWidths.push(0);
  704. }
  705. if (sheetBorder[JV.PROP_BOTTOM] && sheetBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT]) {
  706. sheetBorderLineWidths.push(private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false));
  707. } else {
  708. sheetBorderLineWidths.push(0);
  709. }
  710. for (let i = 0; i < 4; i++) {
  711. if (borderLineWidths[i] != sheetBorderLineWidths[i]) {
  712. rst = false;
  713. break;
  714. }
  715. }
  716. return rst;
  717. };
  718. const private_getIniBorder = function() {
  719. const rst = {};
  720. rst[JV.PROP_LEFT] = {};
  721. rst[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = 0;
  722. rst[JV.PROP_RIGHT] = {};
  723. rst[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = 0;
  724. rst[JV.PROP_TOP] = {};
  725. rst[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = 0;
  726. rst[JV.PROP_BOTTOM] = {};
  727. rst[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = 0;
  728. return rst;
  729. };
  730. const private_getBorderId = function(cell) {
  731. let rst = 0;
  732. let hasBorder = false;
  733. if (!(stylesObj.borders)) {
  734. stylesObj.borders = [];
  735. }
  736. const sheetBorder = pageData[JV.NODE_STYLE_COLLECTION][cell.style];
  737. const mergedBorder = private_getIniBorder();
  738. mergedBorder[JV.PROP_LEFT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_LEFT, true);
  739. mergedBorder[JV.PROP_RIGHT][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_RIGHT, true);
  740. mergedBorder[JV.PROP_TOP][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_TOP, false);
  741. mergedBorder[JV.PROP_BOTTOM][JV.PROP_LINE_WEIGHT] = private_chkAndGetMergeLine(cell, sheetBorder, JV.PROP_BOTTOM, false);
  742. for (let i = 0; i < stylesObj.borders.length; i++) {
  743. const border = stylesObj.borders[i];
  744. if (private_checkBorder(cell, border, mergedBorder)) {
  745. hasBorder = true;
  746. rst = i;
  747. break;
  748. }
  749. }
  750. if (!hasBorder) {
  751. stylesObj.borders.push(mergedBorder);
  752. rst = stylesObj.borders.length - 1;
  753. }
  754. return rst;
  755. };
  756. const private_checkControl = function(cellControl, sheetControl) {
  757. let rst = true;
  758. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  759. if (cellControl[JV.CONTROL_PROPS[i]] !== sheetControl[JV.CONTROL_PROPS[i]]) {
  760. rst = false;
  761. break;
  762. }
  763. }
  764. return rst;
  765. };
  766. const private_getStyleId = function(cell) {
  767. let rst = 1;
  768. let hasStyle = false;
  769. if (!(stylesObj.cellXfs)) stylesObj.cellXfs = [];
  770. const fontId = private_getFontId(cell);
  771. let fontAngle = 0;
  772. if (typeof cell[JV.PROP_FONT] === 'string') {
  773. fontAngle = parseInt(pageData[JV.NODE_FONT_COLLECTION][cell[JV.PROP_FONT]].FontAngle);
  774. } else {
  775. fontAngle = parseInt(cell[JV.PROP_FONT].FontAngle);
  776. }
  777. const borderId = private_getBorderId(cell);
  778. let cellControl = null;
  779. if (typeof cell[JV.PROP_CONTROL] === 'string') {
  780. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][cell[JV.PROP_CONTROL]];
  781. } else {
  782. cellControl = cell[JV.PROP_CONTROL];
  783. }
  784. for (let i = 0; i < stylesObj.cellXfs.length; i++) {
  785. const sheetControl = stylesObj.cellXfs[i];
  786. if (sheetControl.fontId === fontId && sheetControl.borderId === borderId) {
  787. if (private_checkControl(cellControl, sheetControl)) {
  788. rst = i;
  789. hasStyle = true;
  790. break;
  791. }
  792. }
  793. }
  794. if (!hasStyle) {
  795. const sheetControl = {};
  796. sheetControl.fontId = fontId;
  797. sheetControl.borderId = borderId;
  798. sheetControl.fontAngle = fontAngle;
  799. for (let i = 0; i < JV.CONTROL_PROPS.length; i++) {
  800. sheetControl[JV.CONTROL_PROPS[i]] = cellControl[JV.CONTROL_PROPS[i]];
  801. }
  802. stylesObj.cellXfs.push(sheetControl);
  803. rst = stylesObj.cellXfs.length - 1;
  804. }
  805. return rst;
  806. };
  807. const private_setCols = function() {
  808. // remark: 1 excel unit width = 2.117 mm
  809. rst.push('<cols>');
  810. let w = 0;
  811. for (let i = 1; i < xPos.length - 1; i++) {
  812. w = (xPos[i + 1] - xPos[i]) / DPI * 25.4 / 2.117;
  813. w = Math.round(w * 1000) / 1000;
  814. rst.push('<col min="' + i + '" max="' + i + '" width="' + w + '" customWidth="1"/>');
  815. }
  816. rst.push('<col min="' + (xPos.length - 1) + '" max="' + (xPos.length - 1) + '" width="' + 10 + '" customWidth="1"/>');
  817. rst.push('</cols>');
  818. };
  819. const private_setMergedCells = function() {
  820. let cnt = 0;
  821. rst.push('<mergeCells count="?">');
  822. const startIdx = rst.length - 1;
  823. const self_setMergedCells = function(theData, theYPos, offsetY) {
  824. for (let i = 0; i < theData[JV.PROP_CELLS].length; i++) {
  825. const cell = theData[JV.PROP_CELLS][i];
  826. const idxR = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_RIGHT]);
  827. const idxL = xPos.indexOf(cell[JV.PROP_AREA][JV.PROP_LEFT]);
  828. const idxB = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_BOTTOM]);
  829. const idxT = theYPos.indexOf(cell[JV.PROP_AREA][JV.PROP_TOP]);
  830. if (idxR - idxL > 1 || idxB - idxT > 1) {
  831. rst.push('<mergeCell ref="' + private_getCellIdxStr(idxL - 1) + (idxT + offsetY) + ':' + private_getCellIdxStr(idxR - 2) + (idxB - 1 + offsetY) + '"/>');
  832. cnt++;
  833. }
  834. }
  835. };
  836. if (sheetData) {
  837. self_setMergedCells(sheetData, yPos, 0);
  838. } else {
  839. let osY = 0;
  840. for (let i = 0; i < pageData.items.length; i++) {
  841. const shtItemData = pageData.items[i];
  842. const tmpPos = yMultiPos[i];
  843. self_setMergedCells(shtItemData, tmpPos, osY);
  844. osY += tmpPos.length - 2;
  845. }
  846. }
  847. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  848. rst.push('</mergeCells>');
  849. };
  850. const private_setSheetData = function() {
  851. // remark: 1 excel unit height = 0.3612 mm
  852. rst.push('<sheetData>');
  853. const spanX = xPos.length - 2;
  854. let cellIdx = 0;
  855. let h = 0;
  856. const self_create_cell = function(cells, rows, theYPos) {
  857. let rowIdx1 = 0;
  858. let colIdx1 = 0;
  859. let rowIdx2 = 0;
  860. let colIdx2 = 0;
  861. for (let cIdx = 0; cIdx < cells.length; cIdx++) {
  862. const styleIdx = private_getStyleId(cells[cIdx]);
  863. rowIdx1 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  864. colIdx1 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  865. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  866. cellObj.s = styleIdx;
  867. cellObj.isBlank = false;
  868. if (!(strUtil.isEmptyString(cells[cIdx][JV.PROP_VALUE]))) {
  869. const valIdx = private_getSharedStrIdx(cells[cIdx][JV.PROP_VALUE]);
  870. cellObj.v = valIdx;
  871. }
  872. rowIdx2 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  873. colIdx2 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  874. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  875. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  876. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  877. if (i === 0 && j === 0) continue;
  878. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  879. cellObj.s = styleIdx;
  880. cellObj.isBlank = true;
  881. }
  882. }
  883. }
  884. }
  885. };
  886. const self_setDataEx = function(theShtData, theYPos, rowOffset) {
  887. const rows = [];
  888. // 1. build full set of blank rows/cells
  889. for (let i = 1; i < theYPos.length - 1; i++) {
  890. const rowObj = {};
  891. h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  892. h = Math.round(h * 1000) / 1000;
  893. rowObj.height = h;
  894. rowObj.r = i + rowOffset;
  895. rowObj.items = [];
  896. rows.push(rowObj);
  897. for (let j = 1; j < xPos.length - 1; j++) {
  898. const colIdxStr = private_getCellIdxStr(j - 1);
  899. const cellObj = {};
  900. cellObj.r = colIdxStr + (i + rowOffset);
  901. cellObj.s = 0;
  902. cellObj.isBlank = true;
  903. rows[i - 1].items.push(cellObj);
  904. }
  905. }
  906. // 2. then fill up the cell style-ids and values
  907. self_create_cell(theShtData[JV.PROP_CELLS], rows, theYPos);
  908. self_create_cell(theShtData[JV.PROP_SIGNATURE_DATE_CELLS], rows, theYPos);
  909. self_create_cell(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS], rows, theYPos);
  910. // 3. then fill up rst
  911. for (let i = 0; i < rows.length; i++) {
  912. rst.push('<row r="' + (i + 1 + rowOffset) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  913. for (let j = 0; j < rows[i].items.length; j++) {
  914. const cellObj = rows[i].items[j];
  915. if (cellObj.v === undefined) {
  916. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  917. } else {
  918. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  919. rst.push('<v>' + cellObj.v + '</v>');
  920. rst.push('</c>');
  921. }
  922. }
  923. rst.push('</row>');
  924. }
  925. // 4. maybe need to dispose the memory
  926. // ...
  927. };
  928. if (sheetData) {
  929. // current sheet data
  930. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  931. currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
  932. if (hasSignature && sheetData[JV.PROP_SIGNATURE_CELLS] && sheetData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  933. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  934. _createDummyCell(sheetData[JV.PROP_SIGNATURE_CELLS], sheetData);
  935. }
  936. self_setDataEx(sheetData, yPos, 0);
  937. } else {
  938. // total data in one sheet
  939. let rowOffset = 0;
  940. for (let i = 0; i < pageData.items.length; i++) {
  941. const shtItemData = pageData.items[i];
  942. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  943. currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
  944. const tmpPos = yMultiPos[i];
  945. cellIdx = 0;
  946. if (hasSignature && shtItemData[JV.PROP_SIGNATURE_CELLS] && shtItemData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  947. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  948. _createDummyCell(shtItemData[JV.PROP_SIGNATURE_CELLS], shtItemData);
  949. }
  950. self_setDataEx(shtItemData, tmpPos, rowOffset);
  951. rowOffset += tmpPos.length - 2;
  952. }
  953. }
  954. rst.push('</sheetData>');
  955. };
  956. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  957. // console.log('yMultiPos');
  958. // console.log(yMultiPos);
  959. rst.push(dftHeadXml + '\r\n');
  960. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  961. const colStr = private_getCellIdxStr(xPos.length - 3);
  962. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  963. if (isFirstSheet) {
  964. rst.push('<sheetViews><sheetView showGridLines="1" tabSelected="1" workbookViewId="0">');
  965. } else {
  966. rst.push('<sheetViews><sheetView showGridLines="1" workbookViewId="0">');
  967. }
  968. // rst.push('<selection sqref="A1:' + colStr + '1"/>');
  969. rst.push('<selection sqref="A1:A1"/>');
  970. rst.push('</sheetView></sheetViews>');
  971. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  972. private_setCols();
  973. private_setSheetData();
  974. private_setMergedCells();
  975. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  976. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  977. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  978. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  979. const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  980. let pStr = '';
  981. if (paperSizeIdx >= 0) {
  982. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  983. }
  984. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  985. if (currentPageMergePos) {
  986. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  987. }
  988. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  989. rst.push('<headerFooter alignWithMargins="0"/>');
  990. // 插入分页符---------------------------
  991. if (pageData.pageBreaks) {
  992. let pgBrks = [];
  993. if (sheetData !== null) {
  994. pgBrks = pageData.pageBreaks[pgBrkIdx];
  995. } else {
  996. pgBrks = pageData.pageBreaks[0];
  997. }
  998. if (pgBrks.length > 1) {
  999. rst.push('<rowBreaks count="' + pgBrks.length + '" manualBreakCount="' + pgBrks.length + '" >');
  1000. for (let pbi = 0; pbi < pgBrks.length; pbi++) {
  1001. rst.push('<brk id="' + pgBrks[pbi] + '" max="16383" man="1" />');
  1002. }
  1003. rst.push('</rowBreaks>');
  1004. }
  1005. }
  1006. // 插入分页符结束---------------------------
  1007. // 图形要放在最后,否则excel会报错(但WPS不会)
  1008. if (hasSignature && signSheetIdxArr[sheetIdx]) {
  1009. // let rIdx = 1;
  1010. // for (let ssIdx = 0; ssIdx < signSheetIdxArr.length; ssIdx++) {
  1011. // if (signSheetIdxArr[ssIdx]) {
  1012. // if (ssIdx < sheetIdx) rIdx++
  1013. // else break;
  1014. // }
  1015. // }
  1016. rst.push('<drawing r:id="rId1"/>');
  1017. }
  1018. rst.push('</worksheet>');
  1019. return rst;
  1020. }
  1021. function writeWorkSheetRels(signSheetIdx) {
  1022. const rst = [];
  1023. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1024. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1025. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + signSheetIdx + '.xml"/>');
  1026. rst.push('</Relationships>');
  1027. return rst;
  1028. }
  1029. function writeImage(path, pic, baseDir) {
  1030. let rst = null;
  1031. if (pic) {
  1032. // 这里pic的数据就是base64的,不能是其他
  1033. rst = base64ToBuffer(pic);
  1034. // fs.createReadStream
  1035. } else if (path) {
  1036. const filePath = baseDir + '/app' + path;
  1037. // console.log(filePath);
  1038. rst = fs.readFileSync(filePath);
  1039. // console.log(rst);
  1040. }
  1041. return rst;
  1042. }
  1043. function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr) {
  1044. const rst = [];
  1045. // console.log('isSinglePage: ' + isSinglePage);
  1046. if (isSinglePage) {
  1047. rst.push(writeDrawing(pageData, null, signKeyArr[0]));
  1048. } else {
  1049. for (let i = 0; i < pageData.items.length; i++) {
  1050. if (signSheetIdxArr[i]) {
  1051. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr[i]));
  1052. }
  1053. }
  1054. }
  1055. return rst;
  1056. }
  1057. function writeDrawing(pageData, sheetData, subSignKeyArr) {
  1058. const rst = [];
  1059. const xPos = [];
  1060. const yPos = [];
  1061. const yMultiPos = [];
  1062. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  1063. let cellControl;
  1064. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  1065. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  1066. } else {
  1067. cellControl = signCell[JV.PROP_CONTROL];
  1068. }
  1069. const area = getProperSignatureArea(signCell, cellControl);
  1070. // 1. from
  1071. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  1072. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1073. let colIdx = xPos.indexOf(area[0]);
  1074. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1075. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1076. colIdx--;
  1077. rowIdx--;
  1078. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  1079. rst.push('<xdr:from>');
  1080. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1081. rst.push('<xdr:colOff>0</xdr:colOff>');
  1082. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1083. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1084. rst.push('</xdr:from>');
  1085. // 2. to
  1086. colIdx = xPos.indexOf(area[2]);
  1087. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1088. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1089. colIdx--;
  1090. rowIdx--;
  1091. rst.push('<xdr:to>');
  1092. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1093. rst.push('<xdr:colOff>0</xdr:colOff>');
  1094. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1095. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1096. rst.push('</xdr:to>');
  1097. // 3. pic
  1098. rst.push('<xdr:pic>');
  1099. // 3.1
  1100. rst.push('<xdr:nvPicPr>');
  1101. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1102. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1103. rst.push('</xdr:nvPicPr>');
  1104. // 3.2
  1105. rst.push('<xdr:blipFill>');
  1106. // 重点!!!
  1107. const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1108. // 3.2.1
  1109. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1110. rst.push('<a:extLst>');
  1111. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1112. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1113. rst.push('</a:ext>');
  1114. rst.push('</a:extLst>');
  1115. rst.push('</a:blip>');
  1116. // 3.2.2
  1117. rst.push('<a:srcRect/>');
  1118. // 3.2.3
  1119. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1120. rst.push('</xdr:blipFill>');
  1121. // 3.3
  1122. rst.push('<xdr:spPr bwMode="auto">');
  1123. // 3.3.1
  1124. rst.push('<a:xfrm>');
  1125. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1126. rst.push('<a:off x="0" y="0"/>');
  1127. rst.push('<a:ext cx="0" cy="0"/>');
  1128. rst.push('</a:xfrm>');
  1129. // 3.3.2
  1130. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1131. // 3.3.3
  1132. rst.push('<a:noFill/>');
  1133. // 3.3.4
  1134. rst.push('<a:extLst>');
  1135. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1136. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1137. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1138. rst.push('</a14:hiddenFill>');
  1139. rst.push('</a:ext>');
  1140. rst.push('</a:extLst>');
  1141. // 收尾
  1142. rst.push('</xdr:spPr>');
  1143. rst.push('</xdr:pic>');
  1144. // 4. client Data
  1145. rst.push('<xdr:clientData/>');
  1146. rst.push('</xdr:twoCellAnchor>');
  1147. };
  1148. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1149. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1150. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1151. if (sheetData) {
  1152. let startPicIdx = 2;
  1153. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1154. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1155. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1156. startPicIdx++;
  1157. }
  1158. }
  1159. for (const sCell of sheetData[JV.PROP_WATERMARK_CELLS]) {
  1160. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1161. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1162. startPicIdx++;
  1163. }
  1164. }
  1165. } else {
  1166. // total data in one sheet
  1167. let rowOffset = 0;
  1168. for (let i = 0; i < pageData.items.length; i++) {
  1169. const shtItemData = pageData.items[i];
  1170. const tmpPos = yMultiPos[i];
  1171. let startPicIdx = 2;
  1172. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1173. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1174. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1175. startPicIdx++;
  1176. }
  1177. }
  1178. for (const sCell of shtItemData[JV.PROP_WATERMARK_CELLS]) {
  1179. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1180. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1181. startPicIdx++;
  1182. }
  1183. }
  1184. rowOffset += tmpPos.length - 2;
  1185. }
  1186. }
  1187. rst.push('</xdr:wsDr>');
  1188. return rst;
  1189. }
  1190. function writeDrawingsRels(amt, startIdx) {
  1191. const rst = [];
  1192. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1193. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1194. for (let idx = 0; idx < amt; idx++) {
  1195. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1196. }
  1197. rst.push('</Relationships>');
  1198. return rst;
  1199. }
  1200. function mergeProperties(orgObj, newObj) {
  1201. const orgPropArr = [];
  1202. const newPropArr = [];
  1203. for (const p in orgObj) {
  1204. orgPropArr.push(p);
  1205. }
  1206. for (const p in newObj) {
  1207. newPropArr.push(p);
  1208. }
  1209. for (let i = 0; i < newPropArr.length; i++) {
  1210. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1211. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1212. }
  1213. }
  1214. }
  1215. function resetDummuySignature(pageData, roleRel) {
  1216. // 备注:计算草图等其他图形需要额外做些处理
  1217. let dummySignIdx = 0;
  1218. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1219. const page = pageData.items[pageIdx];
  1220. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1221. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1222. if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1223. // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1224. signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1225. dummySignIdx++;
  1226. const roleRelItem = { type: '用户', sign_path: signature.path, signature_name: signature.signature_name };
  1227. roleRel.push(roleRelItem);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. }
  1233. function _createDummyCell(dummyOrgCells, parentPage) {
  1234. for (const signature of dummyOrgCells) {
  1235. const dummyCell = {
  1236. font: 'Content',
  1237. control: 'Default',
  1238. style: 'Default_None',
  1239. Value: '',
  1240. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 },
  1241. };
  1242. if (signature.style) dummyCell.style = signature.style;
  1243. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1244. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1245. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1246. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1247. parentPage[JV.PROP_CELLS].push(dummyCell);
  1248. }
  1249. }
  1250. function createDummySignatureCell(pageData) {
  1251. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1252. const page = pageData.items[pageIdx];
  1253. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1254. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1255. }
  1256. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1257. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1258. }
  1259. if (page[JV.PROP_SIGNATURE_AUDIT_CELLS] && page[JV.PROP_SIGNATURE_AUDIT_CELLS].length > 0) {
  1260. _createDummyCell(page[JV.PROP_SIGNATURE_AUDIT_CELLS], page);
  1261. }
  1262. }
  1263. }
  1264. function _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) {
  1265. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1266. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图、水印等)
  1267. let rst = false;
  1268. let chkRoles = [];
  1269. if (roleRel instanceof Array) {
  1270. chkRoles = roleRel;
  1271. } else {
  1272. if (roleRel) chkRoles.push(roleRel);
  1273. }
  1274. // console.log(chkRoles);
  1275. // console.log(signKeyArr);
  1276. // for (const page of pageData.items) {
  1277. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1278. const page = pageData.items[pageIdx];
  1279. signKeyArr.push([]);
  1280. signPathArr.push([]);
  1281. signSheetIdxArr[pageIdx] = false;
  1282. // console.log('page index: ' + pageIdx);
  1283. // console.log(page[JV.PROP_SIGNATURE_CELLS]);
  1284. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1285. // let dummySignIdx = 0;
  1286. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1287. // if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1288. // // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1289. // signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1290. // dummySignIdx++;
  1291. // rst = true;
  1292. // signSheetIdxArr[pageIdx] = true;
  1293. // }
  1294. if (signKeyArr[pageIdx].indexOf(signature.signature_name) < 0) {
  1295. if (signature.pic) {
  1296. const signPath = { path: null, pic: null };
  1297. signPathArr[pageIdx].push(signPath);
  1298. signPath.pic = signature.pic; // 历史报表
  1299. signKeyArr[pageIdx].push(signature.signature_name);
  1300. rst = true;
  1301. signSheetIdxArr[pageIdx] = true;
  1302. } else {
  1303. for (const role of chkRoles) {
  1304. if (signature.signature_name === role.signature_name) {
  1305. // console.log('signature.signature_name: ' + signature.signature_name);
  1306. if (role.sign_pic) {
  1307. const signPath = { path: null, pic: null };
  1308. signPathArr[pageIdx].push(signPath);
  1309. signPath.pic = role.sign_pic;
  1310. signKeyArr[pageIdx].push(signature.signature_name);
  1311. rst = true;
  1312. signSheetIdxArr[pageIdx] = true;
  1313. } else if (role.sign_path) {
  1314. const signPath = { path: null, pic: null };
  1315. signPathArr[pageIdx].push(signPath);
  1316. signPath.path = role.sign_path;
  1317. signKeyArr[pageIdx].push(signature.signature_name);
  1318. rst = true;
  1319. signSheetIdxArr[pageIdx] = true;
  1320. }
  1321. break;
  1322. }
  1323. }
  1324. }
  1325. }
  1326. }
  1327. }
  1328. if (page[JV.PROP_WATERMARK_CELLS] && page[JV.PROP_WATERMARK_CELLS].length > 0) {
  1329. // 水印,单独处理
  1330. const waterMarkCell = page[JV.PROP_WATERMARK_CELLS][0];
  1331. // console.log('waterMarkCell');
  1332. // console.log(waterMarkCell);
  1333. const signPath = { path: null, pic: null };
  1334. signPathArr[pageIdx].push(signPath);
  1335. signPath.pic = waterMarkCell.pic;
  1336. signPath.path = waterMarkCell.path;
  1337. signKeyArr[pageIdx].push(waterMarkCell.signature_name);
  1338. rst = true;
  1339. signSheetIdxArr[pageIdx] = true;
  1340. }
  1341. }
  1342. return rst;
  1343. }
  1344. function base64ToBuffer(dataurl) {
  1345. // 将base64转换为buffer
  1346. const arr = dataurl.split(',');
  1347. // console.log(arr[1]);
  1348. const rst = new Buffer(arr[1], 'base64');
  1349. return rst;
  1350. }
  1351. module.exports = {
  1352. exportExcel: function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, baseDir, roleRel, callback) {
  1353. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1354. if (options === 'true' || options === true) {
  1355. rptOptions.singlePage = true;
  1356. }
  1357. const isSinglePage = rptOptions.singlePage;
  1358. const sheets = [];
  1359. const signKeyArr = [];
  1360. const signPathArr = [];
  1361. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1362. // console.log('in exportExcel!');
  1363. // console.log(roleRel);
  1364. // const hasSignature = (roleRel !== null) ? _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) : false;
  1365. let thisRoleRel = [];
  1366. if (roleRel !== null && roleRel !== undefined) {
  1367. thisRoleRel = roleRel;
  1368. }
  1369. resetDummuySignature(pageData, thisRoleRel); // 把草图转换一下roleRel
  1370. // console.log(thisRoleRel);
  1371. // const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1372. const hasSignature = _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1373. // console.log('signKeyArr');
  1374. // console.log(signKeyArr);
  1375. // console.log('signPathArr');
  1376. // console.log(signPathArr);
  1377. // console.log('hasSignature: ' + hasSignature);
  1378. if (!hasSignature) {
  1379. createDummySignatureCell(pageData);
  1380. }
  1381. if (isSinglePage) {
  1382. // 加分页符(分页符要在外部处理好)-------------------
  1383. _setupPgBrks(pageData);
  1384. // 加分页符结束-------------------
  1385. sheets.push({ sheetName: '全部页' });
  1386. } else {
  1387. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1388. for (let i = 0; i < pageData.items.length; i++) {
  1389. sheets.push({ sheetName: custSheetNames[i] });
  1390. }
  1391. } else {
  1392. for (let i = 0; i < pageData.items.length; i++) {
  1393. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1394. }
  1395. }
  1396. }
  1397. // 1.
  1398. let file = '[Content_Types].xml';
  1399. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1400. const zip = new JSZip();
  1401. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1402. // console.log('step 1');
  1403. // 2.
  1404. const zip_rels = zip.folder('_rels');
  1405. file = '.rels';
  1406. data = writeRootRels();
  1407. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1408. // console.log('step 2');
  1409. // 3.
  1410. const zip_docProps = zip.folder('docProps');
  1411. file = 'app.xml';
  1412. data = writeApp(sheets, isSinglePage);
  1413. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1414. file = 'core.xml';
  1415. data = writeCore();
  1416. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1417. // console.log('step 3');
  1418. // 4.
  1419. const zip_xl = zip.folder('xl');
  1420. file = 'workbook.xml';
  1421. data = writeXlWorkBook(sheets, isSinglePage);
  1422. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1423. const zip_rels2 = zip_xl.folder('_rels');
  1424. file = 'workbook.xml.rels';
  1425. data = writeXlRels(sheets, isSinglePage);
  1426. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1427. // console.log('step 4');
  1428. // 5.
  1429. const zip_theme = zip_xl.folder('theme');
  1430. file = 'theme1.xml';
  1431. data = writeTheme();
  1432. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1433. if (hasSignature) {
  1434. // 5.1 写所有的image
  1435. const zip_media = zip_xl.folder('media');
  1436. let picIdx = 0;
  1437. // console.log(signKeyArr);
  1438. // console.log(signPathArr);
  1439. for (let signIdx = 0; signIdx < signPathArr.length; signIdx++) {
  1440. if (signKeyArr[signIdx].length > 0) {
  1441. for (let pathIdx = 0; pathIdx < signPathArr[signIdx].length; pathIdx++) {
  1442. picIdx++;
  1443. data = writeImage(signPathArr[signIdx][pathIdx].path, signPathArr[signIdx][pathIdx].pic, baseDir);
  1444. file = 'image' + picIdx + '.png';
  1445. zip_media.file(file, data, { compression: 'DEFLATE' });
  1446. }
  1447. }
  1448. }
  1449. // 5.2
  1450. const zip_drawings = zip_xl.folder('drawings');
  1451. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1452. // console.log('isSinglePage: ' + isSinglePage);
  1453. // console.log(data);
  1454. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1455. file = 'drawing' + (psIdx + 1) + '.xml';
  1456. // console.log('drawing1' + (psIdx + 1) + '.xml data');
  1457. // console.log(data);
  1458. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1459. }
  1460. // 5.3
  1461. const zip_drawings_rels = zip_drawings.folder('_rels');
  1462. let relsIdx = 0;
  1463. let relsImgAmt = 0;
  1464. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1465. if (signKeyArr[ssIdx].length > 0) {
  1466. data = writeDrawingsRels(signKeyArr[ssIdx].length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1467. relsIdx++;
  1468. relsImgAmt += signKeyArr[ssIdx].length;
  1469. // console.log('drawing1.xml.rels data');
  1470. // console.log(data);
  1471. file = 'drawing' + relsIdx + '.xml.rels';
  1472. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1473. }
  1474. }
  1475. }
  1476. // console.log('step 5');
  1477. // 6.
  1478. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1479. const sharedStrList = [];
  1480. const stylesObj = {};
  1481. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1482. if (isSinglePage) {
  1483. for (let i = 0; i < 1; i++) {
  1484. file = 'sheet' + (i + 1) + '.xml';
  1485. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1486. }
  1487. } else {
  1488. for (let i = 0; i < data.length; i++) {
  1489. file = 'sheet' + (i + 1) + '.xml';
  1490. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1491. }
  1492. }
  1493. // console.log('step 6');
  1494. // 7.
  1495. if (hasSignature) {
  1496. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1497. if (isSinglePage) {
  1498. data = writeWorkSheetRels(1);
  1499. file = 'sheet1.xml.rels';
  1500. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1501. } else {
  1502. let ssIdx = 0;
  1503. for (let i = 0; i < data.length; i++) {
  1504. if (signSheetIdxArr[i]) {
  1505. ssIdx++;
  1506. const ssData = writeWorkSheetRels(ssIdx);
  1507. file = 'sheet' + (i + 1) + '.xml.rels';
  1508. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1509. }
  1510. }
  1511. }
  1512. }
  1513. file = 'sharedStrings.xml';
  1514. data = writeSharedString(sharedStrList);
  1515. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1516. file = 'styles.xml';
  1517. data = writeStyles(stylesObj, hasSignature);
  1518. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1519. // console.log('step 7');
  1520. if (fName) {
  1521. // let newName = '' + (new Date()).valueOf();
  1522. const newName = uuidV1();
  1523. // console.log('current path: ' + __dirname);
  1524. // const filePath = this.app.baseDir + '/app/public/download/';
  1525. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1526. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 13) + '/public/download/' + newName + '.xlsx'))
  1527. .on('finish', function() {
  1528. // JSZip generates a readable stream with a "end" event,
  1529. // but is piped here in a writable stream which emits a "finish" event.
  1530. console.log(newName + '.xlsx was written.');
  1531. if (callback) callback(null, newName);
  1532. });
  1533. } else {
  1534. // return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  1535. return zip;
  1536. }
  1537. },
  1538. exportExcelInOneBook: function(pageDataArray, paperSize, fName, baseDir, roleRelArr, callback) {
  1539. const me = this;
  1540. const newPageData = {};
  1541. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1542. // -- 简单来说,就是重新包装数据
  1543. try {
  1544. // 1.1 newPageData外围属性
  1545. const newContrl = {};
  1546. const newFont = {};
  1547. const newStyle = {};
  1548. const custMergeBands = [];
  1549. for (let i = 0; i < pageDataArray.length; i++) {
  1550. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1551. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1552. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1553. }
  1554. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1555. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1556. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1557. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1558. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1559. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1560. const sheetNames = [];
  1561. const newPagePos = [];
  1562. for (let i = 0; i < pageDataArray.length; i++) {
  1563. let offsetY = 0;
  1564. const mergeBand = {};
  1565. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1566. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1567. mergeBand[JV.PROP_LEFT] = [];
  1568. mergeBand[JV.PROP_RIGHT] = [];
  1569. mergeBand[JV.PROP_TOP] = [];
  1570. mergeBand[JV.PROP_BOTTOM] = [];
  1571. newPagePos.push(mergeBand);
  1572. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1573. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1574. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1575. // 加分页符(分页符要在外部处理好)-------------------
  1576. _setupPgBrks(pageDataArray[i]);
  1577. // 加分页符结束-------------------
  1578. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1579. let maxY = 0;
  1580. let minY = 100000;
  1581. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  1582. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1583. mergeBand[JV.PROP_TOP].push(pos);
  1584. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1585. mergeBand[JV.PROP_BOTTOM].push(pos);
  1586. } else if (pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER]) {
  1587. let pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1588. mergeBand[JV.PROP_TOP].push(pos);
  1589. pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1590. mergeBand[JV.PROP_BOTTOM].push(pos);
  1591. } else {
  1592. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1593. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1594. }
  1595. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1596. for (const cell of pageDataArray[i].items[j][JV.PROP_CELLS]) {
  1597. // for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1598. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1599. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1600. }
  1601. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1602. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1603. }
  1604. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1605. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1606. }
  1607. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1608. for (const cell of pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS]) {
  1609. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1610. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1611. }
  1612. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1613. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1614. }
  1615. // console.log('before');
  1616. // console.log(cell);
  1617. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1618. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1619. // console.log('after');
  1620. // console.log(cell);
  1621. }
  1622. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1623. for (const cell of pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS]) {
  1624. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1625. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1626. }
  1627. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1628. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1629. }
  1630. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1631. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1632. }
  1633. const bottomGap = Math.round((pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1] - parseFloat(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]) / 2.54) * DPI) - maxY;
  1634. offsetY += (maxY - minY);
  1635. if (bottomGap > 10) {
  1636. offsetY += (bottomGap - 10);
  1637. }
  1638. }
  1639. }
  1640. // 2. newPageData的items属性及相关分页符
  1641. newPageData.items = [];
  1642. newPageData.pageBreaks = [];
  1643. for (let i = 0; i < pageDataArray.length; i++) {
  1644. const pageItem = {};
  1645. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1646. pageItem[JV.PROP_CELLS] = [];
  1647. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1648. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1649. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j][JV.PROP_CELLS][k]);
  1650. }
  1651. }
  1652. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1653. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1654. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS].length; k++) {
  1655. pageItem[JV.PROP_SIGNATURE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS][k]);
  1656. }
  1657. }
  1658. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1659. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1660. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS].length; k++) {
  1661. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS][k]);
  1662. }
  1663. }
  1664. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS] = [];
  1665. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1666. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS].length; k++) {
  1667. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS][k]);
  1668. }
  1669. }
  1670. pageItem[JV.PROP_WATERMARK_CELLS] = [];
  1671. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1672. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS].length; k++) {
  1673. pageItem[JV.PROP_WATERMARK_CELLS].push(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS][k]);
  1674. }
  1675. }
  1676. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1677. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1678. newPageData.items.push(pageItem);
  1679. // 重新分配分页符()------------------
  1680. if (pageDataArray[i].pageBreaks.length === 1) {
  1681. newPageData.pageBreaks.push(pageDataArray[i].pageBreaks[0]);
  1682. } else {
  1683. newPageData.pageBreaks.push([]);
  1684. }
  1685. // 重新分配分页符结束------------------
  1686. }
  1687. // 3. everything is ok, then call me
  1688. // let roleRel = null; // 未来调用的时候,这个属性要从外面给!!!
  1689. // roleRelArr
  1690. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, baseDir, roleRelArr, callback);
  1691. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1692. } catch (e) {
  1693. console.log(e);
  1694. }
  1695. },
  1696. };