write.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #include "xlswriter.h"
  13. /*
  14. * According to the zval type written to the file
  15. */
  16. void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle)
  17. {
  18. lxw_format *value_format = NULL;
  19. lxw_col_t lxw_col = (lxw_col_t)columns;
  20. lxw_row_t lxw_row = (lxw_row_t)row;
  21. zend_uchar value_type = Z_TYPE_P(value);
  22. if (value_type == IS_STRING) {
  23. zend_string *_zs_value = zval_get_string(value);
  24. int error = worksheet_write_string(res->worksheet, lxw_row, lxw_col, ZSTR_VAL(_zs_value), format_handle);
  25. zend_string_release(_zs_value);
  26. WORKSHEET_WRITER_EXCEPTION(error);
  27. return;
  28. }
  29. if (value_type == IS_LONG) {
  30. if (format != NULL && format_handle == NULL) {
  31. value_format = workbook_add_format(res->workbook);
  32. format_set_num_format(value_format, ZSTR_VAL(format));
  33. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), value_format));
  34. return;
  35. }
  36. if (format == NULL && format_handle != NULL) {
  37. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), format_handle));
  38. return;
  39. }
  40. if(format != NULL && format_handle != NULL) {
  41. value_format = workbook_add_format(res->workbook);
  42. format_copy(value_format, format_handle);
  43. format_set_num_format(value_format, ZSTR_VAL(format));
  44. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), value_format));
  45. return;
  46. }
  47. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), NULL));
  48. }
  49. if (value_type == IS_DOUBLE) {
  50. if (format != NULL && format_handle == NULL) {
  51. value_format = workbook_add_format(res->workbook);
  52. format_set_num_format(value_format, ZSTR_VAL(format));
  53. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format));
  54. return;
  55. }
  56. if (format == NULL && format_handle != NULL) {
  57. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), format_handle));
  58. return;
  59. }
  60. if(format != NULL && format_handle != NULL) {
  61. value_format = workbook_add_format(res->workbook);
  62. format_copy(value_format, format_handle);
  63. format_set_num_format(value_format, ZSTR_VAL(format));
  64. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format));
  65. return;
  66. }
  67. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, zval_get_double(value), NULL));
  68. return;
  69. }
  70. }
  71. void format_copy(lxw_format *new_format, lxw_format *other_format)
  72. {
  73. new_format->bold = other_format->bold;
  74. new_format->bg_color = other_format->bg_color;
  75. new_format->border_count = other_format->border_count;
  76. new_format->border_index = other_format->border_index;
  77. new_format->bottom = other_format->bottom;
  78. new_format->bottom_color = other_format->bottom_color;
  79. new_format->color_indexed = other_format->color_indexed;
  80. new_format->diag_border = other_format->diag_border;
  81. new_format->diag_color = other_format->diag_color;
  82. new_format->font_size = other_format->font_size;
  83. new_format->bold = other_format->bold;
  84. new_format->italic = other_format->italic;
  85. new_format->font_color = other_format->font_color;
  86. new_format->underline = other_format->underline;
  87. new_format->font_strikeout = other_format->font_strikeout;
  88. new_format->font_outline = other_format->font_outline;
  89. new_format->font_shadow = other_format->font_shadow;
  90. new_format->font_script = other_format->font_script;
  91. new_format->font_family = other_format->font_family;
  92. new_format->font_charset = other_format->font_charset;
  93. new_format->font_condense = other_format->font_condense;
  94. new_format->font_extend = other_format->font_extend;
  95. new_format->theme = other_format->theme;
  96. new_format->hyperlink = other_format->hyperlink;
  97. new_format->hidden = other_format->hidden;
  98. new_format->locked = other_format->locked;
  99. new_format->text_h_align = other_format->text_h_align;
  100. new_format->text_wrap = other_format->text_wrap;
  101. new_format->text_v_align = other_format->text_v_align;
  102. new_format->text_justlast = other_format->text_justlast;
  103. new_format->rotation = other_format->rotation;
  104. new_format->fg_color = other_format->fg_color;
  105. new_format->bg_color = other_format->bg_color;
  106. new_format->pattern = other_format->pattern;
  107. new_format->has_fill = other_format->has_fill;
  108. new_format->has_dxf_fill = other_format->has_dxf_fill;
  109. new_format->fill_index = other_format->fill_index;
  110. new_format->fill_count = other_format->fill_count;
  111. new_format->border_index = other_format->border_index;
  112. new_format->has_border = other_format->has_border;
  113. new_format->has_dxf_border = other_format->has_dxf_border;
  114. new_format->border_count = other_format->border_count;
  115. new_format->bottom = other_format->bottom;
  116. new_format->diag_border = other_format->diag_border;
  117. new_format->diag_type = other_format->diag_type;
  118. new_format->left = other_format->left;
  119. new_format->right = other_format->right;
  120. new_format->top = other_format->top;
  121. new_format->bottom_color = other_format->bottom_color;
  122. new_format->diag_color = other_format->diag_color;
  123. new_format->left_color = other_format->left_color;
  124. new_format->right_color = other_format->right_color;
  125. new_format->top_color = other_format->top_color;
  126. new_format->indent = other_format->indent;
  127. new_format->shrink = other_format->shrink;
  128. new_format->merge_range = other_format->merge_range;
  129. new_format->reading_order = other_format->reading_order;
  130. new_format->just_distrib = other_format->just_distrib;
  131. new_format->color_indexed = other_format->color_indexed;
  132. new_format->font_only = other_format->font_only;
  133. }
  134. void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *url, zend_string *text, zend_string *tool_tip, lxw_format *format)
  135. {
  136. if (text == NULL && tool_tip == NULL) {
  137. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, NULL, NULL);
  138. return;
  139. }
  140. if (text == NULL && tool_tip != NULL) {
  141. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, NULL, ZSTR_VAL(tool_tip));
  142. return;
  143. }
  144. if (text != NULL && tool_tip == NULL) {
  145. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, ZSTR_VAL(text), NULL);
  146. return;
  147. }
  148. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, ZSTR_VAL(text), ZSTR_VAL(tool_tip));
  149. }
  150. /*
  151. * Write the image to the file
  152. */
  153. void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res)
  154. {
  155. lxw_image_options options = {.x_scale = width, .y_scale = height};
  156. worksheet_insert_image_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(zval_get_string(value)), &options);
  157. }
  158. /*
  159. * Write the image to the file
  160. */
  161. void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format)
  162. {
  163. worksheet_write_formula(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(value), format);
  164. }
  165. /*
  166. * Write the chart to the file
  167. */
  168. void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res)
  169. {
  170. worksheet_insert_chart(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, chart_resource->chart);
  171. }
  172. /*
  173. * Write the datetime to the file
  174. */
  175. void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle)
  176. {
  177. lxw_format *value_format = workbook_add_format(res->workbook);
  178. if (format_handle != NULL) {
  179. format_copy(value_format, format_handle);
  180. }
  181. format_set_num_format(value_format, ZSTR_VAL(format));
  182. worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, value_format);
  183. }
  184. /*
  185. * Write the comment to the cell
  186. */
  187. void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res)
  188. {
  189. int error = worksheet_write_comment(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(comment));
  190. WORKSHEET_WRITER_EXCEPTION(error);
  191. }
  192. /*
  193. * Show all comments
  194. */
  195. void comment_show(xls_resource_write_t *res)
  196. {
  197. worksheet_show_comments(res->worksheet);
  198. }
  199. /*
  200. * Add the autofilter.
  201. */
  202. void auto_filter(zend_string *range, xls_resource_write_t *res)
  203. {
  204. int error = worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));
  205. // Cells that have been placed cannot be modified using optimization mode
  206. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  207. // Worksheet row or column index out of range
  208. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  209. }
  210. /*
  211. * Merge cells.
  212. */
  213. void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format)
  214. {
  215. char *_range = ZSTR_VAL(range);
  216. int error = worksheet_merge_range(res->worksheet, RANGE(_range), "", format);
  217. // Cells that have been placed cannot be modified using optimization mode
  218. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  219. // Worksheet row or column index out of range
  220. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  221. // writer merge cell
  222. type_writer(value, lxw_name_to_row(_range), lxw_name_to_col(_range), res, NULL, format);
  223. }
  224. /*
  225. * Set column format
  226. */
  227. void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format)
  228. {
  229. worksheet_set_column(res->worksheet, COLS(ZSTR_VAL(range)), width, format);
  230. }
  231. /*
  232. * Set row format
  233. */
  234. void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format)
  235. {
  236. char *rows = ZSTR_VAL(range);
  237. if (strchr(rows, ':')) {
  238. worksheet_set_rows(ROWS(rows), height, res, format);
  239. } else {
  240. int error = worksheet_set_row(res->worksheet, ROW(rows), height, format);
  241. // Cells that have been placed cannot be modified using optimization mode
  242. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  243. // Worksheet row or column index out of range
  244. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  245. }
  246. }
  247. /*
  248. * Add data validations to a worksheet
  249. */
  250. void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validation *validation)
  251. {
  252. worksheet_data_validation_cell(res->worksheet, CELL(ZSTR_VAL(range)), validation);
  253. }
  254. /*
  255. * Set rows format
  256. */
  257. void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format)
  258. {
  259. while (1) {
  260. worksheet_set_row(res->worksheet, end, height, format);
  261. if (end == start)
  262. break;
  263. end--;
  264. }
  265. }
  266. /*
  267. * Set freeze panes
  268. */
  269. void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column)
  270. {
  271. worksheet_freeze_panes(res->worksheet, row, column);
  272. }
  273. /*
  274. * Display or hide screen and print gridlines
  275. */
  276. void gridlines(xls_resource_write_t *res, zend_long option)
  277. {
  278. worksheet_gridlines(res->worksheet, option);
  279. }
  280. /*
  281. * Set the worksheet zoom factor
  282. */
  283. void zoom(xls_resource_write_t *res, zend_long zoom)
  284. {
  285. worksheet_set_zoom(res->worksheet, zoom);
  286. }
  287. /*
  288. * Set the worksheet protection
  289. */
  290. void protection(xls_resource_write_t *res, zend_string *password)
  291. {
  292. if (password == NULL) {
  293. worksheet_protect(res->worksheet, NULL, NULL);
  294. } else {
  295. worksheet_protect(res->worksheet, ZSTR_VAL(password), NULL);
  296. }
  297. }
  298. /*
  299. * Set the worksheet printed direction
  300. */
  301. void printed_direction(xls_resource_write_t *res, unsigned int direction)
  302. {
  303. if (direction == XLSWRITER_PRINTED_PORTRAIT) {
  304. worksheet_set_portrait(res->worksheet);
  305. }
  306. worksheet_set_landscape(res->worksheet);
  307. }
  308. /*
  309. * Hide worksheet
  310. */
  311. void hide_worksheet(xls_resource_write_t *res)
  312. {
  313. worksheet_hide(res->worksheet);
  314. }
  315. /*
  316. * First worksheet
  317. */
  318. void first_worksheet(xls_resource_write_t *res)
  319. {
  320. worksheet_set_first_sheet(res->worksheet);
  321. }
  322. /*
  323. * Call finalization code and close file.
  324. */
  325. lxw_error
  326. workbook_file(xls_resource_write_t *self)
  327. {
  328. lxw_worksheet *worksheet = NULL;
  329. lxw_packager *packager = NULL;
  330. lxw_error error = LXW_NO_ERROR;
  331. /* Add a default worksheet if non have been added. */
  332. if (!self->workbook->num_sheets)
  333. workbook_add_worksheet(self->workbook, NULL);
  334. /* Ensure that at least one worksheet has been selected. */
  335. if (self->workbook->active_sheet == 0) {
  336. worksheet = STAILQ_FIRST(self->workbook->worksheets);
  337. worksheet->selected = 1;
  338. worksheet->hidden = 0;
  339. }
  340. /* Set the active sheet. */
  341. STAILQ_FOREACH(worksheet, self->workbook->worksheets, list_pointers) {
  342. if (worksheet->index == self->workbook->active_sheet)
  343. worksheet->active = 1;
  344. }
  345. /* Prepare the worksheet VML elements such as comments. */
  346. _prepare_vml(self->workbook);
  347. /* Set the defined names for the worksheets such as Print Titles. */
  348. _prepare_defined_names(self->workbook);
  349. /* Prepare the drawings, charts and images. */
  350. _prepare_drawings(self->workbook);
  351. /* Add cached data to charts. */
  352. _add_chart_cache_data(self->workbook);
  353. /* Create a packager object to assemble sub-elements into a zip file. */
  354. packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, self->workbook->options.use_zip64);
  355. /* If the packager fails it is generally due to a zip permission error. */
  356. if (packager == NULL) {
  357. fprintf(stderr, "[ERROR] workbook_close(): "
  358. "Error creating '%s'. "
  359. "Error = %s\n", self->workbook->filename, strerror(errno));
  360. error = LXW_ERROR_CREATING_XLSX_FILE;
  361. goto mem_error;
  362. }
  363. /* Set the workbook object in the packager. */
  364. packager->workbook = self->workbook;
  365. /* Assemble all the sub-files in the xlsx package. */
  366. error = lxw_create_package(packager);
  367. /* Error and non-error conditions fall through to the cleanup code. */
  368. if (error == LXW_ERROR_CREATING_TMPFILE) {
  369. fprintf(stderr, "[ERROR] workbook_close(): "
  370. "Error creating tmpfile(s) to assemble '%s'. "
  371. "Error = %s\n", self->workbook->filename, strerror(errno));
  372. }
  373. /* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zlib. */
  374. if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
  375. fprintf(stderr, "[ERROR] workbook_close(): "
  376. "Zlib error while creating xlsx file '%s'. "
  377. "Error = %s\n", self->workbook->filename, strerror(errno));
  378. }
  379. /* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
  380. if (error == LXW_ERROR_ZIP_PARAMETER_ERROR) {
  381. fprintf(stderr, "[ERROR] workbook_close(): "
  382. "Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
  383. "System error = %s\n", self->workbook->filename, strerror(errno));
  384. }
  385. /* If LXW_ERROR_ZIP_BAD_ZIP_FILE then errno is set by zip. */
  386. if (error == LXW_ERROR_ZIP_BAD_ZIP_FILE) {
  387. fprintf(stderr, "[ERROR] workbook_close(): "
  388. "Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
  389. "This may require the use_zip64 option for large files. "
  390. "System error = %s\n", self->workbook->filename, strerror(errno));
  391. }
  392. /* If LXW_ERROR_ZIP_INTERNAL_ERROR then errno is set by zip. */
  393. if (error == LXW_ERROR_ZIP_INTERNAL_ERROR) {
  394. fprintf(stderr, "[ERROR] workbook_close(): "
  395. "Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
  396. "System error = %s\n", self->workbook->filename, strerror(errno));
  397. }
  398. /* The next 2 error conditions don't set errno. */
  399. if (error == LXW_ERROR_ZIP_FILE_ADD) {
  400. fprintf(stderr, "[ERROR] workbook_close(): "
  401. "Zlib error adding file to xlsx file '%s'.\n",
  402. self->workbook->filename);
  403. }
  404. if (error == LXW_ERROR_ZIP_CLOSE) {
  405. fprintf(stderr, "[ERROR] workbook_close(): "
  406. "Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
  407. }
  408. mem_error:
  409. lxw_packager_free(packager);
  410. return error;
  411. }
  412. void _php_vtiful_xls_close(zend_resource *rsrc TSRMLS_DC)
  413. {
  414. }
  415. /*
  416. * Iterate through the worksheets and set up the VML objects.
  417. */
  418. STATIC void
  419. _prepare_vml(lxw_workbook *self)
  420. {
  421. lxw_worksheet *worksheet;
  422. lxw_sheet *sheet;
  423. uint32_t comment_id = 0;
  424. uint32_t vml_drawing_id = 0;
  425. uint32_t vml_data_id = 1;
  426. uint32_t vml_shape_id = 1024;
  427. uint32_t comment_count = 0;
  428. STAILQ_FOREACH(sheet, self->sheets, list_pointers) {
  429. if (sheet->is_chartsheet)
  430. continue;
  431. else
  432. worksheet = sheet->u.worksheet;
  433. if (!worksheet->has_vml && !worksheet->has_header_vml)
  434. continue;
  435. if (worksheet->has_vml) {
  436. self->has_vml = LXW_TRUE;
  437. if (worksheet->has_comments) {
  438. self->comment_count += 1;
  439. comment_id += 1;
  440. self->has_comments = LXW_TRUE;
  441. }
  442. vml_drawing_id += 1;
  443. comment_count = lxw_worksheet_prepare_vml_objects(worksheet,
  444. vml_data_id,
  445. vml_shape_id,
  446. vml_drawing_id,
  447. comment_id);
  448. /* Each VML should start with a shape id incremented by 1024. */
  449. vml_data_id += 1 * ((1024 + comment_count) / 1024);
  450. vml_shape_id += 1024 * ((1024 + comment_count) / 1024);
  451. }
  452. }
  453. }
  454. /*
  455. * Iterate through the worksheets and store any defined names used for print
  456. * ranges or repeat rows/columns.
  457. */
  458. STATIC void
  459. _prepare_defined_names(lxw_workbook *self)
  460. {
  461. lxw_worksheet *worksheet;
  462. char app_name[LXW_DEFINED_NAME_LENGTH];
  463. char range[LXW_DEFINED_NAME_LENGTH];
  464. char area[LXW_MAX_CELL_RANGE_LENGTH];
  465. char first_col[8];
  466. char last_col[8];
  467. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  468. /*
  469. * Check for autofilter settings and store them.
  470. */
  471. if (worksheet->autofilter.in_use) {
  472. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  473. "%s!_FilterDatabase", worksheet->quoted_name);
  474. lxw_rowcol_to_range_abs(area,
  475. worksheet->autofilter.first_row,
  476. worksheet->autofilter.first_col,
  477. worksheet->autofilter.last_row,
  478. worksheet->autofilter.last_col);
  479. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH, "%s!%s",
  480. worksheet->quoted_name, area);
  481. /* Autofilters are the only defined name to set the hidden flag. */
  482. _store_defined_name(self, "_xlnm._FilterDatabase", app_name, range, worksheet->index, LXW_TRUE);
  483. }
  484. /*
  485. * Check for Print Area settings and store them.
  486. */
  487. if (worksheet->print_area.in_use) {
  488. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  489. "%s!Print_Area", worksheet->quoted_name);
  490. /* Check for print area that is the max row range. */
  491. if (worksheet->print_area.first_row == 0
  492. && worksheet->print_area.last_row == LXW_ROW_MAX - 1) {
  493. lxw_col_to_name(first_col,
  494. worksheet->print_area.first_col, LXW_FALSE);
  495. lxw_col_to_name(last_col,
  496. worksheet->print_area.last_col, LXW_FALSE);
  497. lxw_snprintf(area, LXW_MAX_CELL_RANGE_LENGTH - 1, "$%s:$%s",
  498. first_col, last_col);
  499. }
  500. /* Check for print area that is the max column range. */
  501. else if (worksheet->print_area.first_col == 0
  502. && worksheet->print_area.last_col == LXW_COL_MAX - 1) {
  503. lxw_snprintf(area, LXW_MAX_CELL_RANGE_LENGTH - 1, "$%d:$%d",
  504. worksheet->print_area.first_row + 1,
  505. worksheet->print_area.last_row + 1);
  506. }
  507. else {
  508. lxw_rowcol_to_range_abs(area,
  509. worksheet->print_area.first_row,
  510. worksheet->print_area.first_col,
  511. worksheet->print_area.last_row,
  512. worksheet->print_area.last_col);
  513. }
  514. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH, "%s!%s",
  515. worksheet->quoted_name, area);
  516. _store_defined_name(self, "_xlnm.Print_Area", app_name,
  517. range, worksheet->index, LXW_FALSE);
  518. }
  519. /*
  520. * Check for repeat rows/cols. aka, Print Titles and store them.
  521. */
  522. if (worksheet->repeat_rows.in_use || worksheet->repeat_cols.in_use) {
  523. if (worksheet->repeat_rows.in_use
  524. && worksheet->repeat_cols.in_use) {
  525. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  526. "%s!Print_Titles", worksheet->quoted_name);
  527. lxw_col_to_name(first_col,
  528. worksheet->repeat_cols.first_col, LXW_FALSE);
  529. lxw_col_to_name(last_col,
  530. worksheet->repeat_cols.last_col, LXW_FALSE);
  531. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  532. "%s!$%s:$%s,%s!$%d:$%d",
  533. worksheet->quoted_name, first_col,
  534. last_col, worksheet->quoted_name,
  535. worksheet->repeat_rows.first_row + 1,
  536. worksheet->repeat_rows.last_row + 1);
  537. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  538. range, worksheet->index, LXW_FALSE);
  539. }
  540. else if (worksheet->repeat_rows.in_use) {
  541. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  542. "%s!Print_Titles", worksheet->quoted_name);
  543. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  544. "%s!$%d:$%d", worksheet->quoted_name,
  545. worksheet->repeat_rows.first_row + 1,
  546. worksheet->repeat_rows.last_row + 1);
  547. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  548. range, worksheet->index, LXW_FALSE);
  549. }
  550. else if (worksheet->repeat_cols.in_use) {
  551. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  552. "%s!Print_Titles", worksheet->quoted_name);
  553. lxw_col_to_name(first_col,
  554. worksheet->repeat_cols.first_col, LXW_FALSE);
  555. lxw_col_to_name(last_col,
  556. worksheet->repeat_cols.last_col, LXW_FALSE);
  557. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  558. "%s!$%s:$%s", worksheet->quoted_name,
  559. first_col, last_col);
  560. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  561. range, worksheet->index, LXW_FALSE);
  562. }
  563. }
  564. }
  565. }
  566. /*
  567. * Iterate through the worksheets and set up any chart or image drawings.
  568. */
  569. STATIC void
  570. _prepare_drawings(lxw_workbook *self)
  571. {
  572. lxw_worksheet *worksheet;
  573. lxw_object_properties *image_options;
  574. uint16_t chart_ref_id = 0;
  575. uint16_t image_ref_id = 0;
  576. uint16_t drawing_id = 0;
  577. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  578. if (STAILQ_EMPTY(worksheet->image_props)
  579. && STAILQ_EMPTY(worksheet->chart_data))
  580. continue;
  581. drawing_id++;
  582. STAILQ_FOREACH(image_options, worksheet->chart_data, list_pointers) {
  583. chart_ref_id++;
  584. lxw_worksheet_prepare_chart(worksheet, chart_ref_id, drawing_id,
  585. image_options, 0);
  586. if (image_options->chart)
  587. STAILQ_INSERT_TAIL(self->ordered_charts, image_options->chart,
  588. ordered_list_pointers);
  589. }
  590. STAILQ_FOREACH(image_options, worksheet->image_props, list_pointers) {
  591. if (image_options->image_type == LXW_IMAGE_PNG)
  592. self->has_png = LXW_TRUE;
  593. if (image_options->image_type == LXW_IMAGE_JPEG)
  594. self->has_jpeg = LXW_TRUE;
  595. if (image_options->image_type == LXW_IMAGE_BMP)
  596. self->has_bmp = LXW_TRUE;
  597. image_ref_id++;
  598. lxw_worksheet_prepare_image(worksheet, image_ref_id, drawing_id,
  599. image_options);
  600. }
  601. }
  602. self->drawing_count = drawing_id;
  603. }
  604. /*
  605. * Add "cached" data to charts to provide the numCache and strCache data for
  606. * series and title/axis ranges.
  607. */
  608. STATIC void
  609. _add_chart_cache_data(lxw_workbook *self)
  610. {
  611. lxw_chart *chart;
  612. lxw_chart_series *series;
  613. STAILQ_FOREACH(chart, self->ordered_charts, ordered_list_pointers) {
  614. _populate_range(self, chart->title.range);
  615. _populate_range(self, chart->x_axis->title.range);
  616. _populate_range(self, chart->y_axis->title.range);
  617. if (STAILQ_EMPTY(chart->series_list))
  618. continue;
  619. STAILQ_FOREACH(series, chart->series_list, list_pointers) {
  620. _populate_range(self, series->categories);
  621. _populate_range(self, series->values);
  622. _populate_range(self, series->title.range);
  623. }
  624. }
  625. }
  626. /*
  627. * Process and store the defined names. The defined names are stored with
  628. * the Workbook.xml but also with the App.xml if they refer to a sheet
  629. * range like "Sheet1!:A1". The defined names are store in sorted
  630. * order for consistency with Excel. The names need to be normalized before
  631. * sorting.
  632. */
  633. STATIC lxw_error
  634. _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden)
  635. {
  636. lxw_worksheet *worksheet;
  637. lxw_defined_name *defined_name;
  638. lxw_defined_name *list_defined_name;
  639. char name_copy[LXW_DEFINED_NAME_LENGTH];
  640. char *tmp_str;
  641. char *worksheet_name;
  642. /* Do some checks on the input data */
  643. if (!name || !formula)
  644. return LXW_ERROR_NULL_PARAMETER_IGNORED;
  645. if (lxw_utf8_strlen(name) > LXW_DEFINED_NAME_LENGTH ||
  646. lxw_utf8_strlen(formula) > LXW_DEFINED_NAME_LENGTH) {
  647. return LXW_ERROR_128_STRING_LENGTH_EXCEEDED;
  648. }
  649. /* Allocate a new defined_name to be added to the linked list of names. */
  650. defined_name = calloc(1, sizeof(struct lxw_defined_name));
  651. RETURN_ON_MEM_ERROR(defined_name, LXW_ERROR_MEMORY_MALLOC_FAILED);
  652. /* Copy the user input string. */
  653. lxw_strcpy(name_copy, name);
  654. /* Set the worksheet index or -1 for a global defined name. */
  655. defined_name->index = index;
  656. defined_name->hidden = hidden;
  657. /* Check for local defined names like like "Sheet1!name". */
  658. tmp_str = strchr(name_copy, '!');
  659. if (tmp_str == NULL) {
  660. /* The name is global. We just store the defined name string. */
  661. lxw_strcpy(defined_name->name, name_copy);
  662. }
  663. else {
  664. /* The name is worksheet local. We need to extract the sheet name
  665. * and map it to a sheet index. */
  666. /* Split the into the worksheet name and defined name. */
  667. *tmp_str = '\0';
  668. tmp_str++;
  669. worksheet_name = name_copy;
  670. /* Remove any worksheet quoting. */
  671. if (worksheet_name[0] == '\'')
  672. worksheet_name++;
  673. if (worksheet_name[strlen(worksheet_name) - 1] == '\'')
  674. worksheet_name[strlen(worksheet_name) - 1] = '\0';
  675. /* Search for worksheet name to get the equivalent worksheet index. */
  676. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  677. if (strcmp(worksheet_name, worksheet->name) == 0) {
  678. defined_name->index = worksheet->index;
  679. lxw_strcpy(defined_name->normalised_sheetname,
  680. worksheet_name);
  681. }
  682. }
  683. /* If we didn't find the worksheet name we exit. */
  684. if (defined_name->index == -1)
  685. goto mem_error;
  686. lxw_strcpy(defined_name->name, tmp_str);
  687. }
  688. /* Print titles and repeat title pass in the name used for App.xml. */
  689. if (app_name) {
  690. lxw_strcpy(defined_name->app_name, app_name);
  691. lxw_strcpy(defined_name->normalised_sheetname, app_name);
  692. }
  693. else {
  694. lxw_strcpy(defined_name->app_name, name);
  695. }
  696. /* We need to normalize the defined names for sorting. This involves
  697. * removing any _xlnm namespace and converting it to lowercase. */
  698. tmp_str = strstr(name_copy, "_xlnm.");
  699. if (tmp_str)
  700. lxw_strcpy(defined_name->normalised_name, defined_name->name + 6);
  701. else
  702. lxw_strcpy(defined_name->normalised_name, defined_name->name);
  703. lxw_str_tolower(defined_name->normalised_name);
  704. lxw_str_tolower(defined_name->normalised_sheetname);
  705. /* Strip leading "=" from the formula. */
  706. if (formula[0] == '=')
  707. lxw_strcpy(defined_name->formula, formula + 1);
  708. else
  709. lxw_strcpy(defined_name->formula, formula);
  710. /* We add the defined name to the list in sorted order. */
  711. list_defined_name = TAILQ_FIRST(self->defined_names);
  712. if (list_defined_name == NULL ||
  713. _compare_defined_names(defined_name, list_defined_name) < 1) {
  714. /* List is empty or defined name goes to the head. */
  715. TAILQ_INSERT_HEAD(self->defined_names, defined_name, list_pointers);
  716. return LXW_NO_ERROR;
  717. }
  718. TAILQ_FOREACH(list_defined_name, self->defined_names, list_pointers) {
  719. int res = _compare_defined_names(defined_name, list_defined_name);
  720. /* The entry already exists. We exit and don't overwrite. */
  721. if (res == 0)
  722. goto mem_error;
  723. /* New defined name is inserted in sorted order before other entries. */
  724. if (res < 0) {
  725. TAILQ_INSERT_BEFORE(list_defined_name, defined_name,
  726. list_pointers);
  727. return LXW_NO_ERROR;
  728. }
  729. }
  730. /* If the entry wasn't less than any of the entries in the list we add it
  731. * to the end. */
  732. TAILQ_INSERT_TAIL(self->defined_names, defined_name, list_pointers);
  733. return LXW_NO_ERROR;
  734. mem_error:
  735. free(defined_name);
  736. return LXW_ERROR_MEMORY_MALLOC_FAILED;
  737. }
  738. /*
  739. * Compare two defined_name structures.
  740. */
  741. static int
  742. _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b)
  743. {
  744. int res = strcmp(a->normalised_name, b->normalised_name);
  745. /* Primary comparison based on defined name. */
  746. if (res)
  747. return res;
  748. /* Secondary comparison based on worksheet name. */
  749. res = strcmp(a->normalised_sheetname, b->normalised_sheetname);
  750. return res;
  751. }
  752. /* Convert a chart range such as Sheet1!$A$1:$A$5 to a sheet name and row-col
  753. * dimensions, or vice-versa. This gives us the dimensions to read data back
  754. * from the worksheet.
  755. */
  756. STATIC void
  757. _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range)
  758. {
  759. char formula[LXW_MAX_FORMULA_RANGE_LENGTH] = { 0 };
  760. char *tmp_str;
  761. char *sheetname;
  762. /* If neither the range formula or sheetname is defined then this probably
  763. * isn't a valid range.
  764. */
  765. if (!range->formula && !range->sheetname) {
  766. range->ignore_cache = LXW_TRUE;
  767. return;
  768. }
  769. /* If the sheetname is already defined it was already set via
  770. * chart_series_set_categories() or chart_series_set_values().
  771. */
  772. if (range->sheetname)
  773. return;
  774. /* Ignore non-contiguous range like (Sheet1!$A$1:$A$2,Sheet1!$A$4:$A$5) */
  775. if (range->formula[0] == '(') {
  776. range->ignore_cache = LXW_TRUE;
  777. return;
  778. }
  779. /* Create a copy of the formula to modify and parse into parts. */
  780. lxw_snprintf(formula, LXW_MAX_FORMULA_RANGE_LENGTH, "%s", range->formula);
  781. /* Check for valid formula. TODO. This needs stronger validation. */
  782. tmp_str = strchr(formula, '!');
  783. if (tmp_str == NULL) {
  784. range->ignore_cache = LXW_TRUE;
  785. return;
  786. }
  787. else {
  788. /* Split the formulas into sheetname and row-col data. */
  789. *tmp_str = '\0';
  790. tmp_str++;
  791. sheetname = formula;
  792. /* Remove any worksheet quoting. */
  793. if (sheetname[0] == '\'')
  794. sheetname++;
  795. if (sheetname[strlen(sheetname) - 1] == '\'')
  796. sheetname[strlen(sheetname) - 1] = '\0';
  797. /* Check that the sheetname exists. */
  798. if (!workbook_get_worksheet_by_name(self, sheetname)) {
  799. LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
  800. "in chart formula '%s' doesn't exist.",
  801. sheetname, range->formula);
  802. range->ignore_cache = LXW_TRUE;
  803. return;
  804. }
  805. range->sheetname = lxw_strdup(sheetname);
  806. range->first_row = lxw_name_to_row(tmp_str);
  807. range->first_col = lxw_name_to_col(tmp_str);
  808. if (strchr(tmp_str, ':')) {
  809. /* 2D range. */
  810. range->last_row = lxw_name_to_row_2(tmp_str);
  811. range->last_col = lxw_name_to_col_2(tmp_str);
  812. }
  813. else {
  814. /* 1D range. */
  815. range->last_row = range->first_row;
  816. range->last_col = range->first_col;
  817. }
  818. }
  819. }
  820. /*
  821. * Populate the data cache of a chart data series by reading the data from the
  822. * relevant worksheet and adding it to the cached in the range object as a
  823. * list of points.
  824. *
  825. * Note, the data cache isn't strictly required by Excel but it helps if the
  826. * chart is embedded in another application such as PowerPoint and it also
  827. * helps with comparison testing.
  828. */
  829. STATIC void
  830. _populate_range_data_cache(lxw_workbook *self, lxw_series_range *range)
  831. {
  832. lxw_worksheet *worksheet;
  833. lxw_row_t row_num;
  834. lxw_col_t col_num;
  835. lxw_row *row_obj;
  836. lxw_cell *cell_obj;
  837. struct lxw_series_data_point *data_point;
  838. uint16_t num_data_points = 0;
  839. /* If ignore_cache is set then don't try to populate the cache. This flag
  840. * may be set manually, for testing, or due to a case where the cache
  841. * can't be calculated.
  842. */
  843. if (range->ignore_cache)
  844. return;
  845. /* Currently we only handle 2D ranges so ensure either the rows or cols
  846. * are the same.
  847. */
  848. if (range->first_row != range->last_row
  849. && range->first_col != range->last_col) {
  850. range->ignore_cache = LXW_TRUE;
  851. return;
  852. }
  853. /* Check that the sheetname exists. */
  854. worksheet = workbook_get_worksheet_by_name(self, range->sheetname);
  855. if (!worksheet) {
  856. LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
  857. "in chart formula '%s' doesn't exist.",
  858. range->sheetname, range->formula);
  859. range->ignore_cache = LXW_TRUE;
  860. return;
  861. }
  862. /* We can't read the data when worksheet optimization is on. */
  863. if (worksheet->optimize) {
  864. range->ignore_cache = LXW_TRUE;
  865. return;
  866. }
  867. /* Iterate through the worksheet data and populate the range cache. */
  868. for (row_num = range->first_row; row_num <= range->last_row; row_num++) {
  869. row_obj = lxw_worksheet_find_row(worksheet, row_num);
  870. for (col_num = range->first_col; col_num <= range->last_col;
  871. col_num++) {
  872. data_point = calloc(1, sizeof(struct lxw_series_data_point));
  873. if (!data_point) {
  874. range->ignore_cache = LXW_TRUE;
  875. return;
  876. }
  877. #if defined(LXW_VERSION_ID) && LXW_VERSION_ID >= 93
  878. cell_obj = lxw_worksheet_find_cell_in_row(row_obj, col_num);
  879. #else
  880. cell_obj = lxw_worksheet_find_cell(row_obj, col_num);
  881. #endif
  882. if (cell_obj) {
  883. if (cell_obj->type == NUMBER_CELL) {
  884. data_point->number = cell_obj->u.number;
  885. }
  886. if (cell_obj->type == STRING_CELL) {
  887. data_point->string = lxw_strdup(cell_obj->sst_string);
  888. data_point->is_string = LXW_TRUE;
  889. range->has_string_cache = LXW_TRUE;
  890. }
  891. }
  892. else {
  893. data_point->no_data = LXW_TRUE;
  894. }
  895. STAILQ_INSERT_TAIL(range->data_cache, data_point, list_pointers);
  896. num_data_points++;
  897. }
  898. }
  899. range->num_data_points = num_data_points;
  900. }
  901. /* Set the range dimensions and set the data cache.
  902. */
  903. STATIC void
  904. _populate_range(lxw_workbook *self, lxw_series_range *range)
  905. {
  906. _populate_range_dimensions(self, range);
  907. _populate_range_data_cache(self, range);
  908. }