xlswriter.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. typedef struct {
  60. lxw_workbook *workbook;
  61. lxw_worksheet *worksheet;
  62. } xls_resource_write_t;
  63. typedef struct {
  64. lxw_format *format;
  65. } xls_resource_format_t;
  66. typedef struct {
  67. lxw_chart *chart;
  68. lxw_chart_series *series;
  69. } xls_resource_chart_t;
  70. typedef struct _vtiful_xls_object {
  71. xls_resource_read_t read_ptr;
  72. xls_resource_write_t write_ptr;
  73. zend_long write_line;
  74. xls_resource_format_t format_ptr;
  75. zend_object zo;
  76. } xls_object;
  77. typedef struct _vtiful_format_object {
  78. xls_resource_format_t ptr;
  79. zend_object zo;
  80. } format_object;
  81. typedef struct _vtiful_chart_object {
  82. xls_resource_chart_t ptr;
  83. zend_object zo;
  84. } chart_object;
  85. static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
  86. return (xls_object *)((char *)(obj) - XtOffsetOf(xls_object, zo));
  87. }
  88. static inline format_object *php_vtiful_format_fetch_object(zend_object *obj) {
  89. return (format_object *)((char *)(obj) - XtOffsetOf(format_object, zo));
  90. }
  91. static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
  92. return (chart_object *)((char *)(obj) - XtOffsetOf(chart_object, zo));
  93. }
  94. #define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
  95. zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
  96. #define REGISTER_CLASS_PROPERTY_NULL(class_name, property_name, acc) \
  97. zend_declare_property_null(class_name, ZEND_STRL(property_name), acc);
  98. #define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
  99. #define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
  100. #define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
  101. #define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
  102. do { \
  103. if(obj->write_ptr.workbook == NULL) { \
  104. zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130); \
  105. return; \
  106. } \
  107. } while(0);
  108. #define WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(xls_resource_write_t, error) \
  109. do { \
  110. if(xls_resource_write_t->worksheet->optimize && error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
  111. zend_throw_exception(vtiful_exception_ce, "In const memory mode, you cannot modify the placed cells", 170); \
  112. return; \
  113. } \
  114. } while(0);
  115. #define WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error) \
  116. do { \
  117. if(error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
  118. zend_throw_exception(vtiful_exception_ce, "Worksheet row or column index out of range", 180); \
  119. return; \
  120. } \
  121. } while(0);
  122. #define WORKSHEET_WRITER_EXCEPTION(error) \
  123. do { \
  124. if(error > LXW_NO_ERROR) { \
  125. zend_throw_exception(vtiful_exception_ce, "Worksheet write exception", error); \
  126. return; \
  127. } \
  128. } while(0)
  129. #define FCALL_TWO_ARGS(bucket) \
  130. ZVAL_COPY_VALUE(&args[0], &bucket->val); \
  131. if (bucket->key) { \
  132. ZVAL_STR(&args[1], bucket->key); \
  133. } else { \
  134. ZVAL_LONG(&args[1], bucket->h); \
  135. } \
  136. zend_call_function(&fci, &fci_cache);
  137. #define ROW(range) \
  138. lxw_name_to_row(range)
  139. #define ROWS(range) \
  140. lxw_name_to_row(range), lxw_name_to_row_2(range)
  141. #define SHEET_LINE_INIT(obj_p) \
  142. obj_p->write_line = 0;
  143. #define SHEET_LINE_ADD(obj_p) \
  144. ++obj_p->write_line;
  145. #define SHEET_LINE_SET(obj_p, current_line) \
  146. obj_p->write_line = current_line;
  147. #define SHEET_CURRENT_LINE(obj_p) obj_p->write_line
  148. #ifdef LXW_HAS_SNPRINTF
  149. #define lxw_snprintf snprintf
  150. #else
  151. #define lxw_snprintf __builtin_snprintf
  152. #endif
  153. #if PHP_VERSION_ID < 80000
  154. #define PROP_OBJ(zv) (zv)
  155. #else
  156. #define PROP_OBJ(zv) Z_OBJ_P(zv)
  157. #endif
  158. lxw_format * zval_get_format(zval *handle);
  159. xls_resource_write_t * zval_get_resource(zval *handle);
  160. xls_resource_chart_t * zval_get_chart(zval *resource);
  161. 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);
  162. STATIC int _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b);
  163. STATIC void _prepare_drawings(lxw_workbook *self);
  164. STATIC void _add_chart_cache_data(lxw_workbook *self);
  165. STATIC void _prepare_vml(lxw_workbook *self);
  166. STATIC void _prepare_defined_names(lxw_workbook *self);
  167. STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
  168. STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);
  169. void comment_show(xls_resource_write_t *res);
  170. void zoom(xls_resource_write_t *res, zend_long zoom);
  171. void gridlines(xls_resource_write_t *res, zend_long option);
  172. void auto_filter(zend_string *range, xls_resource_write_t *res);
  173. void protection(xls_resource_write_t *res, zend_string *password);
  174. void format_copy(lxw_format *new_format, lxw_format *other_format);
  175. void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
  176. void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
  177. void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
  178. void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);
  179. void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format);
  180. void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res);
  181. void call_object_method(zval *object, const char *function_name, uint32_t param_count, zval *params, zval *ret_val);
  182. void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res);
  183. void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format);
  184. void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res);
  185. void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format);
  186. void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle);
  187. void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle);
  188. 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);
  189. lxw_error workbook_file(xls_resource_write_t *self);
  190. zend_string* str_pick_up(zend_string *left, const char *right, size_t len);
  191. #endif