Skip to content

Commit 9110b81

Browse files
committed
Test: open xlsx
1 parent 5176a92 commit 9110b81

File tree

8 files changed

+255
-12
lines changed

8 files changed

+255
-12
lines changed

ide-helper/helper.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Excel
1616
const TYPE_DOUBLE = 0x04;
1717
const TYPE_TIMESTAMP = 0x08;
1818

19+
const SKIP_NONE = 0x00;
20+
const SKIP_EMPTY_ROW = 0x01;
21+
const SKIP_EMPTY_CELLS = 0x02;
22+
1923
/**
2024
* Excel constructor.
2125
*
@@ -328,12 +332,13 @@ public function openFile(string $fileName): self
328332
* default open first sheet
329333
*
330334
* @param string|NULL $sheetName
335+
* @param int skipFlag
331336
*
332337
* @return Excel
333338
*
334339
* @author viest
335340
*/
336-
public function openSheet(string $sheetName = NULL): self
341+
public function openSheet(string $sheetName = NULL, int $skipFlag = 0x00): self
337342
{
338343
return $this;
339344
}
@@ -361,6 +366,19 @@ public function nextRow(): array
361366
{
362367
return [];
363368
}
369+
370+
/**
371+
* Next Cell In Callback
372+
*
373+
* @param callable $callback function(int $row, int $cell, string $data)
374+
* @param string|NULL $sheetName sheet name
375+
*
376+
* @author viest
377+
*/
378+
public function nextCellCallback(callable $callback, string $sheetName = NULL): void
379+
{
380+
//
381+
}
364382
}
365383

366384
/**

include/excel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#define V_XLS_CONST_READ_TYPE_STRING "TYPE_STRING"
2424
#define V_XLS_CONST_READ_TYPE_DATETIME "TYPE_TIMESTAMP"
2525

26+
#define V_XLS_CONST_READ_SKIP_NONE "SKIP_NONE"
27+
#define V_XLS_CONST_READ_SKIP_EMPTY_ROW "SKIP_EMPTY_ROW"
28+
#define V_XLS_CONST_READ_SKIP_EMPTY_CELLS "SKIP_EMPTY_CELLS"
29+
2630
#define READ_TYPE_EMPTY 0x00
2731
#define READ_TYPE_STRING 0x01
2832
#define READ_TYPE_INT 0x02

include/read.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
int sheet_read_row(xlsxioreadersheet sheet_t);
2020
xlsxioreader file_open(const char *directory, const char *file_name);
2121
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t);
22-
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t);
22+
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag);
2323
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type, unsigned int flag);
2424
unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data);
2525

kernel/excel.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ ZEND_END_ARG_INFO()
162162
ZEND_BEGIN_ARG_INFO_EX(xls_open_sheet_arginfo, 0, 0, 1)
163163
ZEND_ARG_INFO(0, zs_sheet_name)
164164
ZEND_END_ARG_INFO()
165+
166+
ZEND_BEGIN_ARG_INFO_EX(xls_next_cell_callback_arginfo, 0, 0, 2)
167+
ZEND_ARG_INFO(0, fci)
168+
ZEND_ARG_INFO(0, sheet_name)
169+
ZEND_END_ARG_INFO()
165170
/* }}} */
166171

167172
/** {{{ \Vtiful\Kernel\xls::__construct(array $config)
@@ -728,11 +733,13 @@ PHP_METHOD(vtiful_xls, openFile)
728733
*/
729734
PHP_METHOD(vtiful_xls, openSheet)
730735
{
736+
zend_long zl_flag = XLSXIOREAD_SKIP_NONE;
731737
zend_string *zs_sheet_name = NULL;
732738

733-
ZEND_PARSE_PARAMETERS_START(0, 1)
739+
ZEND_PARSE_PARAMETERS_START(0, 2)
734740
Z_PARAM_OPTIONAL
735741
Z_PARAM_STR(zs_sheet_name)
742+
Z_PARAM_LONG(zl_flag)
736743
ZEND_PARSE_PARAMETERS_END();
737744

738745
ZVAL_COPY(return_value, getThis());
@@ -743,7 +750,7 @@ PHP_METHOD(vtiful_xls, openSheet)
743750
RETURN_NULL();
744751
}
745752

