write.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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_write_t *res, zend_string *format, lxw_format *format_handle)
  17. {
  18. lxw_format *value_format = NULL;
  19. lxw_col_t lxw_col = (lxw_col_t)columns;
  20. lxw_row_t lxw_row = (lxw_row_t)row;
  21. zend_uchar value_type = Z_TYPE_P(value);
  22. if (value_type == IS_STRING) {
  23. zend_string *_zs_value = zval_get_string(value);
  24. int error = worksheet_write_string(res->worksheet, lxw_row, lxw_col, ZSTR_VAL(_zs_value), format_handle);
  25. zend_string_release(_zs_value);
  26. WORKSHEET_WRITER_EXCEPTION(error);
  27. return;
  28. }
  29. if (value_type == IS_LONG) {
  30. if (format != NULL && format_handle == NULL) {
  31. value_format = workbook_add_format(res->workbook);
  32. format_set_num_format(value_format, ZSTR_VAL(format));
  33. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), value_format));
  34. return;
  35. }
  36. if (format == NULL && format_handle != NULL) {
  37. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), format_handle));
  38. return;
  39. }
  40. if(format != NULL && format_handle != NULL) {
  41. value_format = workbook_add_format(res->workbook);
  42. format_copy(value_format, format_handle);
  43. format_set_num_format(value_format, ZSTR_VAL(format));
  44. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), value_format));
  45. return;
  46. }
  47. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, (double)zval_get_long(value), NULL));
  48. }
  49. if (value_type == IS_DOUBLE) {
  50. if (format != NULL && format_handle == NULL) {
  51. value_format = workbook_add_format(res->workbook);
  52. format_set_num_format(value_format, ZSTR_VAL(format));
  53. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format));
  54. return;
  55. }
  56. if (format == NULL && format_handle != NULL) {
  57. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), format_handle));
  58. return;
  59. }
  60. if(format != NULL && format_handle != NULL) {
  61. value_format = workbook_add_format(res->workbook);
  62. format_copy(value_format, format_handle);
  63. format_set_num_format(value_format, ZSTR_VAL(format));
  64. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format));
  65. return;
  66. }
  67. WORKSHEET_WRITER_EXCEPTION(worksheet_write_number(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, zval_get_double(value), NULL));
  68. return;
  69. }
  70. }
  71. void format_copy(lxw_format *new_format, lxw_format *other_format)
  72. {
  73. new_format->bold = other_format->bold;
  74. new_format->bg_color = other_format->bg_color;
  75. new_format->border_count = other_format->border_count;
  76. new_format->border_index = other_format->border_index;
  77. new_format->bottom = other_format->bottom;
  78. new_format->bottom_color = other_format->bottom_color;
  79. new_format->color_indexed = other_format->color_indexed;
  80. new_format->diag_border = other_format->diag_border;
  81. new_format->diag_color = other_format->diag_color;
  82. new_format->font_size = other_format->font_size;
  83. new_format->bold = other_format->bold;
  84. new_format->italic = other_format->italic;
  85. new_format->font_color = other_format->font_color;
  86. new_format->underline = other_format->underline;
  87. new_format->font_strikeout = other_format->font_strikeout;
  88. new_format->font_outline = other_format->font_outline;
  89. new_format->font_shadow = other_format->font_shadow;
  90. new_format->font_script = other_format->font_script;
  91. new_format->font_family = other_format->font_family;
  92. new_format->font_charset = other_format->font_charset;
  93. new_format->font_condense = other_format->font_condense;
  94. new_format->font_extend = other_format->font_extend;
  95. new_format->theme = other_format->theme;
  96. new_format->hyperlink = other_format->hyperlink;
  97. new_format->hidden = other_format->hidden;
  98. new_format->locked = other_format->locked;
  99. new_format->text_h_align = other_format->text_h_align;
  100. new_format->text_wrap = other_format->text_wrap;
  101. new_format->text_v_align = other_format->text_v_align;
  102. new_format->text_justlast = other_format->text_justlast;
  103. new_format->rotation = other_format->rotation;
  104. new_format->fg_color = other_format->fg_color;
  105. new_format->bg_color = other_format->bg_color;
  106. new_format->pattern = other_format->pattern;
  107. new_format->has_fill = other_format->has_fill;
  108. new_format->has_dxf_fill = other_format->has_dxf_fill;
  109. new_format->fill_index = other_format->fill_index;
  110. new_format->fill_count = other_format->fill_count;
  111. new_format->border_index = other_format->border_index;
  112. new_format->has_border = other_format->has_border;
  113. new_format->has_dxf_border = other_format->has_dxf_border;
  114. new_format->border_count = other_format->border_count;
  115. new_format->bottom = other_format->bottom;
  116. new_format->diag_border = other_format->diag_border;
  117. new_format->diag_type = other_format->diag_type;
  118. new_format->left = other_format->left;
  119. new_format->right = other_format->right;
  120. new_format->top = other_format->top;
  121. new_format->bottom_color = other_format->bottom_color;
  122. new_format->diag_color = other_format->diag_color;
  123. new_format->left_color = other_format->left_color;
  124. new_format->right_color = other_format->right_color;
  125. new_format->top_color = other_format->top_color;
  126. new_format->indent = other_format->indent;
  127. new_format->shrink = other_format->shrink;
  128. new_format->merge_range = other_format->merge_range;
  129. new_format->reading_order = other_format->reading_order;
  130. new_format->just_distrib = other_format->just_distrib;
  131. new_format->color_indexed = other_format->color_indexed;
  132. new_format->font_only = other_format->font_only;
  133. }
  134. void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *url, zend_string *text, zend_string *tool_tip, lxw_format *format)
  135. {
  136. if (text == NULL && tool_tip == NULL) {
  137. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, NULL, NULL);
  138. return;
  139. }
  140. if (text == NULL && tool_tip != NULL) {
  141. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, NULL, ZSTR_VAL(tool_tip));
  142. return;
  143. }
  144. if (text != NULL && tool_tip == NULL) {
  145. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, ZSTR_VAL(text), NULL);
  146. return;
  147. }
  148. worksheet_write_url_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(url), format, ZSTR_VAL(text), ZSTR_VAL(tool_tip));
  149. }
  150. /*
  151. * Write the image to the file
  152. */
  153. void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res)
  154. {
  155. lxw_image_options options = {.x_scale = width, .y_scale = height};
  156. worksheet_insert_image_opt(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(zval_get_string(value)), &options);
  157. }
  158. /*
  159. * Write the image to the file
  160. */
  161. void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format)
  162. {
  163. worksheet_write_formula(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(value), format);
  164. }
  165. /*
  166. * Write the chart to the file
  167. */
  168. void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res)
  169. {
  170. worksheet_insert_chart(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, chart_resource->chart);
  171. }
  172. /*
  173. * Write the datetime to the file
  174. */
  175. void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle)
  176. {
  177. lxw_format *value_format = workbook_add_format(res->workbook);
  178. if (format_handle != NULL) {
  179. format_copy(value_format, format_handle);
  180. }
  181. format_set_num_format(value_format, ZSTR_VAL(format));
  182. worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, value_format);
  183. }
  184. /*
  185. * Write the comment to the cell
  186. */
  187. void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res)
  188. {
  189. int error = worksheet_write_comment(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(comment));
  190. WORKSHEET_WRITER_EXCEPTION(error);
  191. }
  192. /*
  193. * Show all comments
  194. */
  195. void comment_show(xls_resource_write_t *res)
  196. {
  197. worksheet_show_comments(res->worksheet);
  198. }
  199. /*
  200. * Add the autofilter.
  201. */
  202. void auto_filter(zend_string *range, xls_resource_write_t *res)
  203. {
  204. int error = worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));
  205. // Cells that have been placed cannot be modified using optimization mode
  206. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  207. // Worksheet row or column index out of range
  208. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  209. }
  210. /*
  211. * Merge cells.
  212. */
  213. void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format)
  214. {
  215. char *_range = ZSTR_VAL(range);
  216. int error = worksheet_merge_range(res->worksheet, RANGE(_range), "", format);
  217. // Cells that have been placed cannot be modified using optimization mode
  218. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  219. // Worksheet row or column index out of range
  220. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  221. // writer merge cell
  222. type_writer(value, lxw_name_to_row(_range), lxw_name_to_col(_range), res, NULL, format);
  223. }
  224. /*
  225. * Set column format
  226. */
  227. void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format)
  228. {
  229. worksheet_set_column(res->worksheet, COLS(ZSTR_VAL(range)), width, format);
  230. }
  231. /*
  232. * Set row format
  233. */
  234. void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format)
  235. {
  236. char *rows = ZSTR_VAL(range);
  237. if (strchr(rows, ':')) {
  238. worksheet_set_rows(ROWS(rows), height, res, format);
  239. } else {
  240. int error = worksheet_set_row(res->worksheet, ROW(rows), height, format);
  241. // Cells that have been placed cannot be modified using optimization mode
  242. WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
  243. // Worksheet row or column index out of range
  244. WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
  245. }
  246. }
  247. /*
  248. * Add data validations to a worksheet
  249. */
  250. void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validation *validation)
  251. {
  252. char *rangeStr = ZSTR_VAL(range);
  253. if (strchr(rangeStr, ':')) {
  254. worksheet_data_validation_range(res->worksheet, RANGE(rangeStr), validation);
  255. } else {
  256. worksheet_data_validation_cell(res->worksheet, CELL(rangeStr), validation);
  257. }
  258. }
  259. /*
  260. * Set rows format
  261. */
  262. void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format)
  263. {
  264. while (1) {
  265. worksheet_set_row(res->worksheet, end, height, format);
  266. if (end == start)
  267. break;
  268. end--;
  269. }
  270. }
  271. /*
  272. * Set freeze panes
  273. */
  274. void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column)
  275. {
  276. worksheet_freeze_panes(res->worksheet, row, column);
  277. }
  278. /*
  279. * Display or hide screen and print gridlines
  280. */
  281. void gridlines(xls_resource_write_t *res, zend_long option)
  282. {
  283. worksheet_gridlines(res->worksheet, option);
  284. }
  285. /*
  286. * Set the worksheet zoom factor
  287. */
  288. void zoom(xls_resource_write_t *res, zend_long zoom)
  289. {
  290. worksheet_set_zoom(res->worksheet, zoom);
  291. }
  292. /*
  293. * Set the worksheet protection
  294. */
  295. void protection(xls_resource_write_t *res, zend_string *password)
  296. {
  297. if (password == NULL) {
  298. worksheet_protect(res->worksheet, NULL, NULL);
  299. } else {
  300. worksheet_protect(res->worksheet, ZSTR_VAL(password), NULL);
  301. }
  302. }
  303. /*
  304. * Set the worksheet printed direction
  305. */
  306. void printed_direction(xls_resource_write_t *res, unsigned int direction)
  307. {
  308. if (direction == XLSWRITER_PRINTED_PORTRAIT) {
  309. worksheet_set_portrait(res->worksheet);
  310. }
  311. worksheet_set_landscape(res->worksheet);
  312. }
  313. /*
  314. * Hide worksheet
  315. */
  316. void hide_worksheet(xls_resource_write_t *res)
  317. {
  318. worksheet_hide(res->worksheet);
  319. }
  320. /*
  321. * First worksheet
  322. */
  323. void first_worksheet(xls_resource_write_t *res)
  324. {
  325. worksheet_set_first_sheet(res->worksheet);
  326. }
  327. /*
  328. * Call finalization code and close file.
  329. */
  330. lxw_error
  331. workbook_file(xls_resource_write_t *self)
  332. {
  333. lxw_worksheet *worksheet = NULL;
  334. lxw_packager *packager = NULL;
  335. lxw_error error = LXW_NO_ERROR;
  336. /* Add a default worksheet if non have been added. */
  337. if (!self->workbook->num_sheets)
  338. workbook_add_worksheet(self->workbook, NULL);
  339. /* Ensure that at least one worksheet has been selected. */
  340. if (self->workbook->active_sheet == 0) {
  341. worksheet = STAILQ_FIRST(self->workbook->worksheets);
  342. worksheet->selected = 1;
  343. worksheet->hidden = 0;
  344. }
  345. /* Set the active sheet. */
  346. STAILQ_FOREACH(worksheet, self->workbook->worksheets, list_pointers) {
  347. if (worksheet->index == self->workbook->active_sheet)
  348. worksheet->active = 1;
  349. }
  350. /* Prepare the worksheet VML elements such as comments. */
  351. _prepare_vml(self->workbook);
  352. /* Set the defined names for the worksheets such as Print Titles. */
  353. _prepare_defined_names(self->workbook);
  354. /* Prepare the drawings, charts and images. */
  355. _prepare_drawings(self->workbook);
  356. /* Add cached data to charts. */
  357. _add_chart_cache_data(self->workbook);
  358. /* Create a packager object to assemble sub-elements into a zip file. */
  359. packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, self->workbook->options.use_zip64);
  360. /* If the packager fails it is generally due to a zip permission error. */
  361. if (packager == NULL) {
  362. fprintf(stderr, "[ERROR] workbook_close(): "
  363. "Error creating '%s'. "
  364. "Error = %s\n", self->workbook->filename, strerror(errno));
  365. error = LXW_ERROR_CREATING_XLSX_FILE;
  366. goto mem_error;
  367. }
  368. /* Set the workbook object in the packager. */
  369. packager->workbook = self->workbook;
  370. /* Assemble all the sub-files in the xlsx package. */
  371. error = lxw_create_package(packager);
  372. /* Error and non-error conditions fall through to the cleanup code. */
  373. if (error == LXW_ERROR_CREATING_TMPFILE) {
  374. fprintf(stderr, "[ERROR] workbook_close(): "
  375. "Error creating tmpfile(s) to assemble '%s'. "
  376. "Error = %s\n", self->workbook->filename, strerror(errno));
  377. }
  378. /* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zlib. */
  379. if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
  380. fprintf(stderr, "[ERROR] workbook_close(): "
  381. "Zlib error while creating xlsx file '%s'. "
  382. "Error = %s\n", self->workbook->filename, strerror(errno));
  383. }
  384. /* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
  385. if (error == LXW_ERROR_ZIP_PARAMETER_ERROR) {
  386. fprintf(stderr, "[ERROR] workbook_close(): "
  387. "Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
  388. "System error = %s\n", self->workbook->filename, strerror(errno));
  389. }
  390. /* If LXW_ERROR_ZIP_BAD_ZIP_FILE then errno is set by zip. */
  391. if (error == LXW_ERROR_ZIP_BAD_ZIP_FILE) {
  392. fprintf(stderr, "[ERROR] workbook_close(): "
  393. "Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
  394. "This may require the use_zip64 option for large files. "
  395. "System error = %s\n", self->workbook->filename, strerror(errno));
  396. }
  397. /* If LXW_ERROR_ZIP_INTERNAL_ERROR then errno is set by zip. */
  398. if (error == LXW_ERROR_ZIP_INTERNAL_ERROR) {
  399. fprintf(stderr, "[ERROR] workbook_close(): "
  400. "Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
  401. "System error = %s\n", self->workbook->filename, strerror(errno));
  402. }
  403. /* The next 2 error conditions don't set errno. */
  404. if (error == LXW_ERROR_ZIP_FILE_ADD) {
  405. fprintf(stderr, "[ERROR] workbook_close(): "
  406. "Zlib error adding file to xlsx file '%s'.\n",
  407. self->workbook->filename);
  408. }
  409. if (error == LXW_ERROR_ZIP_CLOSE) {
  410. fprintf(stderr, "[ERROR] workbook_close(): "
  411. "Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
  412. }
  413. mem_error:
  414. lxw_packager_free(packager);
  415. return error;
  416. }
  417. void _php_vtiful_xls_close(zend_resource *rsrc TSRMLS_DC)
  418. {
  419. }
  420. /*
  421. * Iterate through the worksheets and set up the VML objects.
  422. */
  423. STATIC void
  424. _prepare_vml(lxw_workbook *self)
  425. {
  426. lxw_worksheet *worksheet;
  427. lxw_sheet *sheet;
  428. uint32_t comment_id = 0;
  429. uint32_t vml_drawing_id = 0;
  430. uint32_t vml_data_id = 1;
  431. uint32_t vml_shape_id = 1024;
  432. uint32_t comment_count = 0;
  433. STAILQ_FOREACH(sheet, self->sheets, list_pointers) {
  434. if (sheet->is_chartsheet)
  435. continue;
  436. else
  437. worksheet = sheet->u.worksheet;
  438. if (!worksheet->has_vml && !worksheet->has_header_vml)
  439. continue;
  440. if (worksheet->has_vml) {
  441. self->has_vml = LXW_TRUE;
  442. if (worksheet->has_comments) {
  443. self->comment_count += 1;
  444. comment_id += 1;
  445. self->has_comments = LXW_TRUE;
  446. }
  447. vml_drawing_id += 1;
  448. comment_count = lxw_worksheet_prepare_vml_objects(worksheet,
  449. vml_data_id,
  450. vml_shape_id,
  451. vml_drawing_id,
  452. comment_id);
  453. /* Each VML should start with a shape id incremented by 1024. */
  454. vml_data_id += 1 * ((1024 + comment_count) / 1024);
  455. vml_shape_id += 1024 * ((1024 + comment_count) / 1024);
  456. }
  457. }
  458. }
  459. /*
  460. * Iterate through the worksheets and store any defined names used for print
  461. * ranges or repeat rows/columns.
  462. */
  463. STATIC void
  464. _prepare_defined_names(lxw_workbook *self)
  465. {
  466. lxw_worksheet *worksheet;
  467. char app_name[LXW_DEFINED_NAME_LENGTH];
  468. char range[LXW_DEFINED_NAME_LENGTH];
  469. char area[LXW_MAX_CELL_RANGE_LENGTH];
  470. char first_col[8];
  471. char last_col[8];
  472. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  473. /*
  474. * Check for autofilter settings and store them.
  475. */
  476. if (worksheet->autofilter.in_use) {
  477. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  478. "%s!_FilterDatabase", worksheet->quoted_name);
  479. lxw_rowcol_to_range_abs(area,
  480. worksheet->autofilter.first_row,
  481. worksheet->autofilter.first_col,
  482. worksheet->autofilter.last_row,
  483. worksheet->autofilter.last_col);
  484. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH, "%s!%s",
  485. worksheet->quoted_name, area);
  486. /* Autofilters are the only defined name to set the hidden flag. */
  487. _store_defined_name(self, "_xlnm._FilterDatabase", app_name, range, worksheet->index, LXW_TRUE);
  488. }
  489. /*
  490. * Check for Print Area settings and store them.
  491. */
  492. if (worksheet->print_area.in_use) {
  493. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  494. "%s!Print_Area", worksheet->quoted_name);
  495. /* Check for print area that is the max row range. */
  496. if (worksheet->print_area.first_row == 0
  497. && worksheet->print_area.last_row == LXW_ROW_MAX - 1) {
  498. lxw_col_to_name(first_col,
  499. worksheet->print_area.first_col, LXW_FALSE);
  500. lxw_col_to_name(last_col,
  501. worksheet->print_area.last_col, LXW_FALSE);
  502. lxw_snprintf(area, LXW_MAX_CELL_RANGE_LENGTH - 1, "$%s:$%s",
  503. first_col, last_col);
  504. }
  505. /* Check for print area that is the max column range. */
  506. else if (worksheet->print_area.first_col == 0
  507. && worksheet->print_area.last_col == LXW_COL_MAX - 1) {
  508. lxw_snprintf(area, LXW_MAX_CELL_RANGE_LENGTH - 1, "$%d:$%d",
  509. worksheet->print_area.first_row + 1,
  510. worksheet->print_area.last_row + 1);
  511. }
  512. else {
  513. lxw_rowcol_to_range_abs(area,
  514. worksheet->print_area.first_row,
  515. worksheet->print_area.first_col,
  516. worksheet->print_area.last_row,
  517. worksheet->print_area.last_col);
  518. }
  519. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH, "%s!%s",
  520. worksheet->quoted_name, area);
  521. _store_defined_name(self, "_xlnm.Print_Area", app_name,
  522. range, worksheet->index, LXW_FALSE);
  523. }
  524. /*
  525. * Check for repeat rows/cols. aka, Print Titles and store them.
  526. */
  527. if (worksheet->repeat_rows.in_use || worksheet->repeat_cols.in_use) {
  528. if (worksheet->repeat_rows.in_use
  529. && worksheet->repeat_cols.in_use) {
  530. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  531. "%s!Print_Titles", worksheet->quoted_name);
  532. lxw_col_to_name(first_col,
  533. worksheet->repeat_cols.first_col, LXW_FALSE);
  534. lxw_col_to_name(last_col,
  535. worksheet->repeat_cols.last_col, LXW_FALSE);
  536. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  537. "%s!$%s:$%s,%s!$%d:$%d",
  538. worksheet->quoted_name, first_col,
  539. last_col, worksheet->quoted_name,
  540. worksheet->repeat_rows.first_row + 1,
  541. worksheet->repeat_rows.last_row + 1);
  542. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  543. range, worksheet->index, LXW_FALSE);
  544. }
  545. else if (worksheet->repeat_rows.in_use) {
  546. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  547. "%s!Print_Titles", worksheet->quoted_name);
  548. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  549. "%s!$%d:$%d", worksheet->quoted_name,
  550. worksheet->repeat_rows.first_row + 1,
  551. worksheet->repeat_rows.last_row + 1);
  552. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  553. range, worksheet->index, LXW_FALSE);
  554. }
  555. else if (worksheet->repeat_cols.in_use) {
  556. lxw_snprintf(app_name, LXW_DEFINED_NAME_LENGTH,
  557. "%s!Print_Titles", worksheet->quoted_name);
  558. lxw_col_to_name(first_col,
  559. worksheet->repeat_cols.first_col, LXW_FALSE);
  560. lxw_col_to_name(last_col,
  561. worksheet->repeat_cols.last_col, LXW_FALSE);
  562. lxw_snprintf(range, LXW_DEFINED_NAME_LENGTH,
  563. "%s!$%s:$%s", worksheet->quoted_name,
  564. first_col, last_col);
  565. _store_defined_name(self, "_xlnm.Print_Titles", app_name,
  566. range, worksheet->index, LXW_FALSE);
  567. }
  568. }
  569. }
  570. }
  571. /*
  572. * Iterate through the worksheets and set up any chart or image drawings.
  573. */
  574. STATIC void
  575. _prepare_drawings(lxw_workbook *self)
  576. {
  577. lxw_worksheet *worksheet;
  578. lxw_object_properties *image_options;
  579. uint16_t chart_ref_id = 0;
  580. uint16_t image_ref_id = 0;
  581. uint16_t drawing_id = 0;
  582. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  583. if (STAILQ_EMPTY(worksheet->image_props)
  584. && STAILQ_EMPTY(worksheet->chart_data))
  585. continue;
  586. drawing_id++;
  587. STAILQ_FOREACH(image_options, worksheet->chart_data, list_pointers) {
  588. chart_ref_id++;
  589. lxw_worksheet_prepare_chart(worksheet, chart_ref_id, drawing_id,
  590. image_options, 0);
  591. if (image_options->chart)
  592. STAILQ_INSERT_TAIL(self->ordered_charts, image_options->chart,
  593. ordered_list_pointers);
  594. }
  595. STAILQ_FOREACH(image_options, worksheet->image_props, list_pointers) {
  596. if (image_options->image_type == LXW_IMAGE_PNG)
  597. self->has_png = LXW_TRUE;
  598. if (image_options->image_type == LXW_IMAGE_JPEG)
  599. self->has_jpeg = LXW_TRUE;
  600. if (image_options->image_type == LXW_IMAGE_BMP)
  601. self->has_bmp = LXW_TRUE;
  602. image_ref_id++;
  603. lxw_worksheet_prepare_image(worksheet, image_ref_id, drawing_id,
  604. image_options);
  605. }
  606. }
  607. self->drawing_count = drawing_id;
  608. }
  609. /*
  610. * Add "cached" data to charts to provide the numCache and strCache data for
  611. * series and title/axis ranges.
  612. */
  613. STATIC void
  614. _add_chart_cache_data(lxw_workbook *self)
  615. {
  616. lxw_chart *chart;
  617. lxw_chart_series *series;
  618. STAILQ_FOREACH(chart, self->ordered_charts, ordered_list_pointers) {
  619. _populate_range(self, chart->title.range);
  620. _populate_range(self, chart->x_axis->title.range);
  621. _populate_range(self, chart->y_axis->title.range);
  622. if (STAILQ_EMPTY(chart->series_list))
  623. continue;
  624. STAILQ_FOREACH(series, chart->series_list, list_pointers) {
  625. _populate_range(self, series->categories);
  626. _populate_range(self, series->values);
  627. _populate_range(self, series->title.range);
  628. }
  629. }
  630. }
  631. /*
  632. * Process and store the defined names. The defined names are stored with
  633. * the Workbook.xml but also with the App.xml if they refer to a sheet
  634. * range like "Sheet1!:A1". The defined names are store in sorted
  635. * order for consistency with Excel. The names need to be normalized before
  636. * sorting.
  637. */
  638. STATIC lxw_error
  639. _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden)
  640. {
  641. lxw_worksheet *worksheet;
  642. lxw_defined_name *defined_name;
  643. lxw_defined_name *list_defined_name;
  644. char name_copy[LXW_DEFINED_NAME_LENGTH];
  645. char *tmp_str;
  646. char *worksheet_name;
  647. /* Do some checks on the input data */
  648. if (!name || !formula)
  649. return LXW_ERROR_NULL_PARAMETER_IGNORED;
  650. if (lxw_utf8_strlen(name) > LXW_DEFINED_NAME_LENGTH ||
  651. lxw_utf8_strlen(formula) > LXW_DEFINED_NAME_LENGTH) {
  652. return LXW_ERROR_128_STRING_LENGTH_EXCEEDED;
  653. }
  654. /* Allocate a new defined_name to be added to the linked list of names. */
  655. defined_name = calloc(1, sizeof(struct lxw_defined_name));
  656. RETURN_ON_MEM_ERROR(defined_name, LXW_ERROR_MEMORY_MALLOC_FAILED);
  657. /* Copy the user input string. */
  658. lxw_strcpy(name_copy, name);
  659. /* Set the worksheet index or -1 for a global defined name. */
  660. defined_name->index = index;
  661. defined_name->hidden = hidden;
  662. /* Check for local defined names like like "Sheet1!name". */
  663. tmp_str = strchr(name_copy, '!');
  664. if (tmp_str == NULL) {
  665. /* The name is global. We just store the defined name string. */
  666. lxw_strcpy(defined_name->name, name_copy);
  667. }
  668. else {
  669. /* The name is worksheet local. We need to extract the sheet name
  670. * and map it to a sheet index. */
  671. /* Split the into the worksheet name and defined name. */
  672. *tmp_str = '\0';
  673. tmp_str++;
  674. worksheet_name = name_copy;
  675. /* Remove any worksheet quoting. */
  676. if (worksheet_name[0] == '\'')
  677. worksheet_name++;
  678. if (worksheet_name[strlen(worksheet_name) - 1] == '\'')
  679. worksheet_name[strlen(worksheet_name) - 1] = '\0';
  680. /* Search for worksheet name to get the equivalent worksheet index. */
  681. STAILQ_FOREACH(worksheet, self->worksheets, list_pointers) {
  682. if (strcmp(worksheet_name, worksheet->name) == 0) {
  683. defined_name->index = worksheet->index;
  684. lxw_strcpy(defined_name->normalised_sheetname,
  685. worksheet_name);
  686. }
  687. }
  688. /* If we didn't find the worksheet name we exit. */
  689. if (defined_name->index == -1)
  690. goto mem_error;
  691. lxw_strcpy(defined_name->name, tmp_str);
  692. }
  693. /* Print titles and repeat title pass in the name used for App.xml. */
  694. if (app_name) {
  695. lxw_strcpy(defined_name->app_name, app_name);
  696. lxw_strcpy(defined_name->normalised_sheetname, app_name);
  697. }
  698. else {
  699. lxw_strcpy(defined_name->app_name, name);
  700. }
  701. /* We need to normalize the defined names for sorting. This involves
  702. * removing any _xlnm namespace and converting it to lowercase. */
  703. tmp_str = strstr(name_copy, "_xlnm.");
  704. if (tmp_str)
  705. lxw_strcpy(defined_name->normalised_name, defined_name->name + 6);
  706. else
  707. lxw_strcpy(defined_name->normalised_name, defined_name->name);
  708. lxw_str_tolower(defined_name->normalised_name);
  709. lxw_str_tolower(defined_name->normalised_sheetname);
  710. /* Strip leading "=" from the formula. */
  711. if (formula[0] == '=')
  712. lxw_strcpy(defined_name->formula, formula + 1);
  713. else
  714. lxw_strcpy(defined_name->formula, formula);
  715. /* We add the defined name to the list in sorted order. */
  716. list_defined_name = TAILQ_FIRST(self->defined_names);
  717. if (list_defined_name == NULL ||
  718. _compare_defined_names(defined_name, list_defined_name) < 1) {
  719. /* List is empty or defined name goes to the head. */
  720. TAILQ_INSERT_HEAD(self->defined_names, defined_name, list_pointers);
  721. return LXW_NO_ERROR;
  722. }
  723. TAILQ_FOREACH(list_defined_name, self->defined_names, list_pointers) {
  724. int res = _compare_defined_names(defined_name, list_defined_name);
  725. /* The entry already exists. We exit and don't overwrite. */
  726. if (res == 0)
  727. goto mem_error;
  728. /* New defined name is inserted in sorted order before other entries. */
  729. if (res < 0) {
  730. TAILQ_INSERT_BEFORE(list_defined_name, defined_name,
  731. list_pointers);
  732. return LXW_NO_ERROR;
  733. }
  734. }
  735. /* If the entry wasn't less than any of the entries in the list we add it
  736. * to the end. */
  737. TAILQ_INSERT_TAIL(self->defined_names, defined_name, list_pointers);
  738. return LXW_NO_ERROR;
  739. mem_error:
  740. free(defined_name);
  741. return LXW_ERROR_MEMORY_MALLOC_FAILED;
  742. }
  743. /*
  744. * Compare two defined_name structures.
  745. */
  746. static int
  747. _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b)
  748. {
  749. int res = strcmp(a->normalised_name, b->normalised_name);
  750. /* Primary comparison based on defined name. */
  751. if (res)
  752. return res;
  753. /* Secondary comparison based on worksheet name. */
  754. res = strcmp(a->normalised_sheetname, b->normalised_sheetname);
  755. return res;
  756. }
  757. /* Convert a chart range such as Sheet1!$A$1:$A$5 to a sheet name and row-col
  758. * dimensions, or vice-versa. This gives us the dimensions to read data back
  759. * from the worksheet.
  760. */
  761. STATIC void
  762. _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range)
  763. {
  764. char formula[LXW_MAX_FORMULA_RANGE_LENGTH] = { 0 };
  765. char *tmp_str;
  766. char *sheetname;
  767. /* If neither the range formula or sheetname is defined then this probably
  768. * isn't a valid range.
  769. */
  770. if (!range->formula && !range->sheetname) {
  771. range->ignore_cache = LXW_TRUE;
  772. return;
  773. }
  774. /* If the sheetname is already defined it was already set via
  775. * chart_series_set_categories() or chart_series_set_values().
  776. */
  777. if (range->sheetname)
  778. return;
  779. /* Ignore non-contiguous range like (Sheet1!$A$1:$A$2,Sheet1!$A$4:$A$5) */
  780. if (range->formula[0] == '(') {
  781. range->ignore_cache = LXW_TRUE;
  782. return;
  783. }
  784. /* Create a copy of the formula to modify and parse into parts. */
  785. lxw_snprintf(formula, LXW_MAX_FORMULA_RANGE_LENGTH, "%s", range->formula);
  786. /* Check for valid formula. TODO. This needs stronger validation. */
  787. tmp_str = strchr(formula, '!');
  788. if (tmp_str == NULL) {
  789. range->ignore_cache = LXW_TRUE;
  790. return;
  791. }
  792. else {
  793. /* Split the formulas into sheetname and row-col data. */
  794. *tmp_str = '\0';
  795. tmp_str++;
  796. sheetname = formula;
  797. /* Remove any worksheet quoting. */
  798. if (sheetname[0] == '\'')
  799. sheetname++;
  800. if (sheetname[strlen(sheetname) - 1] == '\'')
  801. sheetname[strlen(sheetname) - 1] = '\0';
  802. /* Check that the sheetname exists. */
  803. if (!workbook_get_worksheet_by_name(self, sheetname)) {
  804. LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
  805. "in chart formula '%s' doesn't exist.",
  806. sheetname, range->formula);
  807. range->ignore_cache = LXW_TRUE;
  808. return;
  809. }
  810. range->sheetname = lxw_strdup(sheetname);
  811. range->first_row = lxw_name_to_row(tmp_str);
  812. range->first_col = lxw_name_to_col(tmp_str);
  813. if (strchr(tmp_str, ':')) {
  814. /* 2D range. */
  815. range->last_row = lxw_name_to_row_2(tmp_str);
  816. range->last_col = lxw_name_to_col_2(tmp_str);
  817. }
  818. else {
  819. /* 1D range. */
  820. range->last_row = range->first_row;
  821. range->last_col = range->first_col;
  822. }
  823. }
  824. }
  825. /*
  826. * Populate the data cache of a chart data series by reading the data from the
  827. * relevant worksheet and adding it to the cached in the range object as a
  828. * list of points.
  829. *
  830. * Note, the data cache isn't strictly required by Excel but it helps if the
  831. * chart is embedded in another application such as PowerPoint and it also
  832. * helps with comparison testing.
  833. */
  834. STATIC void
  835. _populate_range_data_cache(lxw_workbook *self, lxw_series_range *range)
  836. {
  837. lxw_worksheet *worksheet;
  838. lxw_row_t row_num;
  839. lxw_col_t col_num;
  840. lxw_row *row_obj;
  841. lxw_cell *cell_obj;
  842. struct lxw_series_data_point *data_point;
  843. uint16_t num_data_points = 0;
  844. /* If ignore_cache is set then don't try to populate the cache. This flag
  845. * may be set manually, for testing, or due to a case where the cache
  846. * can't be calculated.
  847. */
  848. if (range->ignore_cache)
  849. return;
  850. /* Currently we only handle 2D ranges so ensure either the rows or cols
  851. * are the same.
  852. */
  853. if (range->first_row != range->last_row
  854. && range->first_col != range->last_col) {
  855. range->ignore_cache = LXW_TRUE;
  856. return;
  857. }
  858. /* Check that the sheetname exists. */
  859. worksheet = workbook_get_worksheet_by_name(self, range->sheetname);
  860. if (!worksheet) {
  861. LXW_WARN_FORMAT2("workbook_add_chart(): worksheet name '%s' "
  862. "in chart formula '%s' doesn't exist.",
  863. range->sheetname, range->formula);
  864. range->ignore_cache = LXW_TRUE;
  865. return;
  866. }
  867. /* We can't read the data when worksheet optimization is on. */
  868. if (worksheet->optimize) {
  869. range->ignore_cache = LXW_TRUE;
  870. return;
  871. }
  872. /* Iterate through the worksheet data and populate the range cache. */
  873. for (row_num = range->first_row; row_num <= range->last_row; row_num++) {
  874. row_obj = lxw_worksheet_find_row(worksheet, row_num);
  875. for (col_num = range->first_col; col_num <= range->last_col;
  876. col_num++) {
  877. data_point = calloc(1, sizeof(struct lxw_series_data_point));
  878. if (!data_point) {
  879. range->ignore_cache = LXW_TRUE;
  880. return;
  881. }
  882. #if defined(LXW_VERSION_ID) && LXW_VERSION_ID >= 93
  883. cell_obj = lxw_worksheet_find_cell_in_row(row_obj, col_num);
  884. #else
  885. cell_obj = lxw_worksheet_find_cell(row_obj, col_num);
  886. #endif
  887. if (cell_obj) {
  888. if (cell_obj->type == NUMBER_CELL) {
  889. data_point->number = cell_obj->u.number;
  890. }
  891. if (cell_obj->type == STRING_CELL) {
  892. data_point->string = lxw_strdup(cell_obj->sst_string);
  893. data_point->is_string = LXW_TRUE;
  894. range->has_string_cache = LXW_TRUE;
  895. }
  896. }
  897. else {
  898. data_point->no_data = LXW_TRUE;
  899. }
  900. STAILQ_INSERT_TAIL(range->data_cache, data_point, list_pointers);
  901. num_data_points++;
  902. }
  903. }
  904. range->num_data_points = num_data_points;
  905. }
  906. /* Set the range dimensions and set the data cache.
  907. */
  908. STATIC void
  909. _populate_range(lxw_workbook *self, lxw_series_range *range)
  910. {
  911. _populate_range_dimensions(self, range);
  912. _populate_range_data_cache(self, range);
  913. }