rpt_excel_util.js 81 KB

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