rpt_excel_util.js 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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(cells, theYPos, offsetY) {
  824. for (let i = 0; i < cells.length; i++) {
  825. const cell = 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. self_setMergedCells(sheetData[JV.PROP_CELLS], yPos, 0);
  839. if (sheetData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_DATE_CELLS], yPos, 0);
  840. if (sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(sheetData[JV.PROP_SIGNATURE_AUDIT_CELLS], yPos, 0);
  841. } else {
  842. let osY = 0;
  843. for (let i = 0; i < pageData.items.length; i++) {
  844. const shtItemData = pageData.items[i];
  845. const tmpPos = yMultiPos[i];
  846. // self_setMergedCells(shtItemData, tmpPos, osY);
  847. self_setMergedCells(shtItemData[JV.PROP_CELLS], tmpPos, osY);
  848. if (shtItemData[JV.PROP_SIGNATURE_DATE_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_DATE_CELLS], tmpPos, osY);
  849. if (shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS]) self_setMergedCells(shtItemData[JV.PROP_SIGNATURE_AUDIT_CELLS], tmpPos, osY);
  850. osY += tmpPos.length - 2;
  851. }
  852. }
  853. rst[startIdx] = '<mergeCells count="' + cnt + '">';
  854. rst.push('</mergeCells>');
  855. };
  856. const private_setSheetData = function() {
  857. // remark: 1 excel unit height = 0.3612 mm
  858. rst.push('<sheetData>');
  859. const spanX = xPos.length - 2;
  860. let cellIdx = 0;
  861. let h = 0;
  862. const self_create_cell = function(cells, rows, theYPos) {
  863. let rowIdx1 = 0;
  864. let colIdx1 = 0;
  865. let rowIdx2 = 0;
  866. let colIdx2 = 0;
  867. for (let cIdx = 0; cIdx < cells.length; cIdx++) {
  868. const styleIdx = private_getStyleId(cells[cIdx]);
  869. rowIdx1 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_TOP]);
  870. colIdx1 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_LEFT]);
  871. let cellObj = rows[rowIdx1 - 1].items[colIdx1 - 1];
  872. cellObj.s = styleIdx;
  873. cellObj.isBlank = false;
  874. if (!(strUtil.isEmptyString(cells[cIdx][JV.PROP_VALUE]))) {
  875. const valIdx = private_getSharedStrIdx(cells[cIdx][JV.PROP_VALUE]);
  876. cellObj.v = valIdx;
  877. }
  878. rowIdx2 = theYPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_BOTTOM]);
  879. colIdx2 = xPos.indexOf(cells[cIdx][JV.PROP_AREA][JV.PROP_RIGHT]);
  880. if ((rowIdx2 - rowIdx1 > 1) || (colIdx2 - colIdx1 > 1)) {
  881. for (let i = 0; i < rowIdx2 - rowIdx1; i++) {
  882. for (let j = 0; j < colIdx2 - colIdx1; j++) {
  883. if (i === 0 && j === 0) continue;
  884. cellObj = rows[rowIdx1 - 1 + i].items[colIdx1 - 1 + j];
  885. cellObj.s = styleIdx;
  886. cellObj.isBlank = true;
  887. }
  888. }
  889. }
  890. }
  891. };
  892. const self_setDataEx = function(theShtData, theYPos, rowOffset) {
  893. const rows = [];
  894. // 1. build full set of blank rows/cells
  895. for (let i = 1; i < theYPos.length - 1; i++) {
  896. const rowObj = {};
  897. h = (theYPos[i + 1] - theYPos[i]) / DPI * 25.4 / 0.3612;
  898. h = Math.round(h * 1000) / 1000;
  899. rowObj.height = h;
  900. rowObj.r = i + rowOffset;
  901. rowObj.items = [];
  902. rows.push(rowObj);
  903. for (let j = 1; j < xPos.length - 1; j++) {
  904. const colIdxStr = private_getCellIdxStr(j - 1);
  905. const cellObj = {};
  906. cellObj.r = colIdxStr + (i + rowOffset);
  907. cellObj.s = 0;
  908. cellObj.isBlank = true;
  909. rows[i - 1].items.push(cellObj);
  910. }
  911. }
  912. // 2. then fill up the cell style-ids and values
  913. self_create_cell(theShtData[JV.PROP_CELLS], rows, theYPos);
  914. self_create_cell(theShtData[JV.PROP_SIGNATURE_DATE_CELLS], rows, theYPos);
  915. self_create_cell(theShtData[JV.PROP_SIGNATURE_AUDIT_CELLS], rows, theYPos);
  916. // 3. then fill up rst
  917. for (let i = 0; i < rows.length; i++) {
  918. rst.push('<row r="' + (i + 1 + rowOffset) + '" spans="1:' + spanX + '" ht="' + rows[i].height + '" customHeight="1">');
  919. for (let j = 0; j < rows[i].items.length; j++) {
  920. const cellObj = rows[i].items[j];
  921. if (cellObj.v === undefined) {
  922. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '"/>');
  923. } else {
  924. rst.push('<c r="' + cellObj.r + '" s="' + cellObj.s + '" t="s">');
  925. rst.push('<v>' + cellObj.v + '</v>');
  926. rst.push('</c>');
  927. }
  928. }
  929. rst.push('</row>');
  930. }
  931. // 4. maybe need to dispose the memory
  932. // ...
  933. };
  934. if (sheetData) {
  935. // current sheet data
  936. currentPageMergePos = sheetData[JV.PAGE_SPECIAL_MERGE_POS];
  937. currentMergeBorder = sheetData[JV.PROP_PAGE_MERGE_BORDER];
  938. if (hasSignature && sheetData[JV.PROP_SIGNATURE_CELLS] && sheetData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  939. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  940. _createDummyCell(sheetData[JV.PROP_SIGNATURE_CELLS], sheetData);
  941. }
  942. self_setDataEx(sheetData, yPos, 0);
  943. } else {
  944. // total data in one sheet
  945. let rowOffset = 0;
  946. for (let i = 0; i < pageData.items.length; i++) {
  947. const shtItemData = pageData.items[i];
  948. currentPageMergePos = shtItemData[JV.PAGE_SPECIAL_MERGE_POS];
  949. currentMergeBorder = shtItemData[JV.PROP_PAGE_MERGE_BORDER];
  950. const tmpPos = yMultiPos[i];
  951. cellIdx = 0;
  952. if (hasSignature && shtItemData[JV.PROP_SIGNATURE_CELLS] && shtItemData[JV.PROP_SIGNATURE_CELLS].length > 0) {
  953. // 有签名情况下,还是有必要创建一个dummy cell的(画框用)
  954. _createDummyCell(shtItemData[JV.PROP_SIGNATURE_CELLS], shtItemData);
  955. }
  956. self_setDataEx(shtItemData, tmpPos, rowOffset);
  957. rowOffset += tmpPos.length - 2;
  958. }
  959. }
  960. rst.push('</sheetData>');
  961. };
  962. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  963. // console.log('yMultiPos');
  964. // console.log(yMultiPos);
  965. rst.push(dftHeadXml + '\r\n');
  966. rst.push('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
  967. const colStr = private_getCellIdxStr(xPos.length - 3);
  968. rst.push('<dimension ref="A1:' + colStr + '' + yPos.length + '"/>');
  969. if (isFirstSheet) {
  970. rst.push('<sheetViews><sheetView showGridLines="1" tabSelected="1" workbookViewId="0">');
  971. } else {
  972. rst.push('<sheetViews><sheetView showGridLines="1" workbookViewId="0">');
  973. }
  974. // rst.push('<selection sqref="A1:' + colStr + '1"/>');
  975. rst.push('<selection sqref="A1:A1"/>');
  976. rst.push('</sheetView></sheetViews>');
  977. rst.push('<sheetFormatPr defaultRowHeight="13.5"/>');
  978. private_setCols();
  979. private_setSheetData();
  980. private_setMergedCells();
  981. rst.push('<phoneticPr fontId="1" type="noConversion"/>');
  982. // rst.push('<pageMargins left="0.315" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  983. rst.push('<pageMargins left="' + (parseFloat(pageData[JV.NODE_PAGE_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) * 0.39375) +
  984. '" right="0.215" top="0.315" bottom="0.315" header="0" footer="0"/>');
  985. const paperSizeIdx = JV.PAGES_SIZE_STR.indexOf(paperSize);
  986. let pStr = '';
  987. if (paperSizeIdx >= 0) {
  988. pStr = 'paperSize="' + JV.PAGES_SIZE_IDX[paperSizeIdx] + '"';
  989. }
  990. let orientationStr = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] > pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  991. if (currentPageMergePos) {
  992. orientationStr = (currentPageMergePos[JV.NODE_PAGE_SIZE][0] > currentPageMergePos[JV.NODE_PAGE_SIZE][1]) ? 'landscape' : 'portrait';
  993. }
  994. rst.push('<pageSetup ' + pStr + ' fitToWidth="0" fitToHeight="0" orientation="' + orientationStr + '" />');
  995. rst.push('<headerFooter alignWithMargins="0"/>');
  996. // 插入分页符---------------------------
  997. if (pageData.pageBreaks) {
  998. let pgBrks = [];
  999. if (sheetData !== null) {
  1000. pgBrks = pageData.pageBreaks[pgBrkIdx];
  1001. } else {
  1002. pgBrks = pageData.pageBreaks[0];
  1003. }
  1004. if (pgBrks.length > 1) {
  1005. rst.push('<rowBreaks count="' + pgBrks.length + '" manualBreakCount="' + pgBrks.length + '" >');
  1006. for (let pbi = 0; pbi < pgBrks.length; pbi++) {
  1007. rst.push('<brk id="' + pgBrks[pbi] + '" max="16383" man="1" />');
  1008. }
  1009. rst.push('</rowBreaks>');
  1010. }
  1011. }
  1012. // 插入分页符结束---------------------------
  1013. // 图形要放在最后,否则excel会报错(但WPS不会)
  1014. if (hasSignature && signSheetIdxArr[sheetIdx]) {
  1015. // let rIdx = 1;
  1016. // for (let ssIdx = 0; ssIdx < signSheetIdxArr.length; ssIdx++) {
  1017. // if (signSheetIdxArr[ssIdx]) {
  1018. // if (ssIdx < sheetIdx) rIdx++
  1019. // else break;
  1020. // }
  1021. // }
  1022. rst.push('<drawing r:id="rId1"/>');
  1023. }
  1024. rst.push('</worksheet>');
  1025. return rst;
  1026. }
  1027. function writeWorkSheetRels(signSheetIdx) {
  1028. const rst = [];
  1029. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1030. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1031. rst.push('<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing' + signSheetIdx + '.xml"/>');
  1032. rst.push('</Relationships>');
  1033. return rst;
  1034. }
  1035. function writeImage(path, pic, baseDir) {
  1036. let rst = null;
  1037. if (pic) {
  1038. // 这里pic的数据就是base64的,不能是其他
  1039. rst = base64ToBuffer(pic);
  1040. // fs.createReadStream
  1041. } else if (path) {
  1042. const filePath = baseDir + '/app' + path;
  1043. // console.log(filePath);
  1044. rst = fs.readFileSync(filePath);
  1045. // console.log(rst);
  1046. }
  1047. return rst;
  1048. }
  1049. function writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr) {
  1050. const rst = [];
  1051. // console.log('isSinglePage: ' + isSinglePage);
  1052. if (isSinglePage) {
  1053. // console.log('singlePage! ');
  1054. rst.push(writeDrawing(pageData, null, signKeyArr[0]));
  1055. } else {
  1056. // console.log('pageData! ');
  1057. for (let i = 0; i < pageData.items.length; i++) {
  1058. if (signSheetIdxArr[i]) {
  1059. // console.log('signKeyArr[' + i + ']');
  1060. // console.log(signKeyArr[i]);
  1061. rst.push(writeDrawing(pageData, pageData.items[i], signKeyArr[i]));
  1062. }
  1063. }
  1064. }
  1065. return rst;
  1066. }
  1067. function writeDrawing(pageData, sheetData, subSignKeyArr) {
  1068. const rst = [];
  1069. const xPos = [];
  1070. const yPos = [];
  1071. const yMultiPos = [];
  1072. const private_setSheetDrawingCellData = function(signCell, theYPos, startPicIdx, offsetRow) {
  1073. let cellControl;
  1074. if (typeof signCell[JV.PROP_CONTROL] === 'string') {
  1075. cellControl = pageData[JV.NODE_CONTROL_COLLECTION][signCell[JV.PROP_CONTROL]];
  1076. } else {
  1077. cellControl = signCell[JV.PROP_CONTROL];
  1078. }
  1079. const area = getProperSignatureArea(signCell, cellControl);
  1080. // 1. from
  1081. // let colIdx = xPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_LEFT]);
  1082. // let rowIdx = theYPos.indexOf(signCell[JV.PROP_AREA][JV.PROP_TOP]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1083. let colIdx = xPos.indexOf(area[0]);
  1084. let rowIdx = theYPos.indexOf(area[1]) + offsetRow; // 这里要考虑Row的偏移量(多页在同同一个sheet输出)
  1085. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1086. colIdx--;
  1087. rowIdx--;
  1088. rst.push('<xdr:twoCellAnchor editAs="oneCell">');
  1089. rst.push('<xdr:from>');
  1090. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1091. rst.push('<xdr:colOff>0</xdr:colOff>');
  1092. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1093. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1094. rst.push('</xdr:from>');
  1095. // 2. to
  1096. colIdx = xPos.indexOf(area[2]);
  1097. rowIdx = theYPos.indexOf(area[3]) + offsetRow;
  1098. // 因为 xPos, theYPos的首位是0,所以还需要减1
  1099. colIdx--;
  1100. rowIdx--;
  1101. rst.push('<xdr:to>');
  1102. rst.push('<xdr:col>' + colIdx + '</xdr:col>');
  1103. rst.push('<xdr:colOff>0</xdr:colOff>');
  1104. rst.push('<xdr:row>' + rowIdx + '</xdr:row>');
  1105. rst.push('<xdr:rowOff>0</xdr:rowOff>');
  1106. rst.push('</xdr:to>');
  1107. // 3. pic
  1108. rst.push('<xdr:pic>');
  1109. // 3.1
  1110. rst.push('<xdr:nvPicPr>');
  1111. rst.push('<xdr:cNvPr id="' + startPicIdx + '" name="图片 ' + startPicIdx + '"/>');
  1112. rst.push('<xdr:cNvPicPr><a:picLocks noChangeAspect="1" noChangeArrowheads="1"/></xdr:cNvPicPr>');
  1113. rst.push('</xdr:nvPicPr>');
  1114. // 3.2
  1115. rst.push('<xdr:blipFill>');
  1116. // 重点!!!
  1117. const picIdx = subSignKeyArr.indexOf(signCell.signature_name) + 1;
  1118. // 3.2.1
  1119. rst.push('<a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId' + picIdx + '" cstate="print">');
  1120. rst.push('<a:extLst>');
  1121. rst.push('<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">'); // 参考excel本身,都是固定的uri
  1122. rst.push('<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>');
  1123. rst.push('</a:ext>');
  1124. rst.push('</a:extLst>');
  1125. rst.push('</a:blip>');
  1126. // 3.2.2
  1127. rst.push('<a:srcRect/>');
  1128. // 3.2.3
  1129. rst.push('<a:stretch><a:fillRect/></a:stretch>');
  1130. rst.push('</xdr:blipFill>');
  1131. // 3.3
  1132. rst.push('<xdr:spPr bwMode="auto">');
  1133. // 3.3.1
  1134. rst.push('<a:xfrm>');
  1135. // 以下经过Excel及WPS的测试,这数据好像没什么意义,都设为0
  1136. rst.push('<a:off x="0" y="0"/>');
  1137. rst.push('<a:ext cx="0" cy="0"/>');
  1138. rst.push('</a:xfrm>');
  1139. // 3.3.2
  1140. rst.push('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>');
  1141. // 3.3.3
  1142. rst.push('<a:noFill/>');
  1143. // 3.3.4
  1144. rst.push('<a:extLst>');
  1145. rst.push('<a:ext uri="{909E8E84-426E-40DD-AFC4-6F175D3DCCD1}">');
  1146. rst.push('<a14:hiddenFill xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main">');
  1147. rst.push('<a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill>');
  1148. rst.push('</a14:hiddenFill>');
  1149. rst.push('</a:ext>');
  1150. rst.push('</a:extLst>');
  1151. // 收尾
  1152. rst.push('</xdr:spPr>');
  1153. rst.push('</xdr:pic>');
  1154. // 4. client Data
  1155. rst.push('<xdr:clientData/>');
  1156. rst.push('</xdr:twoCellAnchor>');
  1157. };
  1158. preAnalyzePos(pageData, sheetData, xPos, yPos, yMultiPos);
  1159. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1160. rst.push('<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">');
  1161. // console.log('subSignKeyArr');
  1162. // console.log(subSignKeyArr);
  1163. if (sheetData) {
  1164. // console.log('sheetData');
  1165. let startPicIdx = 2;
  1166. for (const sCell of sheetData[JV.PROP_SIGNATURE_CELLS]) {
  1167. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1168. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1169. startPicIdx++;
  1170. }
  1171. }
  1172. for (const sCell of sheetData[JV.PROP_WATERMARK_CELLS]) {
  1173. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1174. private_setSheetDrawingCellData(sCell, yPos, startPicIdx, 0);
  1175. startPicIdx++;
  1176. }
  1177. }
  1178. } else {
  1179. // console.log('pageData.items[0][JV.PROP_SIGNATURE_CELLS]');
  1180. // console.log(pageData.items[1][JV.PROP_SIGNATURE_CELLS]);
  1181. // total data in one sheet
  1182. let rowOffset = 0;
  1183. for (let i = 0; i < pageData.items.length; i++) {
  1184. const shtItemData = pageData.items[i];
  1185. const tmpPos = yMultiPos[i];
  1186. let startPicIdx = 2;
  1187. for (const sCell of shtItemData[JV.PROP_SIGNATURE_CELLS]) {
  1188. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1189. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1190. startPicIdx++;
  1191. // } else {
  1192. // console.log('index < 0 subSignKeyArr: ');
  1193. // console.log(subSignKeyArr);
  1194. // console.log('sCell.signature_name');
  1195. // console.log(sCell.signature_name);
  1196. }
  1197. }
  1198. for (const sCell of shtItemData[JV.PROP_WATERMARK_CELLS]) {
  1199. if (subSignKeyArr.indexOf(sCell.signature_name) >= 0) {
  1200. private_setSheetDrawingCellData(sCell, tmpPos, startPicIdx, rowOffset);
  1201. startPicIdx++;
  1202. }
  1203. }
  1204. rowOffset += tmpPos.length - 2;
  1205. }
  1206. }
  1207. rst.push('</xdr:wsDr>');
  1208. return rst;
  1209. }
  1210. function writeDrawingsRels(amt, startIdx) {
  1211. const rst = [];
  1212. rst.push('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  1213. rst.push('<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">');
  1214. for (let idx = 0; idx < amt; idx++) {
  1215. rst.push('<Relationship Id="rId' + (idx + 1) + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image' + (startIdx + idx + 1) + '.png"/>');
  1216. }
  1217. rst.push('</Relationships>');
  1218. return rst;
  1219. }
  1220. function mergeProperties(orgObj, newObj) {
  1221. const orgPropArr = [];
  1222. const newPropArr = [];
  1223. for (const p in orgObj) {
  1224. orgPropArr.push(p);
  1225. }
  1226. for (const p in newObj) {
  1227. newPropArr.push(p);
  1228. }
  1229. for (let i = 0; i < newPropArr.length; i++) {
  1230. if (orgPropArr.indexOf(newPropArr[i]) < 0) {
  1231. orgObj[newPropArr[i]] = newObj[newPropArr[i]];
  1232. }
  1233. }
  1234. }
  1235. function resetDummuySignature(pageData, roleRel) {
  1236. // 备注:计算草图等其他图形需要额外做些处理
  1237. let dummySignIdx = 0;
  1238. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1239. const page = pageData.items[pageIdx];
  1240. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1241. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1242. if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1243. // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1244. signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1245. dummySignIdx++;
  1246. const roleRelItem = { type: '用户', sign_path: signature.path, signature_name: signature.signature_name };
  1247. roleRel.push(roleRelItem);
  1248. }
  1249. }
  1250. }
  1251. }
  1252. }
  1253. function _createDummyCell(dummyOrgCells, parentPage) {
  1254. for (const signature of dummyOrgCells) {
  1255. const dummyCell = {
  1256. font: 'Content',
  1257. control: 'Default',
  1258. style: 'Default_None',
  1259. Value: '',
  1260. area: { Left: 0, Right: 0, Top: 0, Bottom: 0 },
  1261. };
  1262. if (signature.style) dummyCell.style = signature.style;
  1263. dummyCell[JV.PROP_AREA][JV.PROP_LEFT] = signature[JV.PROP_AREA][JV.PROP_LEFT];
  1264. dummyCell[JV.PROP_AREA][JV.PROP_RIGHT] = signature[JV.PROP_AREA][JV.PROP_RIGHT];
  1265. dummyCell[JV.PROP_AREA][JV.PROP_TOP] = signature[JV.PROP_AREA][JV.PROP_TOP];
  1266. dummyCell[JV.PROP_AREA][JV.PROP_BOTTOM] = signature[JV.PROP_AREA][JV.PROP_BOTTOM];
  1267. parentPage[JV.PROP_CELLS].push(dummyCell);
  1268. }
  1269. }
  1270. function createDummySignatureCell(pageData) {
  1271. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1272. const page = pageData.items[pageIdx];
  1273. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1274. _createDummyCell(page[JV.PROP_SIGNATURE_CELLS], page);
  1275. }
  1276. if (page[JV.PROP_SIGNATURE_DATE_CELLS] && page[JV.PROP_SIGNATURE_DATE_CELLS].length > 0) {
  1277. _createDummyCell(page[JV.PROP_SIGNATURE_DATE_CELLS], page);
  1278. }
  1279. if (page[JV.PROP_SIGNATURE_AUDIT_CELLS] && page[JV.PROP_SIGNATURE_AUDIT_CELLS].length > 0) {
  1280. _createDummyCell(page[JV.PROP_SIGNATURE_AUDIT_CELLS], page);
  1281. }
  1282. }
  1283. }
  1284. function _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr, isSinglePage) {
  1285. // 备注:电子签名是以图形的方式处理,一页可以有多个签名,多页的签名基本是引用同样的图片,在这里先处理一下,后期统一引用。
  1286. // 另:以后的图片(在电子签名(signature_cells)以外的图片)会单独处理(如计算草图、水印等)
  1287. let rst = false;
  1288. let chkRoles = [];
  1289. if (roleRel instanceof Array) {
  1290. chkRoles = roleRel;
  1291. } else {
  1292. if (roleRel) chkRoles.push(roleRel);
  1293. }
  1294. // console.log(chkRoles);
  1295. // console.log(signKeyArr);
  1296. // for (const page of pageData.items) {
  1297. for (let pageIdx = 0; pageIdx < pageData.items.length; pageIdx++) {
  1298. const page = pageData.items[pageIdx];
  1299. if (isSinglePage) {
  1300. if (pageIdx === 0) {
  1301. signKeyArr.push([]);
  1302. signPathArr.push([]);
  1303. }
  1304. } else {
  1305. signKeyArr.push([]);
  1306. signPathArr.push([]);
  1307. }
  1308. const curSignIdx = signKeyArr.length - 1;
  1309. signSheetIdxArr[pageIdx] = false;
  1310. // console.log('page index: ' + pageIdx);
  1311. // console.log(page[JV.PROP_SIGNATURE_CELLS]);
  1312. if (page[JV.PROP_SIGNATURE_CELLS] && page[JV.PROP_SIGNATURE_CELLS].length > 0) {
  1313. // let dummySignIdx = 0;
  1314. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1315. // if (signature.signature_name.indexOf(JV.SIGNATURE_NAME_DUMMY) >= 0) {
  1316. // // 表示这是一个其他类型的非原生电子签名图,只是借用signature的处理机制,每个图都是唯一的,所以需要重新给个唯一的新signature_name
  1317. // signature.signature_name = signature.signature_name + '_' + pageIdx + '_' + dummySignIdx;
  1318. // dummySignIdx++;
  1319. // rst = true;
  1320. // signSheetIdxArr[pageIdx] = true;
  1321. // }
  1322. if (signKeyArr[curSignIdx].indexOf(signature.signature_name) < 0) {
  1323. if (signature.pic) {
  1324. const signPath = { path: null, pic: null };
  1325. signPathArr[curSignIdx].push(signPath);
  1326. signPath.pic = signature.pic; // 历史报表
  1327. signKeyArr[curSignIdx].push(signature.signature_name);
  1328. rst = true;
  1329. signSheetIdxArr[pageIdx] = true;
  1330. } else {
  1331. for (const role of chkRoles) {
  1332. if (signature.signature_name === role.signature_name) {
  1333. // console.log('signature.signature_name: ' + signature.signature_name);
  1334. if (role.sign_pic) {
  1335. const signPath = { path: null, pic: null };
  1336. signPathArr[curSignIdx].push(signPath);
  1337. signPath.pic = role.sign_pic;
  1338. signKeyArr[curSignIdx].push(signature.signature_name);
  1339. rst = true;
  1340. signSheetIdxArr[pageIdx] = true;
  1341. } else if (role.sign_path) {
  1342. const signPath = { path: null, pic: null };
  1343. signPathArr[curSignIdx].push(signPath);
  1344. signPath.path = role.sign_path;
  1345. signKeyArr[curSignIdx].push(signature.signature_name);
  1346. rst = true;
  1347. signSheetIdxArr[pageIdx] = true;
  1348. }
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. }
  1354. }
  1355. }
  1356. if (page[JV.PROP_WATERMARK_CELLS] && page[JV.PROP_WATERMARK_CELLS].length > 0) {
  1357. // 水印,单独处理
  1358. const waterMarkCell = page[JV.PROP_WATERMARK_CELLS][0];
  1359. if (signKeyArr[curSignIdx].indexOf(waterMarkCell.signature_name) < 0) {
  1360. // console.log('waterMarkCell');
  1361. // console.log(waterMarkCell);
  1362. const signPath = { path: null, pic: null };
  1363. signPathArr[curSignIdx].push(signPath);
  1364. signPath.pic = waterMarkCell.pic;
  1365. signPath.path = waterMarkCell.path;
  1366. signKeyArr[curSignIdx].push(waterMarkCell.signature_name);
  1367. rst = true;
  1368. signSheetIdxArr[pageIdx] = true;
  1369. }
  1370. }
  1371. }
  1372. return rst;
  1373. }
  1374. function base64ToBuffer(dataurl) {
  1375. // 将base64转换为buffer
  1376. const arr = dataurl.split(',');
  1377. // console.log(arr[1]);
  1378. const rst = new Buffer(arr[1], 'base64');
  1379. return rst;
  1380. }
  1381. module.exports = {
  1382. exportExcel: function(pageData, paperSize, fName, options, custSheetNames, custSheetMergeBands, baseDir, roleRel, callback) {
  1383. const rptOptions = ({ singlePage: false, fileName: 'report' });
  1384. if (options === 'true' || options === true) {
  1385. rptOptions.singlePage = true;
  1386. }
  1387. const isSinglePage = rptOptions.singlePage;
  1388. const sheets = [];
  1389. const signKeyArr = [];
  1390. const signPathArr = [];
  1391. const signSheetIdxArr = []; // 确定哪些sheet有签名(在多表导出的时候,有些表可能没有签名,但其他的表有签名)
  1392. // console.log('in exportExcel!');
  1393. // console.log(roleRel);
  1394. // const hasSignature = (roleRel !== null) ? _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, roleRel, signSheetIdxArr) : false;
  1395. let thisRoleRel = [];
  1396. if (roleRel !== null && roleRel !== undefined) {
  1397. thisRoleRel = roleRel;
  1398. }
  1399. resetDummuySignature(pageData, thisRoleRel); // 把草图转换一下roleRel
  1400. // console.log(thisRoleRel);
  1401. // const hasSignature = false; // 暂时不支持电子签名、草图导出excel
  1402. const hasSignature = _checkAndSetSignatureCache(pageData, signKeyArr, signPathArr, thisRoleRel, signSheetIdxArr, isSinglePage); // 因草图的关系,thisRoleRel是否为null就不是充要的条件
  1403. // console.log('signKeyArr');
  1404. // console.log(signKeyArr);
  1405. // console.log('signPathArr');
  1406. // console.log(signPathArr);
  1407. // console.log('hasSignature: ' + hasSignature);
  1408. if (!hasSignature) {
  1409. createDummySignatureCell(pageData);
  1410. }
  1411. if (isSinglePage) {
  1412. // 加分页符(分页符要在外部处理好)-------------------
  1413. _setupPgBrks(pageData);
  1414. // 加分页符结束-------------------
  1415. sheets.push({ sheetName: '全部页' });
  1416. } else {
  1417. if (custSheetNames && custSheetNames.length === pageData.items.length) {
  1418. for (let i = 0; i < pageData.items.length; i++) {
  1419. sheets.push({ sheetName: custSheetNames[i] });
  1420. }
  1421. } else {
  1422. for (let i = 0; i < pageData.items.length; i++) {
  1423. sheets.push({ sheetName: '第' + (i + 1) + '页' });
  1424. }
  1425. }
  1426. }
  1427. // 1.
  1428. let file = '[Content_Types].xml';
  1429. let data = writeContentTypes(sheets, isSinglePage, hasSignature);
  1430. const zip = new JSZip();
  1431. zip.file(file, data.join(''), { compression: 'DEFLATE' });
  1432. // console.log('step 1');
  1433. // 2.
  1434. const zip_rels = zip.folder('_rels');
  1435. file = '.rels';
  1436. data = writeRootRels();
  1437. zip_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1438. // console.log('step 2');
  1439. // 3.
  1440. const zip_docProps = zip.folder('docProps');
  1441. file = 'app.xml';
  1442. data = writeApp(sheets, isSinglePage);
  1443. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1444. file = 'core.xml';
  1445. data = writeCore();
  1446. zip_docProps.file(file, data.join(''), { compression: 'DEFLATE' });
  1447. // console.log('step 3');
  1448. // 4.
  1449. const zip_xl = zip.folder('xl');
  1450. file = 'workbook.xml';
  1451. data = writeXlWorkBook(sheets, isSinglePage);
  1452. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1453. const zip_rels2 = zip_xl.folder('_rels');
  1454. file = 'workbook.xml.rels';
  1455. data = writeXlRels(sheets, isSinglePage);
  1456. zip_rels2.file(file, data.join(''), { compression: 'DEFLATE' });
  1457. // console.log('step 4');
  1458. // 5.
  1459. const zip_theme = zip_xl.folder('theme');
  1460. file = 'theme1.xml';
  1461. data = writeTheme();
  1462. zip_theme.file(file, data, { compression: 'DEFLATE' });
  1463. if (hasSignature) {
  1464. // 5.1 写所有的image
  1465. const zip_media = zip_xl.folder('media');
  1466. let picIdx = 0;
  1467. // console.log(signKeyArr);
  1468. // console.log(signPathArr);
  1469. for (let signIdx = 0; signIdx < signPathArr.length; signIdx++) {
  1470. if (signKeyArr[signIdx].length > 0) {
  1471. for (let pathIdx = 0; pathIdx < signPathArr[signIdx].length; pathIdx++) {
  1472. picIdx++;
  1473. data = writeImage(signPathArr[signIdx][pathIdx].path, signPathArr[signIdx][pathIdx].pic, baseDir);
  1474. file = 'image' + picIdx + '.png';
  1475. zip_media.file(file, data, { compression: 'DEFLATE' });
  1476. }
  1477. }
  1478. }
  1479. // 5.2
  1480. const zip_drawings = zip_xl.folder('drawings');
  1481. data = writeDrawings(pageData, signKeyArr, signPathArr, isSinglePage, signSheetIdxArr);
  1482. // data = writeDrawings(pageData, signKeyArr, signPathArr, false, signSheetIdxArr); // 备注:这里根本不需要考虑是否single page
  1483. // console.log('isSinglePage: ' + isSinglePage);
  1484. // console.log(data);
  1485. for (let psIdx = 0; psIdx < data.length; psIdx++) {
  1486. file = 'drawing' + (psIdx + 1) + '.xml';
  1487. // console.log('drawing1' + (psIdx + 1) + '.xml data');
  1488. // console.log(data);
  1489. zip_drawings.file(file, data[psIdx].join(''), { compression: 'DEFLATE' });
  1490. }
  1491. // 5.3
  1492. const zip_drawings_rels = zip_drawings.folder('_rels');
  1493. let relsIdx = 0;
  1494. let relsImgAmt = 0;
  1495. for (let ssIdx = 0; ssIdx < signKeyArr.length; ssIdx++) {
  1496. if (signKeyArr[ssIdx].length > 0) {
  1497. data = writeDrawingsRels(signKeyArr[ssIdx].length, relsImgAmt); // 一个drawingX.xml.rels文件与一个drawingX.xml对应
  1498. relsIdx++;
  1499. relsImgAmt += signKeyArr[ssIdx].length;
  1500. // console.log('drawing1.xml.rels data');
  1501. // console.log(data);
  1502. file = 'drawing' + relsIdx + '.xml.rels';
  1503. zip_drawings_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1504. }
  1505. }
  1506. }
  1507. // console.log('step 5');
  1508. // 6.
  1509. const zip_xl_worksheets = zip_xl.folder('worksheets');
  1510. const sharedStrList = [];
  1511. const stylesObj = {};
  1512. data = writeSheets(pageData, paperSize, sharedStrList, stylesObj, isSinglePage, custSheetMergeBands, hasSignature, signSheetIdxArr);
  1513. if (isSinglePage) {
  1514. for (let i = 0; i < 1; i++) {
  1515. file = 'sheet' + (i + 1) + '.xml';
  1516. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1517. }
  1518. } else {
  1519. for (let i = 0; i < data.length; i++) {
  1520. file = 'sheet' + (i + 1) + '.xml';
  1521. zip_xl_worksheets.file(file, data[i].join(''), { compression: 'DEFLATE' });
  1522. }
  1523. }
  1524. // console.log('step 6');
  1525. // 7.
  1526. if (hasSignature) {
  1527. const zip_xl_worksheets_rels = zip_xl_worksheets.folder('_rels');
  1528. if (isSinglePage) {
  1529. data = writeWorkSheetRels(1);
  1530. file = 'sheet1.xml.rels';
  1531. zip_xl_worksheets_rels.file(file, data.join(''), { compression: 'DEFLATE' });
  1532. } else {
  1533. let ssIdx = 0;
  1534. for (let i = 0; i < data.length; i++) {
  1535. if (signSheetIdxArr[i]) {
  1536. ssIdx++;
  1537. const ssData = writeWorkSheetRels(ssIdx);
  1538. file = 'sheet' + (i + 1) + '.xml.rels';
  1539. zip_xl_worksheets_rels.file(file, ssData.join(''), { compression: 'DEFLATE' });
  1540. }
  1541. }
  1542. }
  1543. }
  1544. file = 'sharedStrings.xml';
  1545. data = writeSharedString(sharedStrList);
  1546. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1547. file = 'styles.xml';
  1548. data = writeStyles(stylesObj, hasSignature);
  1549. zip_xl.file(file, data.join(''), { compression: 'DEFLATE' });
  1550. // console.log('step 7');
  1551. if (fName) {
  1552. // let newName = '' + (new Date()).valueOf();
  1553. const newName = uuidV1();
  1554. // console.log('current path: ' + __dirname);
  1555. // const filePath = this.app.baseDir + '/app/public/download/';
  1556. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1557. .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 13) + '/public/download/' + newName + '.xlsx'))
  1558. .on('finish', function() {
  1559. // JSZip generates a readable stream with a "end" event,
  1560. // but is piped here in a writable stream which emits a "finish" event.
  1561. console.log(newName + '.xlsx was written.');
  1562. if (callback) callback(null, newName);
  1563. });
  1564. } else {
  1565. // return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
  1566. return zip;
  1567. }
  1568. },
  1569. exportExcelInOneBook: function(pageDataArray, paperSize, fName, baseDir, roleRelArr, callback) {
  1570. const me = this;
  1571. const newPageData = {};
  1572. // 1. 重新编排一下数据,把一份报表的pageData合并到一起作为一个Sheet输出(需要重新调整数据纵向坐标),多份报表数据就形成多个Sheet
  1573. // -- 简单来说,就是重新包装数据
  1574. try {
  1575. // 1.1 newPageData外围属性
  1576. const newContrl = {};
  1577. const newFont = {};
  1578. const newStyle = {};
  1579. const custMergeBands = [];
  1580. for (let i = 0; i < pageDataArray.length; i++) {
  1581. mergeProperties(newContrl, pageDataArray[i][JV.NODE_CONTROL_COLLECTION]);
  1582. mergeProperties(newFont, pageDataArray[i][JV.NODE_FONT_COLLECTION]);
  1583. mergeProperties(newStyle, pageDataArray[i][JV.NODE_STYLE_COLLECTION]);
  1584. }
  1585. newPageData[JV.NODE_CONTROL_COLLECTION] = newContrl;
  1586. newPageData[JV.NODE_FONT_COLLECTION] = newFont;
  1587. newPageData[JV.NODE_STYLE_COLLECTION] = newStyle;
  1588. newPageData[JV.NODE_PAGE_INFO] = pageDataArray[0][JV.NODE_PAGE_INFO];
  1589. newPageData[JV.BAND_PROP_MERGE_BAND] = pageDataArray[0][JV.BAND_PROP_MERGE_BAND];
  1590. // 1.2 重新设置pageDataArray的各个cell的Top/Bottom坐标
  1591. const sheetNames = [];
  1592. const newPagePos = [];
  1593. for (let i = 0; i < pageDataArray.length; i++) {
  1594. let offsetY = 0;
  1595. const mergeBand = {};
  1596. custMergeBands.push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND]);
  1597. // 备注:不同的报表有可能有不同的边框,如封面表就是无边框的
  1598. mergeBand[JV.PROP_LEFT] = [];
  1599. mergeBand[JV.PROP_RIGHT] = [];
  1600. mergeBand[JV.PROP_TOP] = [];
  1601. mergeBand[JV.PROP_BOTTOM] = [];
  1602. newPagePos.push(mergeBand);
  1603. mergeBand[JV.PROP_LEFT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_LEFT]);
  1604. mergeBand[JV.PROP_RIGHT].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_RIGHT]);
  1605. sheetNames.push(pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
  1606. // 加分页符(分页符要在外部处理好)-------------------
  1607. _setupPgBrks(pageDataArray[i]);
  1608. // 加分页符结束-------------------
  1609. let maxY = 0;
  1610. let minY = 100000;
  1611. const checkAndResetCellYPos = function(cells) {
  1612. if (cells && cells.length > 0) {
  1613. for (const cell of cells) {
  1614. if (maxY < cell[JV.PROP_AREA][JV.PROP_BOTTOM]) {
  1615. maxY = cell[JV.PROP_AREA][JV.PROP_BOTTOM];
  1616. }
  1617. if (minY > cell[JV.PROP_AREA][JV.PROP_TOP]) {
  1618. minY = cell[JV.PROP_AREA][JV.PROP_TOP];
  1619. }
  1620. cell[JV.PROP_AREA][JV.PROP_BOTTOM] += offsetY;
  1621. cell[JV.PROP_AREA][JV.PROP_TOP] += offsetY;
  1622. }
  1623. }
  1624. };
  1625. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1626. maxY = 0;
  1627. minY = 100000;
  1628. if (pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS]) {
  1629. let pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][0] + offsetY;
  1630. mergeBand[JV.PROP_TOP].push(pos);
  1631. pos = pageDataArray[i].items[j][JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][0] + offsetY;
  1632. mergeBand[JV.PROP_BOTTOM].push(pos);
  1633. } else if (pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER]) {
  1634. let pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_TOP] + offsetY;
  1635. mergeBand[JV.PROP_TOP].push(pos);
  1636. pos = pageDataArray[i].items[j][JV.PROP_PAGE_MERGE_BORDER][JV.PROP_BOTTOM] + offsetY;
  1637. mergeBand[JV.PROP_BOTTOM].push(pos);
  1638. } else {
  1639. mergeBand[JV.PROP_TOP].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_TOP] + offsetY);
  1640. mergeBand[JV.PROP_BOTTOM].push(pageDataArray[i][JV.BAND_PROP_MERGE_BAND][JV.PROP_BOTTOM] + offsetY);
  1641. }
  1642. // 1.2.1 重新设置普通cells的Top Bottom坐标
  1643. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_CELLS]);
  1644. // 1.2.2 重新设置电子签名cells的Top Bottom坐标
  1645. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS]);
  1646. // 1.2.3 重新设置电子签名日期cells的Top Bottom坐标
  1647. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS]);
  1648. // 1.2.4 重置电子签名审核意见cells的Top Bottom坐标
  1649. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS]);
  1650. // 1.2.5 重置水印的Top Bottom坐标
  1651. checkAndResetCellYPos(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS]);
  1652. 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;
  1653. offsetY += (maxY - minY);
  1654. if (bottomGap > 10) {
  1655. offsetY += (bottomGap - 10);
  1656. }
  1657. }
  1658. }
  1659. // 2. newPageData的items属性及相关分页符
  1660. newPageData.items = [];
  1661. newPageData.pageBreaks = [];
  1662. for (let i = 0; i < pageDataArray.length; i++) {
  1663. const pageItem = {};
  1664. pageItem[JV.PROP_PAGE_SEQ] = i + 1;
  1665. pageItem[JV.PROP_CELLS] = [];
  1666. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1667. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_CELLS].length; k++) {
  1668. pageItem[JV.PROP_CELLS].push(pageDataArray[i].items[j][JV.PROP_CELLS][k]);
  1669. }
  1670. }
  1671. pageItem[JV.PROP_SIGNATURE_CELLS] = [];
  1672. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1673. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS].length; k++) {
  1674. pageItem[JV.PROP_SIGNATURE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_CELLS][k]);
  1675. }
  1676. }
  1677. pageItem[JV.PROP_SIGNATURE_DATE_CELLS] = [];
  1678. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1679. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS].length; k++) {
  1680. pageItem[JV.PROP_SIGNATURE_DATE_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_DATE_CELLS][k]);
  1681. }
  1682. }
  1683. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS] = [];
  1684. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1685. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS].length; k++) {
  1686. pageItem[JV.PROP_SIGNATURE_AUDIT_CELLS].push(pageDataArray[i].items[j][JV.PROP_SIGNATURE_AUDIT_CELLS][k]);
  1687. }
  1688. }
  1689. pageItem[JV.PROP_WATERMARK_CELLS] = [];
  1690. for (let j = 0; j < pageDataArray[i].items.length; j++) {
  1691. for (let k = 0; k < pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS].length; k++) {
  1692. pageItem[JV.PROP_WATERMARK_CELLS].push(pageDataArray[i].items[j][JV.PROP_WATERMARK_CELLS][k]);
  1693. }
  1694. }
  1695. newPagePos[i][JV.NODE_PAGE_SIZE] = pageDataArray[i][JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE];
  1696. pageItem[JV.PAGE_SPECIAL_MERGE_POS] = newPagePos[i];
  1697. newPageData.items.push(pageItem);
  1698. // 重新分配分页符()------------------
  1699. if (pageDataArray[i].pageBreaks.length === 1) {
  1700. newPageData.pageBreaks.push(pageDataArray[i].pageBreaks[0]);
  1701. } else {
  1702. newPageData.pageBreaks.push([]);
  1703. }
  1704. // 重新分配分页符结束------------------
  1705. }
  1706. // 3. everything is ok, then call me
  1707. // let roleRel = null; // 未来调用的时候,这个属性要从外面给!!!
  1708. // roleRelArr
  1709. me.exportExcel(newPageData, paperSize, fName, 'false', sheetNames, custMergeBands, baseDir, roleRelArr, callback);
  1710. // fsUtil.writeObjToFile(newPageData, 'D:/GitHome/ConstructionOperation/tmp/combinedHeader.js');
  1711. } catch (e) {
  1712. console.log(e);
  1713. }
  1714. },
  1715. };