write.c 24 KB

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