rpt_excel_util.js 83 KB

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