write.c 32 KB

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