write.c 41 KB

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