excel.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. zend_class_entry *vtiful_xls_ce;
  14. static zend_object_handlers vtiful_xls_handlers;
  15. static zend_always_inline void *vtiful_object_alloc(size_t obj_size, zend_class_entry *ce) {
  16. void *obj = emalloc(obj_size + zend_object_properties_size(ce));
  17. memset(obj, 0, obj_size);
  18. return obj;
  19. }
  20. /* {{{ xls_objects_new
  21. */
  22. PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
  23. {
  24. xls_object *intern = vtiful_object_alloc(sizeof(xls_object), ce);
  25. SHEET_LINE_INIT(intern)
  26. zend_object_std_init(&intern->zo, ce);
  27. object_properties_init(&intern->zo, ce);
  28. intern->zo.handlers = &vtiful_xls_handlers;
  29. intern->read_ptr.file_t = NULL;
  30. intern->read_ptr.sheet_t = NULL;
  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->write_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_ARG_INFO(0, sheet_name)
  51. ZEND_END_ARG_INFO()
  52. ZEND_BEGIN_ARG_INFO_EX(xls_const_memory_arginfo, 0, 0, 1)
  53. ZEND_ARG_INFO(0, file_name)
  54. ZEND_ARG_INFO(0, sheet_name)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
  57. ZEND_ARG_INFO(0, sheet_name)
  58. ZEND_END_ARG_INFO()
  59. ZEND_BEGIN_ARG_INFO_EX(xls_file_checkout_sheet, 0, 0, 1)
  60. ZEND_ARG_INFO(0, sheet_name)
  61. ZEND_END_ARG_INFO()
  62. ZEND_BEGIN_ARG_INFO_EX(xls_header_arginfo, 0, 0, 1)
  63. ZEND_ARG_INFO(0, header)
  64. ZEND_END_ARG_INFO()
  65. ZEND_BEGIN_ARG_INFO_EX(xls_data_arginfo, 0, 0, 1)
  66. ZEND_ARG_INFO(0, data)
  67. ZEND_END_ARG_INFO()
  68. ZEND_BEGIN_ARG_INFO_EX(xls_insert_text_arginfo, 0, 0, 5)
  69. ZEND_ARG_INFO(0, row)
  70. ZEND_ARG_INFO(0, column)
  71. ZEND_ARG_INFO(0, data)
  72. ZEND_ARG_INFO(0, format)
  73. ZEND_ARG_INFO(0, format_handle)
  74. ZEND_END_ARG_INFO()
  75. ZEND_BEGIN_ARG_INFO_EX(xls_insert_url_arginfo, 0, 0, 4)
  76. ZEND_ARG_INFO(0, row)
  77. ZEND_ARG_INFO(0, column)
  78. ZEND_ARG_INFO(0, url)
  79. ZEND_ARG_INFO(0, format)
  80. ZEND_END_ARG_INFO()
  81. ZEND_BEGIN_ARG_INFO_EX(xls_insert_chart_arginfo, 0, 0, 3)
  82. ZEND_ARG_INFO(0, row)
  83. ZEND_ARG_INFO(0, column)
  84. ZEND_ARG_INFO(0, chart_resource)
  85. ZEND_END_ARG_INFO()
  86. ZEND_BEGIN_ARG_INFO_EX(xls_insert_image_arginfo, 0, 0, 3)
  87. ZEND_ARG_INFO(0, row)
  88. ZEND_ARG_INFO(0, column)
  89. ZEND_ARG_INFO(0, image)
  90. ZEND_ARG_INFO(0, width)
  91. ZEND_ARG_INFO(0, height)
  92. ZEND_END_ARG_INFO()
  93. ZEND_BEGIN_ARG_INFO_EX(xls_insert_formula_arginfo, 0, 0, 3)
  94. ZEND_ARG_INFO(0, row)
  95. ZEND_ARG_INFO(0, column)
  96. ZEND_ARG_INFO(0, formula)
  97. ZEND_END_ARG_INFO()
  98. ZEND_BEGIN_ARG_INFO_EX(xls_auto_filter_arginfo, 0, 0, 1)
  99. ZEND_ARG_INFO(0, range)
  100. ZEND_END_ARG_INFO()
  101. ZEND_BEGIN_ARG_INFO_EX(xls_merge_cells_arginfo, 0, 0, 2)
  102. ZEND_ARG_INFO(0, range)
  103. ZEND_ARG_INFO(0, data)
  104. ZEND_END_ARG_INFO()
  105. ZEND_BEGIN_ARG_INFO_EX(xls_set_column_arginfo, 0, 0, 3)
  106. ZEND_ARG_INFO(0, format_handle)
  107. ZEND_ARG_INFO(0, range)
  108. ZEND_ARG_INFO(0, width)
  109. ZEND_END_ARG_INFO()
  110. ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 3)
  111. ZEND_ARG_INFO(0, format_handle)
  112. ZEND_ARG_INFO(0, range)
  113. ZEND_ARG_INFO(0, height)
  114. ZEND_END_ARG_INFO()
  115. /* }}} */
  116. /** {{{ \Vtiful\Kernel\xls::__construct(array $config)
  117. */
  118. PHP_METHOD(vtiful_xls, __construct)
  119. {
  120. zval *config, *c_path;
  121. ZEND_PARSE_PARAMETERS_START(1, 1)
  122. Z_PARAM_ARRAY(config)
  123. ZEND_PARSE_PARAMETERS_END();
  124. if((c_path = zend_hash_str_find(Z_ARRVAL_P(config), ZEND_STRL(V_XLS_PAT))) == NULL)
  125. {
  126. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  127. return;
  128. }
  129. if(Z_TYPE_P(c_path) != IS_STRING)
  130. {
  131. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  132. return;
  133. }
  134. add_property_zval(getThis(), V_XLS_COF, config);
  135. }
  136. /* }}} */
  137. /** {{{ \Vtiful\Kernel\xls::filename(string $fileName [, string $sheetName])
  138. */
  139. PHP_METHOD(vtiful_xls, fileName)
  140. {
  141. zval file_path, *dir_path = NULL;
  142. zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
  143. char *sheet_name = NULL;
  144. ZEND_PARSE_PARAMETERS_START(1, 2)
  145. Z_PARAM_STR(zs_file_name)
  146. Z_PARAM_OPTIONAL
  147. Z_PARAM_STR(zs_sheet_name)
  148. ZEND_PARSE_PARAMETERS_END();
  149. ZVAL_COPY(return_value, getThis());
  150. GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
  151. xls_object *obj = Z_XLS_P(getThis());
  152. if(obj->write_ptr.workbook == NULL) {
  153. xls_file_path(zs_file_name, dir_path, &file_path);
  154. if(zs_sheet_name != NULL) {
  155. sheet_name = ZSTR_VAL(zs_sheet_name);
  156. }
  157. obj->write_ptr.workbook = workbook_new(Z_STRVAL(file_path));
  158. obj->write_ptr.worksheet = workbook_add_worksheet(obj->write_ptr.workbook, sheet_name);
  159. add_property_zval(return_value, V_XLS_FIL, &file_path);
  160. zval_ptr_dtor(&file_path);
  161. }
  162. }
  163. /* }}} */
  164. /** {{{ \Vtiful\Kernel\xls::addSheet(string $sheetName)
  165. */
  166. PHP_METHOD(vtiful_xls, addSheet)
  167. {
  168. zend_string *zs_sheet_name = NULL;
  169. char *sheet_name = NULL;
  170. ZEND_PARSE_PARAMETERS_START(0, 1)
  171. Z_PARAM_OPTIONAL
  172. Z_PARAM_STR(zs_sheet_name)
  173. ZEND_PARSE_PARAMETERS_END();
  174. ZVAL_COPY(return_value, getThis());
  175. xls_object *obj = Z_XLS_P(getThis());
  176. SHEET_LINE_INIT(obj)
  177. if(obj->write_ptr.workbook == NULL) {
  178. zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
  179. return;
  180. }
  181. if(zs_sheet_name != NULL) {
  182. sheet_name = ZSTR_VAL(zs_sheet_name);
  183. }
  184. obj->write_ptr.worksheet = workbook_add_worksheet(obj->write_ptr.workbook, sheet_name);
  185. }
  186. /* }}} */
  187. /** {{{ \Vtiful\Kernel\xls::checkoutSheet(string $sheetName)
  188. */
  189. PHP_METHOD(vtiful_xls, checkoutSheet)
  190. {
  191. int line = 0;
  192. lxw_worksheet *sheet_t = NULL;
  193. zend_string *zs_sheet_name = NULL;
  194. ZEND_PARSE_PARAMETERS_START(1, 1)
  195. Z_PARAM_STR(zs_sheet_name)
  196. ZEND_PARSE_PARAMETERS_END();
  197. ZVAL_COPY(return_value, getThis());
  198. xls_object *obj = Z_XLS_P(getThis());
  199. if(obj->write_ptr.workbook == NULL) {
  200. zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
  201. return;
  202. }
  203. if ((sheet_t = workbook_get_worksheet_by_name(obj->write_ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
  204. zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
  205. return;
  206. }
  207. line = sheet_t->table->cached_row_num;
  208. SHEET_LINE_SET(obj, line);
  209. obj->write_ptr.worksheet = sheet_t;
  210. }
  211. /* }}} */
  212. /** {{{ \Vtiful\Kernel\xls::constMemory(string $fileName [, string $sheetName])
  213. */
  214. PHP_METHOD(vtiful_xls, constMemory)
  215. {
  216. zval file_path, *dir_path = NULL;
  217. zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
  218. char *sheet_name = NULL;
  219. ZEND_PARSE_PARAMETERS_START(1, 2)
  220. Z_PARAM_STR(zs_file_name)
  221. Z_PARAM_OPTIONAL
  222. Z_PARAM_STR(zs_sheet_name)
  223. ZEND_PARSE_PARAMETERS_END();
  224. ZVAL_COPY(return_value, getThis());
  225. GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
  226. xls_object *obj = Z_XLS_P(getThis());
  227. if(obj->write_ptr.workbook == NULL) {
  228. xls_file_path(zs_file_name, dir_path, &file_path);
  229. lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
  230. if(zs_sheet_name != NULL) {
  231. sheet_name = ZSTR_VAL(zs_sheet_name);
  232. }
  233. obj->write_ptr.workbook = workbook_new_opt(Z_STRVAL(file_path), &options);
  234. obj->write_ptr.worksheet = workbook_add_worksheet(obj->write_ptr.workbook, sheet_name);
  235. add_property_zval(return_value, V_XLS_FIL, &file_path);
  236. zval_ptr_dtor(&file_path);
  237. }
  238. }
  239. /* }}} */
  240. /** {{{ \Vtiful\Kernel\xls::header(array $header)
  241. */
  242. PHP_METHOD(vtiful_xls, header)
  243. {
  244. zval *header, *header_value;
  245. zend_long header_l_key;
  246. ZEND_PARSE_PARAMETERS_START(1, 1)
  247. Z_PARAM_ARRAY(header)
  248. ZEND_PARSE_PARAMETERS_END();
  249. ZVAL_COPY(return_value, getThis());
  250. xls_object *obj = Z_XLS_P(getThis());
  251. ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
  252. type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, NULL);
  253. zval_ptr_dtor(header_value);
  254. ZEND_HASH_FOREACH_END();
  255. }
  256. /* }}} */
  257. /** {{{ \Vtiful\Kernel\xls::data(array $data)
  258. */
  259. PHP_METHOD(vtiful_xls, data)
  260. {
  261. zval *data = NULL, *data_r_value = NULL;
  262. ZEND_PARSE_PARAMETERS_START(1, 1)
  263. Z_PARAM_ARRAY(data)
  264. ZEND_PARSE_PARAMETERS_END();
  265. ZVAL_COPY(return_value, getThis());
  266. xls_object *obj = Z_XLS_P(getThis());
  267. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
  268. if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
  269. SHEET_LINE_ADD(obj)
  270. ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
  271. type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->write_ptr, NULL, NULL);
  272. zval_ptr_dtor(&bucket->val);
  273. ZEND_HASH_FOREACH_END();
  274. }
  275. ZEND_HASH_FOREACH_END();
  276. }
  277. /* }}} */
  278. /** {{{ \Vtiful\Kernel\xls::output()
  279. */
  280. PHP_METHOD(vtiful_xls, output)
  281. {
  282. zval rv, *file_path;
  283. file_path = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_FIL), 0, &rv TSRMLS_DC);
  284. xls_object *obj = Z_XLS_P(getThis());
  285. workbook_file(&obj->write_ptr);
  286. add_property_null(getThis(), V_XLS_HANDLE);
  287. add_property_null(getThis(), V_XLS_PAT);
  288. ZVAL_COPY(return_value, file_path);
  289. }
  290. /* }}} */
  291. /** {{{ \Vtiful\Kernel\xls::getHandle()
  292. */
  293. PHP_METHOD(vtiful_xls, getHandle)
  294. {
  295. xls_object *obj = Z_XLS_P(getThis());
  296. RETURN_RES(zend_register_resource(&obj->write_ptr, le_xls_writer));
  297. }
  298. /* }}} */
  299. /** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data[, string $format, resource $formatHandle])
  300. */
  301. PHP_METHOD(vtiful_xls, insertText)
  302. {
  303. zval *data, *format_handle = NULL;
  304. zend_long row, column;
  305. zend_string *format = NULL;
  306. ZEND_PARSE_PARAMETERS_START(3, 5)
  307. Z_PARAM_LONG(row)
  308. Z_PARAM_LONG(column)
  309. Z_PARAM_ZVAL(data)
  310. Z_PARAM_OPTIONAL
  311. Z_PARAM_STR(format)
  312. Z_PARAM_RESOURCE(format_handle)
  313. ZEND_PARSE_PARAMETERS_END();
  314. ZVAL_COPY(return_value, getThis());
  315. xls_object *obj = Z_XLS_P(getThis());
  316. SHEET_LINE_SET(obj, row);
  317. if (format_handle) {
  318. type_writer(data, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
  319. } else {
  320. type_writer(data, row, column, &obj->write_ptr, format, NULL);
  321. }
  322. }
  323. /* }}} */
  324. /** {{{ \Vtiful\Kernel\xls::insertChart(int $row, int $column, resource $chartResource)
  325. */
  326. PHP_METHOD(vtiful_xls, insertChart)
  327. {
  328. zval *chart_resource;
  329. zend_long row, column;
  330. ZEND_PARSE_PARAMETERS_START(3, 3)
  331. Z_PARAM_LONG(row)
  332. Z_PARAM_LONG(column)
  333. Z_PARAM_ZVAL(chart_resource)
  334. ZEND_PARSE_PARAMETERS_END();
  335. ZVAL_COPY(return_value, getThis());
  336. xls_object *obj = Z_XLS_P(getThis());
  337. chart_writer(row, column, zval_get_chart(chart_resource), &obj->write_ptr);
  338. }
  339. /* }}} */
  340. /** {{{ \Vtiful\Kernel\xls::insertUrl(int $row, int $column, string $url)
  341. */
  342. PHP_METHOD(vtiful_xls, insertUrl)
  343. {
  344. zend_long row, column;
  345. zend_string *url = NULL;
  346. zval *format_handle = NULL;
  347. int argc = ZEND_NUM_ARGS();
  348. ZEND_PARSE_PARAMETERS_START(3, 4)
  349. Z_PARAM_LONG(row)
  350. Z_PARAM_LONG(column)
  351. Z_PARAM_STR(url)
  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 == 4) {
  358. url_writer(row, column, &obj->write_ptr, url, zval_get_format(format_handle));
  359. }
  360. if (argc == 3) {
  361. url_writer(row, column, &obj->write_ptr, url, NULL);
  362. }
  363. }
  364. /* }}} */
  365. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  366. */
  367. PHP_METHOD(vtiful_xls, insertImage)
  368. {
  369. zval *image;
  370. zend_long row, column;
  371. double width = 1, height = 1;
  372. ZEND_PARSE_PARAMETERS_START(3, 5)
  373. Z_PARAM_LONG(row)
  374. Z_PARAM_LONG(column)
  375. Z_PARAM_ZVAL(image)
  376. Z_PARAM_OPTIONAL
  377. Z_PARAM_DOUBLE(width)
  378. Z_PARAM_DOUBLE(height)
  379. ZEND_PARSE_PARAMETERS_END();
  380. ZVAL_COPY(return_value, getThis());
  381. xls_object *obj = Z_XLS_P(getThis());
  382. image_writer(image, row, column, width, height, &obj->write_ptr);
  383. }
  384. /* }}} */
  385. /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  386. */
  387. PHP_METHOD(vtiful_xls, insertFormula)
  388. {
  389. zval *formula;
  390. zend_long row, column;
  391. ZEND_PARSE_PARAMETERS_START(3, 3)
  392. Z_PARAM_LONG(row)
  393. Z_PARAM_LONG(column)
  394. Z_PARAM_ZVAL(formula)
  395. ZEND_PARSE_PARAMETERS_END();
  396. ZVAL_COPY(return_value, getThis());
  397. xls_object *obj = Z_XLS_P(getThis());
  398. formula_writer(formula, row, column, &obj->write_ptr);
  399. }
  400. /* }}} */
  401. /** {{{ \Vtiful\Kernel\xls::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
  402. */
  403. PHP_METHOD(vtiful_xls, autoFilter)
  404. {
  405. zend_string *range;
  406. ZEND_PARSE_PARAMETERS_START(1, 1)
  407. Z_PARAM_STR(range)
  408. ZEND_PARSE_PARAMETERS_END();
  409. ZVAL_COPY(return_value, getThis());
  410. xls_object *obj = Z_XLS_P(getThis());
  411. auto_filter(range, &obj->write_ptr);
  412. }
  413. /* }}} */
  414. /** {{{ \Vtiful\Kernel\xls::mergeCells(string $range, string $data)
  415. */
  416. PHP_METHOD(vtiful_xls, mergeCells)
  417. {
  418. zend_string *range, *data;
  419. ZEND_PARSE_PARAMETERS_START(2, 2)
  420. Z_PARAM_STR(range)
  421. Z_PARAM_STR(data)
  422. ZEND_PARSE_PARAMETERS_END();
  423. ZVAL_COPY(return_value, getThis());
  424. xls_object *obj = Z_XLS_P(getThis());
  425. merge_cells(range, data, &obj->write_ptr);
  426. }
  427. /* }}} */
  428. /** {{{ \Vtiful\Kernel\xls::setColumn(resource $format, string $range [, int $width])
  429. */
  430. PHP_METHOD(vtiful_xls, setColumn)
  431. {
  432. zval *format_handle;
  433. zend_string *range;
  434. double width = 0;
  435. int argc = ZEND_NUM_ARGS();
  436. ZEND_PARSE_PARAMETERS_START(2, 3)
  437. Z_PARAM_STR(range)
  438. Z_PARAM_DOUBLE(width)
  439. Z_PARAM_OPTIONAL
  440. Z_PARAM_RESOURCE(format_handle)
  441. ZEND_PARSE_PARAMETERS_END();
  442. ZVAL_COPY(return_value, getThis());
  443. xls_object *obj = Z_XLS_P(getThis());
  444. if (argc == 3) {
  445. set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
  446. }
  447. if (argc == 2) {
  448. set_column(range, width, &obj->write_ptr, NULL);
  449. }
  450. }
  451. /* }}} */
  452. /** {{{ \Vtiful\Kernel\xls::setRow(resource $format, string $range [, int $heitght])
  453. */
  454. PHP_METHOD(vtiful_xls, setRow)
  455. {
  456. zval *format_handle;
  457. zend_string *range;
  458. double height = 0;
  459. int argc = ZEND_NUM_ARGS();
  460. ZEND_PARSE_PARAMETERS_START(2, 3)
  461. Z_PARAM_STR(range)
  462. Z_PARAM_DOUBLE(height)
  463. Z_PARAM_OPTIONAL
  464. Z_PARAM_RESOURCE(format_handle)
  465. ZEND_PARSE_PARAMETERS_END();
  466. ZVAL_COPY(return_value, getThis());
  467. xls_object *obj = Z_XLS_P(getThis());
  468. if (argc == 3) {
  469. set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
  470. }
  471. if (argc == 2) {
  472. set_row(range, height, &obj->write_ptr, NULL);
  473. }
  474. }
  475. /* }}} */
  476. /** {{{ \Vtiful\Kernel\xls::openFile()
  477. */
  478. PHP_METHOD(vtiful_xls, openFile)
  479. {
  480. zval *zv_config_path = NULL;
  481. zend_string *zs_file_name = NULL;
  482. ZEND_PARSE_PARAMETERS_START(1, 1)
  483. Z_PARAM_STR(zs_file_name)
  484. ZEND_PARSE_PARAMETERS_END();
  485. ZVAL_COPY(return_value, getThis());
  486. GET_CONFIG_PATH(zv_config_path, vtiful_xls_ce, return_value);
  487. xls_object* obj = Z_XLS_P(getThis());
  488. obj->read_ptr.file_t = file_open(Z_STRVAL_P(zv_config_path), ZSTR_VAL(zs_file_name));
  489. }
  490. /* }}} */
  491. /** {{{ \Vtiful\Kernel\xls::openSheet()
  492. */
  493. PHP_METHOD(vtiful_xls, openSheet)
  494. {
  495. zend_string *sheet_name = NULL;
  496. ZEND_PARSE_PARAMETERS_START(0, 1)
  497. Z_PARAM_OPTIONAL
  498. Z_PARAM_STR(sheet_name)
  499. ZEND_PARSE_PARAMETERS_END();
  500. ZVAL_COPY(return_value, getThis());
  501. xls_object* obj = Z_XLS_P(getThis());
  502. if (obj->read_ptr.file_t == NULL) {
  503. RETURN_NULL();
  504. }
  505. obj->read_ptr.sheet_t = sheet_open(obj->read_ptr.file_t, sheet_name);
  506. }
  507. /* }}} */
  508. /** {{{ \Vtiful\Kernel\xls::getSheetData()
  509. */
  510. PHP_METHOD(vtiful_xls, getSheetData)
  511. {
  512. xls_object *obj = Z_XLS_P(getThis());
  513. if (!obj->read_ptr.sheet_t) {
  514. RETURN_FALSE;
  515. }
  516. load_sheet_all_data(obj->read_ptr.sheet_t, return_value);
  517. }
  518. /* }}} */
  519. /** {{{ \Vtiful\Kernel\xls::nextRow()
  520. */
  521. PHP_METHOD(vtiful_xls, nextRow)
  522. {
  523. xls_object *obj = Z_XLS_P(getThis());
  524. if (!obj->read_ptr.sheet_t) {
  525. RETURN_FALSE;
  526. }
  527. load_sheet_current_row_data(obj->read_ptr.sheet_t, return_value, READ_ROW);
  528. }
  529. /* }}} */
  530. /** {{{ xls_methods
  531. */
  532. zend_function_entry xls_methods[] = {
  533. PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC)
  534. PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
  535. PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
  536. PHP_ME(vtiful_xls, checkoutSheet, xls_file_checkout_sheet, ZEND_ACC_PUBLIC)
  537. PHP_ME(vtiful_xls, constMemory, xls_const_memory_arginfo, ZEND_ACC_PUBLIC)
  538. PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
  539. PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
  540. PHP_ME(vtiful_xls, output, NULL, ZEND_ACC_PUBLIC)
  541. PHP_ME(vtiful_xls, getHandle, NULL, ZEND_ACC_PUBLIC)
  542. PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
  543. PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
  544. PHP_ME(vtiful_xls, insertChart, xls_insert_chart_arginfo, ZEND_ACC_PUBLIC)
  545. PHP_ME(vtiful_xls, insertUrl, xls_insert_url_arginfo, ZEND_ACC_PUBLIC)
  546. PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
  547. PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
  548. PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
  549. PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
  550. PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
  551. PHP_ME(vtiful_xls, openFile, NULL, ZEND_ACC_PUBLIC)
  552. PHP_ME(vtiful_xls, openSheet, NULL, ZEND_ACC_PUBLIC)
  553. PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
  554. //PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
  555. PHP_FE_END
  556. };
  557. /* }}} */
  558. /** {{{ VTIFUL_STARTUP_FUNCTION
  559. */
  560. VTIFUL_STARTUP_FUNCTION(excel) {
  561. zend_class_entry ce;
  562. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", xls_methods);
  563. ce.create_object = vtiful_xls_objects_new;
  564. vtiful_xls_ce = zend_register_internal_class(&ce);
  565. memcpy(&vtiful_xls_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  566. vtiful_xls_handlers.offset = XtOffsetOf(xls_object, zo);
  567. vtiful_xls_handlers.free_obj = vtiful_xls_objects_free;
  568. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
  569. REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
  570. return SUCCESS;
  571. }
  572. /* }}} */