write.c 26 KB

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