746-
obj->read_ptr.sheet_t = sheet_open(obj->read_ptr.file_t, zs_sheet_name);
753+
obj->read_ptr.sheet_t = sheet_open(obj->read_ptr.file_t, zs_sheet_name, zl_flag);
747754
}
748755
/* }}} */
749756

@@ -837,11 +844,11 @@ zend_function_entry xls_methods[] = {
837844
PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
838845

839846
#ifdef ENABLE_READER
840-
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)
841-
PHP_ME(vtiful_xls, openSheet, xls_open_sheet_arginfo, ZEND_ACC_PUBLIC)
842-
PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
843-
PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
844-
PHP_ME(vtiful_xls, nextCellCallback, NULL, ZEND_ACC_PUBLIC)
847+
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)
848+
PHP_ME(vtiful_xls, openSheet, xls_open_sheet_arginfo, ZEND_ACC_PUBLIC)
849+
PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
850+
PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
851+
PHP_ME(vtiful_xls, nextCellCallback, xls_next_cell_callback_arginfo, ZEND_ACC_PUBLIC)
845852
#endif
846853

847854
PHP_FE_END
@@ -864,6 +871,10 @@ VTIFUL_STARTUP_FUNCTION(excel) {
864871
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
865872
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
866873

874+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_NONE, XLSXIOREAD_SKIP_NONE);
875+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_EMPTY_ROW, XLSXIOREAD_SKIP_EMPTY_ROWS);
876+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_EMPTY_CELLS, XLSXIOREAD_SKIP_EMPTY_CELLS);
877+
867878
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_INT, READ_TYPE_INT);
868879
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_DOUBLE, READ_TYPE_DOUBLE);
869880
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_STRING, READ_TYPE_STRING);

kernel/read.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
3131
/* }}} */
3232

