write.c 33 KB

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