write.c 39 KB

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