3333
/* {{{ */
34-
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t)
34+
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag)
3535
{
3636
if (zs_sheet_name_t == NULL) {
37-
return xlsxioread_sheet_open(file_t, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS);
37+
return xlsxioread_sheet_open(file_t, NULL, zl_flag);
3838
}
3939

40-
return xlsxioread_sheet_open(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_EMPTY_ROWS);
40+
return xlsxioread_sheet_open(file_t, ZSTR_VAL(zs_sheet_name_t), zl_flag);
4141
}
4242
/* }}} */
4343

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/include/skipif.inc';
6+
skip_disable_reader();
7+
?>
8+
--FILE--
9+
<?php
10+
$config = ['path' => './tests'];
11+
$excel = new \Vtiful\Kernel\Excel($config);
12+
$filePath = $excel->fileName('tutorial.xlsx')
13+
->header(['', 'Cost'])
14+
->data([
15+
[],
16+
['viest', '']
17+
])
18+
->output();
19+
20+
$data = $excel->openFile('tutorial.xlsx')
21+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS)
22+
->getSheetData();
23+
24+
var_dump($data);
25+
26+
$data = $excel->openFile('tutorial.xlsx')
27+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW)
28+
->getSheetData();
29+
30+
var_dump($data);
31+
32+
$data = $excel->openFile('tutorial.xlsx')
33+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS|\Vtiful\Kernel\Excel::SKIP_EMPTY_ROW)
34+
->getSheetData();
35+
36+
var_dump($data);
37+
?>
38+
--CLEAN--
39+
<?php
40+
@unlink(__DIR__ . '/tutorial.xlsx');
41+
?>
42+
--EXPECT--
43+
array(3) {
44+
[0]=>
45+
array(1) {
46+
[0]=>
47+
string(4) "Cost"
48+
}
49+
[1]=>
50+
array(2) {
51+
[0]=>
52+
string(0) ""
53+
[1]=>
54+
string(0) ""
55+
}
56+
[2]=>
57+
array(1) {
58+
[0]=>
59+
string(5) "viest"
60+
}
61+
}
62+
array(2) {
63+
[0]=>
64+
array(2) {
65+
[0]=>
66+
string(0) ""
67+
[1]=>
68+
string(4) "Cost"
69+
}
70+
[1]=>
71+
array(2) {
72+
[0]=>
73+
string(5) "viest"
74+
[1]=>
75+
string(0) ""
76+
}
77+
}
78+
array(2) {
79+
[0]=>
80+
array(1) {
81+
[0]=>
82+
string(4) "Cost"
83+
}
84+
[1]=>
85+
array(1) {
86+
[0]=>
87+
string(5) "viest"
88+
}
89+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/include/skipif.inc';
6+
skip_disable_reader();
7+
?>
8+
--FILE--
9+
<?php
10+
$config = ['path' => './tests'];
11+
$excel = new \Vtiful\Kernel\Excel($config);
12+
$filePath = $excel->fileName('tutorial.xlsx')
13+
->header(['', 'Cost'])
14+
->data([
15+
[],
16+
['viest', ''],
17+
])
18+
->output();
19+
20+
echo 'skip cells' . PHP_EOL;
21+
22+
$data = $excel->openFile('tutorial.xlsx')
23+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS);
24+
25+
while ($data = $excel->nextRow()) {
26+
var_dump($data);
27+
}
28+
29+
echo 'skip row' . PHP_EOL;
30+
31+
$data = $excel->openFile('tutorial.xlsx')
32+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW);
33+
34+
while ($data = $excel->nextRow()) {
35+
var_dump($data);
36+
}
37+
38+
echo 'skip cells & row' . PHP_EOL;
39+
40+
$data = $excel->openFile('tutorial.xlsx')
41+
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS | \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW);
42+
43+
while ($data = $excel->nextRow()) {
44+
var_dump($data);
45+
}
46+
?>
47+
--CLEAN--
48+
<?php
49+
@unlink(__DIR__ . '/tutorial.xlsx');
50+
?>
51+
--EXPECT--
52+
skip cells
53+
array(1) {
54+
[0]=>
55+
string(4) "Cost"
56+
}
57+
array(2) {
58+
[0]=>
59+
string(0) ""
60+
[1]=>
61+
string(0) ""
62+
}
63+
array(1) {
64+
[0]=>
65+
string(5) "viest"
66+
}
67+
skip row
68+
array(2) {
69+
[0]=>
70+
string(0) ""
71+
[1]=>
72+
string(4) "Cost"
73+
}
74+
array(2) {
75+
[0]=>
76+
string(5) "viest"
77+
[1]=>
78+
string(0) ""
79+
}
80+
skip cells & row
81+
array(1) {
82+
[0]=>
83+
string(4) "Cost"
84+
}
85+
array(1) {
86+
[0]=>
87+
string(5) "viest"
88+
}

tests/open_xlsx_sheet_flag.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/include/skipif.inc';
6+
skip_disable_reader();
7+
?>
8+
--FILE--
9+
<?php
10+
$config = ['path' => './tests'];
11+
$excel = new \Vtiful\Kernel\Excel($config);
12+
$filePath = $excel->fileName('tutorial.xlsx')
13+
->header(['Item', 'Cost'])
14+
->output();
15+
16+
$data = $excel->openFile('tutorial.xlsx')->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS);
17+
18+
var_dump($data);
19+
?>
20+
--CLEAN--
21+
<?php
22+
@unlink(__DIR__ . '/tutorial.xlsx');
23+
?>
24+
--EXPECT--
25+
object(Vtiful\Kernel\Excel)#1 (2) {
26+
["config":"Vtiful\Kernel\Excel":private]=>
27+
array(1) {
28+
["path"]=>
29+
string(7) "./tests"
30+
}
31+
["fileName":"Vtiful\Kernel\Excel":private]=>
32+
string(21) "./tests/tutorial.xlsx"
33+
}

0 commit comments

Comments
 (0)