Skip to content

Commit b2c95e3

Browse files
committed
checkout sheet
1 parent ae85049 commit b2c95e3

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

include/xlswriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
9898
#define SHEET_LINE_ADD(obj_p) \
9999
++obj_p->line;
100100

101+
#define SHEET_LINE_SET(obj_p, line) \
102+
obj_p->line = line;
103+
101104
#define SHEET_CURRENT_LINE(obj_p) obj_p->line
102105

103106
lxw_format * zval_get_format(zval *handle);

kernel/excel.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
7171
ZEND_ARG_INFO(0, sheet_name)
7272
ZEND_END_ARG_INFO()
7373

74+
ZEND_BEGIN_ARG_INFO_EX(xls_file_checkout_sheet, 0, 0, 1)
75+
ZEND_ARG_INFO(0, sheet_name)
76+
ZEND_END_ARG_INFO()
77+
7478
ZEND_BEGIN_ARG_INFO_EX(xls_header_arginfo, 0, 0, 1)
7579
ZEND_ARG_INFO(0, header)
7680
ZEND_END_ARG_INFO()
@@ -229,6 +233,40 @@ PHP_METHOD(vtiful_xls, addSheet)
229233
}
230234
/* }}} */
231235

236+
/** {{{ \Vtiful\Kernel\xls::checkoutSheet(string $sheetName)
237+
*/
238+
PHP_METHOD(vtiful_xls, checkoutSheet)
239+
{
240+
int line = 0;
241+
lxw_worksheet *sheet_t = NULL;
242+
zend_string *zs_sheet_name = NULL;
243+
244+
ZEND_PARSE_PARAMETERS_START(1, 1)
245+
Z_PARAM_STR(zs_sheet_name)
246+
ZEND_PARSE_PARAMETERS_END();
247+
248+
ZVAL_COPY(return_value, getThis());
249+
250+
xls_object *obj = Z_XLS_P(getThis());
251+
252+
if(obj->ptr.workbook == NULL) {
253+
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
254+
return;
255+
}
256+
257+
if ((sheet_t = workbook_get_worksheet_by_name(obj->ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
258+
zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
259+
return;
260+
}
261+
262+
line = sheet_t->table->cached_row_num;
263+
264+
SHEET_LINE_SET(obj, line);
265+
266+
obj->ptr.worksheet = sheet_t;
267+
}
268+
/* }}} */
269+
232270
/** {{{ \Vtiful\Kernel\xls::constMemory(string $fileName [, string $sheetName])
233271
*/
234272
PHP_METHOD(vtiful_xls, constMemory)
@@ -575,6 +613,7 @@ zend_function_entry xls_methods[] = {
575613
PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC)
576614
PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
577615
PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
616+
PHP_ME(vtiful_xls, checkoutSheet, xls_file_checkout_sheet, ZEND_ACC_PUBLIC)
578617
PHP_ME(vtiful_xls, constMemory, xls_const_memory_arginfo, ZEND_ACC_PUBLIC)
579618
PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
580619
PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
File renamed without changes.

tests/sheet_checkout.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = ['path' => './tests'];
8+
$excel = new \Vtiful\Kernel\Excel($config);
9+
10+
$fileObject = $excel->fileName("tutorial01.xlsx");
11+
12+
$fileObject->header(['name', 'age'])
13+
->data([
14+
['viest', 21],
15+
['viest', 22],
16+
['viest', 23],
17+
]);
18+
19+
$fileObject->addSheet('twoSheet')
20+
->header(['name', 'age'])
21+
->data([['vikin', 22]]);
22+
23+
$fileObject->checkoutSheet('Sheet1')
24+
->data([['sheet1']]);
25+
26+
$filePath = $fileObject->output();
27+
28+
var_dump($filePath);
29+
?>
30+
--CLEAN--
31+
<?php
32+
@unlink(__DIR__ . '/tutorial01.xlsx');
33+
?>
34+
--EXPECT--
35+
string(23) "./tests/tutorial01.xlsx"

0 commit comments

Comments
 (0)