write.c 38 KB

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