write.c 26 KB

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