write.c 38 KB

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