xlswriter.h 9.8 KB

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