write.c 25 KB

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