excel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. #include "include.h"
  16. zend_class_entry *vtiful_xls_ce;
  17. static zend_object_handlers vtiful_xls_handlers;
  18. static zend_always_inline void *vtiful_object_alloc(size_t obj_size, zend_class_entry *ce) {
  19. void *obj = emalloc(obj_size + zend_object_properties_size(ce));
  20. memset(obj, 0, obj_size);
  21. return obj;
  22. }
  23. /* {{{ xls_objects_new
  24. */
  25. PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
  26. {
  27. xls_object *intern = vtiful_object_alloc(sizeof(xls_object), ce);
  28. zend_object_std_init(&intern->zo, ce);
  29. object_properties_init(&intern->zo, ce);
  30. intern->zo.handlers = &vtiful_xls_handlers;
  31. return &intern->zo;
  32. }
  33. /* }}} */
  34. /* {{{ vtiful_xls_objects_free
  35. */
  36. static void vtiful_xls_objects_free(zend_object *object)
  37. {
  38. xls_object *intern = php_vtiful_xls_fetch_object(object);
  39. lxw_workbook_free(intern->ptr.workbook);
  40. zend_object_std_dtor(&intern->zo);
  41. }
  42. /* }}} */
  43. /* {{{ ARG_INFO
  44. */
  45. ZEND_BEGIN_ARG_INFO_EX(xls_construct_arginfo, 0, 0, 1)
  46. ZEND_ARG_INFO(0, config)
  47. ZEND_END_ARG_INFO()
  48. ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 1)
  49. ZEND_ARG_INFO(0, file_name)
  50. ZEND_END_ARG_INFO()
  51. ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
  52. ZEND_ARG_INFO(0, sheet_name)
  53. ZEND_END_ARG_INFO()
  54. ZEND_BEGIN_ARG_INFO_EX(xls_header_arginfo, 0, 0, 1)
  55. ZEND_ARG_INFO(0, header)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO_EX(xls_data_arginfo, 0, 0, 1)
  58. ZEND_ARG_INFO(0, data)
  59. ZEND_END_ARG_INFO()
  60. ZEND_BEGIN_ARG_INFO_EX(xls_insert_text_arginfo, 0, 0, 4)
  61. ZEND_ARG_INFO(0, row)
  62. ZEND_ARG_INFO(0, column)
  63. ZEND_ARG_INFO(0, data)
  64. ZEND_ARG_INFO(0, format)
  65. ZEND_END_ARG_INFO()
  66. ZEND_BEGIN_ARG_INFO_EX(xls_insert_image_arginfo, 0, 0, 3)
  67. ZEND_ARG_INFO(0, row)
  68. ZEND_ARG_INFO(0, column)
  69. ZEND_ARG_INFO(0, image)
  70. ZEND_END_ARG_INFO()
  71. ZEND_BEGIN_ARG_INFO_EX(xls_insert_formula_arginfo, 0, 0, 3)
  72. ZEND_ARG_INFO(0, row)
  73. ZEND_ARG_INFO(0, column)
  74. ZEND_ARG_INFO(0, formula)
  75. ZEND_END_ARG_INFO()
  76. ZEND_BEGIN_ARG_INFO_EX(xls_auto_filter_arginfo, 0, 0, 1)
  77. ZEND_ARG_INFO(0, range)
  78. ZEND_END_ARG_INFO()
  79. ZEND_BEGIN_ARG_INFO_EX(xls_merge_cells_arginfo, 0, 0, 2)
  80. ZEND_ARG_INFO(0, range)
  81. ZEND_ARG_INFO(0, data)
  82. ZEND_END_ARG_INFO()
  83. ZEND_BEGIN_ARG_INFO_EX(xls_set_column_arginfo, 0, 0, 3)
  84. ZEND_ARG_INFO(0, format_handle)
  85. ZEND_ARG_INFO(0, range)
  86. ZEND_ARG_INFO(0, width)
  87. ZEND_END_ARG_INFO()
  88. ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 3)
  89. ZEND_ARG_INFO(0, format_handle)
  90. ZEND_ARG_INFO(0, range)
  91. ZEND_ARG_INFO(0, height)
  92. ZEND_END_ARG_INFO()
  93. /* }}} */
  94. /** {{{ \Vtiful\Kernel\xls::__construct(array $config)
  95. */
  96. PHP_METHOD(vtiful_xls, __construct)
  97. {
  98. zval *config, *c_path;
  99. ZEND_PARSE_PARAMETERS_START(1, 1)
  100. Z_PARAM_ARRAY(config)
  101. ZEND_PARSE_PARAMETERS_END();
  102. if((c_path = zend_hash_str_find(Z_ARRVAL_P(config), ZEND_STRL(V_XLS_PAT))) == NULL)
  103. {
  104. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  105. return;
  106. }
  107. if(Z_TYPE_P(c_path) != IS_STRING)
  108. {
  109. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  110. return;
  111. }
  112. add_property_zval(getThis(), V_XLS_COF, config);
  113. }
  114. /* }}} */
  115. /** {{{ \Vtiful\Kernel\xls::filename(string $fileName)
  116. */
  117. PHP_METHOD(vtiful_xls, fileName)
  118. {
  119. zval file_path, *dir_path;
  120. zend_string *file_name;
  121. ZEND_PARSE_PARAMETERS_START(1, 1)
  122. Z_PARAM_STR(file_name)
  123. ZEND_PARSE_PARAMETERS_END();
  124. ZVAL_COPY(return_value, getThis());
  125. GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
  126. xls_object *obj = Z_XLS_P(getThis());
  127. if(obj->ptr.workbook == NULL) {
  128. xls_file_path(file_name, dir_path, &file_path);
  129. obj->ptr.workbook = workbook_new(Z_STRVAL(file_path));
  130. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  131. add_property_zval(return_value, V_XLS_FIL, &file_path);
  132. zval_ptr_dtor(&file_path);
  133. }
  134. }
  135. /* }}} */
  136. /** {{{ \Vtiful\Kernel\xls::addSheet(string $sheetName)
  137. */
  138. PHP_METHOD(vtiful_xls, addSheet)
  139. {
  140. zend_string *zs_sheet_name = NULL;
  141. char *sheet_name = NULL;
  142. ZEND_PARSE_PARAMETERS_START(0, 1)
  143. Z_PARAM_OPTIONAL
  144. Z_PARAM_STR(zs_sheet_name)
  145. ZEND_PARSE_PARAMETERS_END();
  146. ZVAL_COPY(return_value, getThis());
  147. xls_object *obj = Z_XLS_P(getThis());
  148. if(obj->ptr.workbook == NULL) {
  149. zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
  150. return;
  151. }
  152. if(zs_sheet_name != NULL) {
  153. sheet_name = ZSTR_VAL(zs_sheet_name);
  154. }
  155. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, sheet_name);
  156. }
  157. /* }}} */
  158. /** {{{ \Vtiful\Kernel\xls::constMemory(string $fileName)
  159. */
  160. PHP_METHOD(vtiful_xls, constMemory)
  161. {
  162. zval file_path, *dir_path;
  163. zend_string *file_name;
  164. ZEND_PARSE_PARAMETERS_START(1, 1)
  165. Z_PARAM_STR(file_name)
  166. ZEND_PARSE_PARAMETERS_END();
  167. ZVAL_COPY(return_value, getThis());
  168. GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
  169. xls_object *obj = Z_XLS_P(getThis());
  170. if(obj->ptr.workbook == NULL) {
  171. xls_file_path(file_name, dir_path, &file_path);
  172. lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
  173. obj->ptr.workbook = workbook_new_opt(Z_STRVAL(file_path), &options);
  174. obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
  175. add_property_zval(return_value, V_XLS_FIL, &file_path);
  176. zval_ptr_dtor(&file_path);
  177. }
  178. }
  179. /* }}} */
  180. /** {{{ \Vtiful\Kernel\xls::header(array $header)
  181. */
  182. PHP_METHOD(vtiful_xls, header)
  183. {
  184. zval *header, *header_value;
  185. zend_long header_l_key;
  186. ZEND_PARSE_PARAMETERS_START(1, 1)
  187. Z_PARAM_ARRAY(header)
  188. ZEND_PARSE_PARAMETERS_END();
  189. ZVAL_COPY(return_value, getThis());
  190. xls_object *obj = Z_XLS_P(getThis());
  191. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
  192. type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
  193. zval_ptr_dtor(header_value);
  194. } ZEND_HASH_FOREACH_END();
  195. }
  196. /* }}} */
  197. /** {{{ \Vtiful\Kernel\xls::data(array $data)
  198. */
  199. PHP_METHOD(vtiful_xls, data)
  200. {
  201. zval *data, *data_r_value, *data_l_value;
  202. zend_long data_r_key, data_l_key;
  203. ZEND_PARSE_PARAMETERS_START(1, 1)
  204. Z_PARAM_ARRAY(data)
  205. ZEND_PARSE_PARAMETERS_END();
  206. ZVAL_COPY(return_value, getThis());
  207. xls_object *obj = Z_XLS_P(getThis());
  208. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
  209. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  210. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
  211. type_writer(data_l_value, data_r_key+1, data_l_key, &obj->ptr, NULL);
  212. zval_ptr_dtor(data_l_value);
  213. } ZEND_HASH_FOREACH_END();
  214. }
  215. } ZEND_HASH_FOREACH_END();
  216. }
  217. /* }}} */
  218. /** {{{ \Vtiful\Kernel\xls::output()
  219. */
  220. PHP_METHOD(vtiful_xls, output)
  221. {
  222. zval rv, *file_path;
  223. file_path = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_FIL), 0, &rv TSRMLS_DC);
  224. xls_object *obj = Z_XLS_P(getThis());
  225. workbook_file(&obj->ptr);
  226. add_property_null(getThis(), V_XLS_HANDLE);
  227. add_property_null(getThis(), V_XLS_PAT);
  228. ZVAL_COPY(return_value, file_path);
  229. }
  230. /* }}} */
  231. /** {{{ \Vtiful\Kernel\xls::getHandle()
  232. */
  233. PHP_METHOD(vtiful_xls, getHandle)
  234. {
  235. xls_object *obj = Z_XLS_P(getThis());
  236. RETURN_RES(zend_register_resource(&obj->ptr, le_xls_writer));
  237. }
  238. /* }}} */
  239. /** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data)
  240. */
  241. PHP_METHOD(vtiful_xls, insertText)
  242. {
  243. zval *data;
  244. zend_long row, column;
  245. zend_string *format = NULL;
  246. ZEND_PARSE_PARAMETERS_START(3, 4)
  247. Z_PARAM_LONG(row)
  248. Z_PARAM_LONG(column)
  249. Z_PARAM_ZVAL(data)
  250. Z_PARAM_OPTIONAL
  251. Z_PARAM_STR(format)
  252. ZEND_PARSE_PARAMETERS_END();
  253. ZVAL_COPY(return_value, getThis());
  254. xls_object *obj = Z_XLS_P(getThis());
  255. type_writer(data, row, column, &obj->ptr, format);
  256. }
  257. /* }}} */
  258. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  259. */
  260. PHP_METHOD(vtiful_xls, insertImage)
  261. {
  262. zval *image;
  263. zend_long row, column;
  264. ZEND_PARSE_PARAMETERS_START(3, 3)
  265. Z_PARAM_LONG(row)
  266. Z_PARAM_LONG(column)
  267. Z_PARAM_ZVAL(image)
  268. ZEND_PARSE_PARAMETERS_END();
  269. ZVAL_COPY(return_value, getThis());
  270. xls_object *obj = Z_XLS_P(getThis());
  271. image_writer(image, row, column, &obj->ptr);
  272. }
  273. /* }}} */
  274. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  275. */
  276. PHP_METHOD(vtiful_xls, insertFormula)
  277. {
  278. zval *formula;
  279. zend_long row, column;
  280. ZEND_PARSE_PARAMETERS_START(3, 3)
  281. Z_PARAM_LONG(row)
  282. Z_PARAM_LONG(column)
  283. Z_PARAM_ZVAL(formula)
  284. ZEND_PARSE_PARAMETERS_END();
  285. ZVAL_COPY(return_value, getThis());
  286. xls_object *obj = Z_XLS_P(getThis());
  287. formula_writer(formula, row, column, &obj->ptr);
  288. }
  289. /* }}} */
  290. /** {{{ \Vtiful\Kernel\xls::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  291. */
  292. PHP_METHOD(vtiful_xls, autoFilter)
  293. {
  294. zend_string *range;
  295. ZEND_PARSE_PARAMETERS_START(1, 1)
  296. Z_PARAM_STR(range)
  297. ZEND_PARSE_PARAMETERS_END();
  298. ZVAL_COPY(return_value, getThis());
  299. xls_object *obj = Z_XLS_P(getThis());
  300. auto_filter(range, &obj->ptr);
  301. }
  302. /* }}} */
  303. /** {{{ \Vtiful\Kernel\xls::mergeCells(string $range, string $data)
  304. */
  305. PHP_METHOD(vtiful_xls, mergeCells)
  306. {
  307. zend_string *range, *data;
  308. ZEND_PARSE_PARAMETERS_START(2, 2)
  309. Z_PARAM_STR(range)
  310. Z_PARAM_STR(data)
  311. ZEND_PARSE_PARAMETERS_END();
  312. ZVAL_COPY(return_value, getThis());
  313. xls_object *obj = Z_XLS_P(getThis());
  314. merge_cells(range, data, &obj->ptr);
  315. }
  316. /* }}} */
  317. /** {{{ \Vtiful\Kernel\xls::setColumn(resource $format, string $range [, int $width])
  318. */
  319. PHP_METHOD(vtiful_xls, setColumn)
  320. {
  321. zval *format_handle;
  322. zend_string *range;
  323. double width = 0;
  324. int argc = ZEND_NUM_ARGS();
  325. ZEND_PARSE_PARAMETERS_START(2, 3)
  326. Z_PARAM_STR(range)
  327. Z_PARAM_DOUBLE(width)
  328. Z_PARAM_OPTIONAL
  329. Z_PARAM_RESOURCE(format_handle)
  330. ZEND_PARSE_PARAMETERS_END();
  331. ZVAL_COPY(return_value, getThis());
  332. xls_object *obj = Z_XLS_P(getThis());
  333. if (argc == 3) {
  334. set_column(range, width, &obj->ptr, zval_get_format(format_handle));
  335. }
  336. if (argc == 2) {
  337. set_column(range, width, &obj->ptr, NULL);
  338. }
  339. }
  340. /* }}} */
  341. /** {{{ \Vtiful\Kernel\xls::setRow(resource $format, string $range [, int $heitght])
  342. */
  343. PHP_METHOD(vtiful_xls, setRow)
  344. {
  345. zval *format_handle;
  346. zend_string *range;
  347. double height = 0;
  348. int argc = ZEND_NUM_ARGS();
  349. ZEND_PARSE_PARAMETERS_START(2, 3)
  350. Z_PARAM_STR(range)
  351. Z_PARAM_DOUBLE(height)
  352. Z_PARAM_OPTIONAL
  353. Z_PARAM_RESOURCE(format_handle)
  354. ZEND_PARSE_PARAMETERS_END();
  355. ZVAL_COPY(return_value, getThis());
  356. xls_object *obj = Z_XLS_P(getThis());
  357. if (argc == 3) {
  358. set_row(range, height, &obj->ptr, zval_get_format(format_handle));
  359. }
  360. if (argc == 2) {
  361. set_row(range, height, &obj->ptr, NULL);
  362. }
  363. }
  364. /* }}} */
  365. /** {{{ xls_methods
  366. */
  367. zend_function_entry xls_methods[] = {
  368. PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  369. PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
  370. PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
  371. PHP_ME(vtiful_xls, constMemory, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
  372. PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
  373. PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
  374. PHP_ME(vtiful_xls, output, NULL, ZEND_ACC_PUBLIC)
  375. PHP_ME(vtiful_xls, getHandle, NULL, ZEND_ACC_PUBLIC)
  376. PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  377. PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
  378. PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
  379. PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  380. PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  381. PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
  382. PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
  383. PHP_FE_END
  384. };
  385. /* }}} */
  386. /** {{{ VTIFUL_STARTUP_FUNCTION
  387. */
  388. VTIFUL_STARTUP_FUNCTION(excel) {
  389. zend_class_entry ce;
  390. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", xls_methods);
  391. ce.create_object = vtiful_xls_objects_new;
  392. vtiful_xls_ce = zend_register_internal_class(&ce);
  393. memcpy(&vtiful_xls_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  394. vtiful_xls_handlers.offset = XtOffsetOf(xls_object, zo);
  395. vtiful_xls_handlers.free_obj = vtiful_xls_objects_free;
  396. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
  397. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
  398. return SUCCESS;
  399. }
  400. /* }}} */