write.c 37 KB

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