Skip to content

Commit 33b0b88

Browse files
authored
Merge pull request #412 from viest/dev
Feat: string Key, data function batch processing
2 parents 6081722 + df3b726 commit 33b0b88

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

kernel/excel.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ PHP_METHOD(vtiful_xls, header)
623623
*/
624624
PHP_METHOD(vtiful_xls, data)
625625
{
626+
zend_ulong column_index = 0;
626627
zval *data = NULL, *data_r_value = NULL;
627628

628629
ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -636,13 +637,25 @@ PHP_METHOD(vtiful_xls, data)
636637
WORKBOOK_NOT_INITIALIZED(obj);
637638

638639
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
639-
if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
640-
ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
641-
type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->write_ptr, NULL, obj->format_ptr.format);
642-
ZEND_HASH_FOREACH_END();
643-
644-
SHEET_LINE_ADD(obj)
640+
if(Z_TYPE_P(data_r_value) != IS_ARRAY) {
641+
continue;
645642
}
643+
644+
column_index = 0;
645+
646+
ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
647+
// numeric index rewriting
648+
if (bucket->key == NULL) {
649+
column_index = bucket->h;
650+
}
651+
652+
type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), column_index, &obj->write_ptr, NULL, obj->format_ptr.format);
653+
654+
// next number index
655+
++column_index;
656+
ZEND_HASH_FOREACH_END();
657+
658+
SHEET_LINE_ADD(obj)
646659
ZEND_HASH_FOREACH_END();
647660
}
648661
/* }}} */

tests/data_string_key.phpt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
$excel = new \Vtiful\Kernel\Excel([
11+
'path' => './tests',
12+
]);
13+
14+
$fileObject = $excel->constMemory('data_string_key.xlsx', NULL, false);
15+
$fileHandle = $fileObject->getHandle();
16+
17+
$path = $fileObject->header(['name', 'age'])
18+
->data([
19+
['name'=>'viest', 'age' => 21, 1 => 23],
20+
['name'=>'viest', 'age' => 21],
21+
['name'=>'viest', 'age' => 21],
22+
['viest', 21],
23+
])
24+
->output();
25+
26+
$excel->openFile('data_string_key.xlsx')
27+
->openSheet();
28+
29+
var_dump($excel->nextRow());
30+
var_dump($excel->nextRow());
31+
var_dump($excel->nextRow());
32+
var_dump($excel->nextRow());
33+
?>
34+
--CLEAN--
35+
<?php
36+
@unlink(__DIR__ . '/open_xlsx_next_row.xlsx');
37+
?>
38+
--EXPECT--
39+
array(2) {
40+
[0]=>
41+
string(4) "name"
42+
[1]=>
43+
string(3) "age"
44+
}
45+
array(2) {
46+
[0]=>
47+
string(5) "viest"
48+
[1]=>
49+
int(23)
50+
}
51+
array(2) {
52+
[0]=>
53+
string(5) "viest"
54+
[1]=>
55+
int(21)
56+
}
57+
array(2) {
58+
[0]=>
59+
string(5) "viest"
60+
[1]=>
61+
int(21)
62+
}

0 commit comments

Comments
 (0)