Browse Source

Merge pull request #412 from viest/dev

Feat: string Key, data function batch processing
viest 3 years ago
parent
commit
33b0b88ae1
2 changed files with 81 additions and 6 deletions
  1. 19 6
      kernel/excel.c
  2. 62 0
      tests/data_string_key.phpt

+ 19 - 6
kernel/excel.c

@@ -623,6 +623,7 @@ PHP_METHOD(vtiful_xls, header)
  */
 PHP_METHOD(vtiful_xls, data)
 {
+    zend_ulong column_index = 0;
     zval *data = NULL, *data_r_value = NULL;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -636,13 +637,25 @@ PHP_METHOD(vtiful_xls, data)
     WORKBOOK_NOT_INITIALIZED(obj);
 
     ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
-        if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
-            ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
-                type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->write_ptr, NULL, obj->format_ptr.format);
-            ZEND_HASH_FOREACH_END();
-
-            SHEET_LINE_ADD(obj)
+        if(Z_TYPE_P(data_r_value) != IS_ARRAY) {
+            continue;
         }
+
+        column_index = 0;
+
+        ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
+            // numeric index rewriting
+            if (bucket->key == NULL) {
+                column_index = bucket->h;
+            }
+
+            type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), column_index, &obj->write_ptr, NULL, obj->format_ptr.format);
+
+            // next number index
+            ++column_index;
+        ZEND_HASH_FOREACH_END();
+
+        SHEET_LINE_ADD(obj)
     ZEND_HASH_FOREACH_END();
 }
 /* }}} */

+ 62 - 0
tests/data_string_key.phpt

@@ -0,0 +1,62 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php
+require __DIR__ . '/include/skipif.inc';
+skip_disable_reader();
+?>
+--FILE--
+<?php
+$excel = new \Vtiful\Kernel\Excel([
+    'path' => './tests',
+]);
+
+$fileObject = $excel->constMemory('data_string_key.xlsx', NULL, false);
+$fileHandle = $fileObject->getHandle();
+
+$path = $fileObject->header(['name', 'age'])
+    ->data([
+        ['name'=>'viest', 'age' => 21, 1 => 23],
+        ['name'=>'viest', 'age' => 21],
+        ['name'=>'viest', 'age' => 21],
+        ['viest', 21],
+    ])
+    ->output();
+
+$excel->openFile('data_string_key.xlsx')
+    ->openSheet();
+
+var_dump($excel->nextRow());
+var_dump($excel->nextRow());
+var_dump($excel->nextRow());
+var_dump($excel->nextRow());
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/open_xlsx_next_row.xlsx');
+?>
+--EXPECT--
+array(2) {
+  [0]=>
+  string(4) "name"
+  [1]=>
+  string(3) "age"
+}
+array(2) {
+  [0]=>
+  string(5) "viest"
+  [1]=>
+  int(23)
+}
+array(2) {
+  [0]=>
+  string(5) "viest"
+  [1]=>
+  int(21)
+}
+array(2) {
+  [0]=>
+  string(5) "viest"
+  [1]=>
+  int(21)
+}