write.c 26 KB

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