write.c 31 KB

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