xlswriter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifndef PHP_XLS_WRITER_INCLUDE_H
  13. #define PHP_XLS_WRITER_INCLUDE_H
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include <php.h>
  18. #include "zend_smart_str.h"
  19. #include "zend_exceptions.h"
  20. #include "zend.h"
  21. #include "zend_API.h"
  22. #include "php.h"
  23. #include "xlsxwriter.h"
  24. #include "xlsxwriter/packager.h"
  25. #include "xlsxwriter/format.h"
  26. #include "php_xlswriter.h"
  27. #include "excel.h"
  28. #include "exception.h"
  29. #include "format.h"
  30. #include "chart.h"
  31. #include "help.h"
  32. #ifdef ENABLE_READER
  33. #include "xlsxio_read.h"
  34. #include "read.h"
  35. #include "csv.h"
  36. typedef struct {
  37. xlsxioreader file_t;
  38. xlsxioreadersheet sheet_t;
  39. zend_long data_type_default;
  40. zend_long sheet_flag;
  41. } xls_resource_read_t;
  42. typedef struct {
  43. zend_long data_type_default;
  44. zval *zv_type_t;
  45. zend_fcall_info *fci;
  46. zend_fcall_info_cache *fci_cache;
  47. } xls_read_callback_data;
  48. #endif
  49. #ifndef ENABLE_READER
  50. typedef struct {
  51. void * file_t;
  52. void * sheet_t;
  53. } xls_resource_read_t;
  54. #endif
  55. enum xlswriter_boolean {
  56. XLSWRITER_FALSE,
  57. XLSWRITER_TRUE
  58. };
  59. enum xlswirter_printed_direction {
  60. XLSWRITER_PRINTED_LANDSCAPE,
  61. XLSWRITER_PRINTED_PORTRAIT,
  62. };
  63. typedef struct {
  64. lxw_workbook *workbook;
  65. lxw_worksheet *worksheet;
  66. } xls_resource_write_t;
  67. typedef struct {
  68. lxw_format *format;
  69. } xls_resource_format_t;
  70. typedef struct {
  71. lxw_chart *chart;
  72. lxw_chart_series *series;
  73. } xls_resource_chart_t;
  74. typedef struct _vtiful_xls_object {
  75. xls_resource_read_t read_ptr;
  76. xls_resource_write_t write_ptr;
  77. zend_long write_line;
  78. xls_resource_format_t format_ptr;
  79. zend_object zo;
  80. } xls_object;
  81. typedef struct _vtiful_format_object {
  82. xls_resource_format_t ptr;
  83. zend_object zo;
  84. } format_object;
  85. typedef struct _vtiful_chart_object {
  86. xls_resource_chart_t ptr;
  87. zend_object zo;
  88. } chart_object;
  89. static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
  90. return (xls_object *)((char *)(obj) - XtOffsetOf(xls_object, zo));
  91. }
  92. static inline format_object *php_vtiful_format_fetch_object(zend_object *obj) {
  93. return (format_object *)((char *)(obj) - XtOffsetOf(format_object, zo));
  94. }
  95. static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
  96. return (chart_object *)((char *)(obj) - XtOffsetOf(chart_object, zo));
  97. }
  98. #define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
  99. zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
  100. #define REGISTER_CLASS_PROPERTY_NULL(class_name, property_name, acc) \
  101. zend_declare_property_null(class_name, ZEND_STRL(property_name), acc);
  102. #define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
  103. #define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
  104. #define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
  105. #define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
  106. do { \
  107. if(xls_object_t->write_ptr.workbook == NULL) { \
  108. zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130); \
  109. return; \
  110. } \
  111. } while(0);
  112. #define WORKSHEET_NOT_INITIALIZED(xls_object_t) \
  113. do { \
  114. if (xls_object_t->write_ptr.worksheet == NULL) { \
  115. zend_throw_exception(vtiful_exception_ce, "worksheet not initialized", 200); \
  116. return; \
  117. } \
  118. } while(0);
  119. #define WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(xls_resource_write_t, error) \
  120. do { \
  121. if(xls_resource_write_t->worksheet->optimize && error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
  122. zend_throw_exception(vtiful_exception_ce, "In const memory mode, you cannot modify the placed cells", 170); \
  123. return; \
  124. } \
  125. } while(0);
  126. #define WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error) \
  127. do { \
  128. if(error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
  129. zend_throw_exception(vtiful_exception_ce, "Worksheet row or column index out of range", 180); \
  130. return; \
  131. } \
  132. } while(0);
  133. #define WORKSHEET_WRITER_EXCEPTION(error) \
  134. do { \
  135. if(error > LXW_NO_ERROR) { \
  136. zend_throw_exception(vtiful_exception_ce, "Worksheet write exception", error); \
  137. return; \
  138. } \
  139. } while(0)
  140. #define FCALL_TWO_ARGS(bucket) \
  141. ZVAL_COPY_VALUE(&args[0], &bucket->val); \
  142. if (bucket->key) { \
  143. ZVAL_STR(&args[1], bucket->key); \
  144. } else { \
  145. ZVAL_LONG(&args[1], bucket->h); \
  146. } \
  147. zend_call_function(&fci, &fci_cache);
  148. #define ROW(range) \
  149. lxw_name_to_row(range)
  150. #define ROWS(range) \
  151. lxw_name_to_row(range), lxw_name_to_row_2(range)
  152. #define SHEET_LINE_INIT(obj_p) \
  153. obj_p->write_line = 0;
  154. #define SHEET_LINE_ADD(obj_p) \
  155. ++obj_p->write_line;
  156. #define SHEET_LINE_SET(obj_p, current_line) \
  157. obj_p->write_line = current_line;
  158. #define SHEET_CURRENT_LINE(obj_p) obj_p->write_line
  159. #ifdef LXW_HAS_SNPRINTF
  160. #define lxw_snprintf snprintf
  161. #else
  162. #define lxw_snprintf __builtin_snprintf
  163. #endif
  164. #if PHP_VERSION_ID < 80000
  165. #define PROP_OBJ(zv) (zv)
  166. #else
  167. #define PROP_OBJ(zv) Z_OBJ_P(zv)
  168. #endif
  169. lxw_format * zval_get_format(zval *handle);
  170. xls_resource_write_t * zval_get_resource(zval *handle);
  171. xls_resource_chart_t * zval_get_chart(zval *resource);
  172. STATIC lxw_error _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden);
  173. STATIC int _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b);
  174. STATIC void _prepare_drawings(lxw_workbook *self);
  175. STATIC void _add_chart_cache_data(lxw_workbook *self);
  176. STATIC void _prepare_vml(lxw_workbook *self);
  177. STATIC void _prepare_defined_names(lxw_workbook *self);
  178. STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
  179. STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);
  180. void comment_show(xls_resource_write_t *res);
  181. void hide_worksheet(xls_resource_write_t *res);
  182. void first_worksheet(xls_resource_write_t *res);
  183. void zoom(xls_resource_write_t *res, zend_long zoom);
  184. void gridlines(xls_resource_write_t *res, zend_long option);
  185. void auto_filter(zend_string *range, xls_resource_write_t *res);
  186. void protection(xls_resource_write_t *res, zend_string *password);
  187. void format_copy(lxw_format *new_format, lxw_format *other_format);
  188. void printed_direction(xls_resource_write_t *res, unsigned int direction);
  189. void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
  190. void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
  191. void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
  192. void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);
  193. void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format);
  194. void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res);
  195. void call_object_method(zval *object, const char *function_name, uint32_t param_count, zval *params, zval *ret_val);
  196. void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res);
  197. void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format);
  198. void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res);
  199. void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format);
  200. void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle);
  201. void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle);
  202. 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);
  203. lxw_error workbook_file(xls_resource_write_t *self);
  204. zend_string* str_pick_up(zend_string *left, const char *right, size_t len);
  205. #endif