Skip to content

Commit 7d136b2

Browse files
authored
Merge pull request #161 from viest/dev
Feat insert date and read data with type
2 parents 1d0ae54 + 2e70313 commit 7d136b2

File tree

9 files changed

+310
-23
lines changed

9 files changed

+310
-23
lines changed

include/excel.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
#define V_XLS_COF "config"
1919
#define V_XLS_PAT "path"
2020

21+
#define V_XLS_CONST_READ_TYPE_INT "TYPE_INT"
22+
#define V_XLS_CONST_READ_TYPE_DOUBLE "TYPE_DOUBLE"
23+
#define V_XLS_CONST_READ_TYPE_STRING "TYPE_STRING"
24+
#define V_XLS_CONST_READ_TYPE_DATETIME "TYPE_TIMESTAMP"
25+
26+
#define READ_TYPE_EMPTY 0x00
27+
#define READ_TYPE_STRING 0x01
28+
#define READ_TYPE_INT 0x02
29+
#define READ_TYPE_DOUBLE 0x04
30+
#define READ_TYPE_DATETIME 0x08
31+
2132
#define GET_CONFIG_PATH(dir_path_res, class_name, object) \
2233
do { \
2334
zval rv; \

include/read.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
#define READ_ROW 0x01
1818

1919
int sheet_read_row(xlsxioreadersheet sheet);
20-
char* sheet_read_column(xlsxioreadersheet sheet);
2120
xlsxioreader file_open(const char *directory, const char *file_name);
2221
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t);
2322
xlsxioreadersheet sheet_open(xlsxioreader file, const zend_string *zs_sheet_name_t);
24-
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, unsigned int flag);
23+
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type, unsigned int flag);
2524

2625
#endif //PHP_READ_INCLUDE_H

include/xlswriter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
109109
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
110110
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
111111

112+
#define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
113+
do { \
114+
if(obj->write_ptr.workbook == NULL) { \
115+
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130); \
116+
return; \
117+
} \
118+
} while(0);
119+
112120
#define ROW(range) \
113121
lxw_name_to_row(range)
114122

kernel/excel.c

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ ZEND_BEGIN_ARG_INFO_EX(xls_insert_text_arginfo, 0, 0, 5)
9999
ZEND_ARG_INFO(0, format_handle)
100100
ZEND_END_ARG_INFO()
101101

102+
ZEND_BEGIN_ARG_INFO_EX(xls_insert_date_arginfo, 0, 0, 5)
103+
ZEND_ARG_INFO(0, row)
104+
ZEND_ARG_INFO(0, column)
105+
ZEND_ARG_INFO(0, timestamp)
106+
ZEND_ARG_INFO(0, format)
107+
ZEND_ARG_INFO(0, format_handle)
108+
ZEND_END_ARG_INFO()
109+
102110
ZEND_BEGIN_ARG_INFO_EX(xls_insert_url_arginfo, 0, 0, 4)
103111
ZEND_ARG_INFO(0, row)
104112
ZEND_ARG_INFO(0, column)
@@ -235,13 +243,9 @@ PHP_METHOD(vtiful_xls, addSheet)
235243

236244
xls_object *obj = Z_XLS_P(getThis());
237245

246+
WORKBOOK_NOT_INITIALIZED(obj);
238247
SHEET_LINE_INIT(obj)
239248

240-
if(obj->write_ptr.workbook == NULL) {
241-
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
242-
return;
243-
}
244-
245249
if(zs_sheet_name != NULL) {
246250
sheet_name = ZSTR_VAL(zs_sheet_name);
247251
}
@@ -266,10 +270,7 @@ PHP_METHOD(vtiful_xls, checkoutSheet)
266270

267271
xls_object *obj = Z_XLS_P(getThis());
268272

269-
if(obj->write_ptr.workbook == NULL) {
270-
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
271-
return;
272-
}
273+
WORKBOOK_NOT_INITIALIZED(obj);
273274

274275
if ((sheet_t = workbook_get_worksheet_by_name(obj->write_ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
275276
zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
@@ -339,6 +340,8 @@ PHP_METHOD(vtiful_xls, header)
339340

340341
xls_object *obj = Z_XLS_P(getThis());
341342

343+
WORKBOOK_NOT_INITIALIZED(obj);
344+
342345
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
343346
type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, NULL);
344347
zval_ptr_dtor(header_value);
@@ -360,6 +363,8 @@ PHP_METHOD(vtiful_xls, data)
360363

361364
xls_object *obj = Z_XLS_P(getThis());
362365

366+
WORKBOOK_NOT_INITIALIZED(obj);
367+
363368
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
364369
if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
365370
SHEET_LINE_ADD(obj)
@@ -383,6 +388,8 @@ PHP_METHOD(vtiful_xls, output)
383388

384389
xls_object *obj = Z_XLS_P(getThis());
385390

391+
WORKBOOK_NOT_INITIALIZED(obj);
392+
386393
workbook_file(&obj->write_ptr);
387394

388395
ZVAL_COPY(return_value, file_path);
@@ -420,14 +427,63 @@ PHP_METHOD(vtiful_xls, insertText)
420427

421428
xls_object *obj = Z_XLS_P(getThis());
422429

430+
WORKBOOK_NOT_INITIALIZED(obj);
431+
423432
SHEET_LINE_SET(obj, row);
424433

425434
if (format_handle) {
426435
type_writer(data, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
427436
} else {
428437
type_writer(data, row, column, &obj->write_ptr, format, NULL);
429438
}
439+
}
440+
/* }}} */
441+
442+
/** {{{ \Vtiful\Kernel\xls::insertDate(int $row, int $column, int $timestamp[, string $format, resource $formatHandle])
443+
*/
444+
PHP_METHOD(vtiful_xls, insertDate)
445+
{
446+
zval *data = NULL, *format_handle = NULL;
447+
zend_long row, column;
448+
zend_string *format = NULL;
449+
450+
ZEND_PARSE_PARAMETERS_START(3, 5)
451+
Z_PARAM_LONG(row)
452+
Z_PARAM_LONG(column)
453+
Z_PARAM_ZVAL(data)
454+
Z_PARAM_OPTIONAL
455+
Z_PARAM_STR(format)
456+
Z_PARAM_RESOURCE(format_handle)
457+
ZEND_PARSE_PARAMETERS_END();
458+
459+
ZVAL_COPY(return_value, getThis());
460+
461+
xls_object *obj = Z_XLS_P(getThis());
462+
463+
WORKBOOK_NOT_INITIALIZED(obj);
464+
SHEET_LINE_SET(obj, row);
465+
466+
if (Z_TYPE_P(data) != IS_LONG) {
467+
zend_throw_exception(vtiful_exception_ce, "timestamp is long", 160);
468+
return;
469+
}
470+
471+
// Default datetime format
472+
if (format == NULL) {
473+
format = zend_string_init(ZEND_STRL("yyyy-mm-dd hh:mm:ss"), 0);
474+
}
475+
476+
zval _zv_double_time;
477+
ZVAL_DOUBLE(&_zv_double_time, ((double)data->value.lval / 86400 + 25569));
478+
479+
if (format_handle) {
480+
type_writer(&_zv_double_time, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
481+
} else {
482+
type_writer(&_zv_double_time, row, column, &obj->write_ptr, format, NULL);
483+
}
430484

485+
zend_string_release(format);
486+
zval_ptr_dtor(&_zv_double_time);
431487
}
432488
/* }}} */
433489

@@ -448,6 +504,8 @@ PHP_METHOD(vtiful_xls, insertChart)
448504

449505
xls_object *obj = Z_XLS_P(getThis());
450506

507+
WORKBOOK_NOT_INITIALIZED(obj);
508+
451509
chart_writer(row, column, zval_get_chart(chart_resource), &obj->write_ptr);
452510
}
453511
/* }}} */
@@ -474,6 +532,8 @@ PHP_METHOD(vtiful_xls, insertUrl)
474532

475533
xls_object *obj = Z_XLS_P(getThis());
476534

535+
WORKBOOK_NOT_INITIALIZED(obj);
536+
477537
if (argc == 4) {
478538
url_writer(row, column, &obj->write_ptr, url, zval_get_format(format_handle));
479539
}
@@ -505,6 +565,8 @@ PHP_METHOD(vtiful_xls, insertImage)
505565

506566
xls_object *obj = Z_XLS_P(getThis());
507567

568+
WORKBOOK_NOT_INITIALIZED(obj);
569+
508570
image_writer(image, row, column, width, height, &obj->write_ptr);
509571
}
510572
/* }}} */
@@ -526,6 +588,8 @@ PHP_METHOD(vtiful_xls, insertFormula)
526588

527589
xls_object *obj = Z_XLS_P(getThis());
528590

591+
WORKBOOK_NOT_INITIALIZED(obj);
592+
529593
formula_writer(formula, row, column, &obj->write_ptr);
530594
}
531595
/* }}} */
@@ -544,6 +608,8 @@ PHP_METHOD(vtiful_xls, autoFilter)
544608

545609
xls_object *obj = Z_XLS_P(getThis());
546610

611+
WORKBOOK_NOT_INITIALIZED(obj);
612+
547613
auto_filter(range, &obj->write_ptr);
548614
}
549615
/* }}} */
@@ -563,6 +629,8 @@ PHP_METHOD(vtiful_xls, mergeCells)
563629

564630
xls_object *obj = Z_XLS_P(getThis());
565631

632+
WORKBOOK_NOT_INITIALIZED(obj);
633+
566634
merge_cells(range, data, &obj->write_ptr);
567635
}
568636
/* }}} */
@@ -588,6 +656,8 @@ PHP_METHOD(vtiful_xls, setColumn)
588656

589657
xls_object *obj = Z_XLS_P(getThis());
590658

659+
WORKBOOK_NOT_INITIALIZED(obj);
660+
591661
if (argc == 3) {
592662
set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
593663
}
@@ -619,6 +689,8 @@ PHP_METHOD(vtiful_xls, setRow)
619689

620690
xls_object *obj = Z_XLS_P(getThis());
621691

692+
WORKBOOK_NOT_INITIALIZED(obj);
693+
622694
if (argc == 3) {
623695
set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
624696
}
@@ -693,13 +765,20 @@ PHP_METHOD(vtiful_xls, getSheetData)
693765
*/
694766
PHP_METHOD(vtiful_xls, nextRow)
695767
{
696-
xls_object *obj = Z_XLS_P(getThis());
768+
zval *zv_type = NULL;
769+
770+
ZEND_PARSE_PARAMETERS_START(0, 1)
771+
Z_PARAM_OPTIONAL
772+
Z_PARAM_ARRAY(zv_type)
773+
ZEND_PARSE_PARAMETERS_END();
774+
775+
xls_object *obj = Z_XLS_P(getThis());
697776

698777
if (!obj->read_ptr.sheet_t) {
699778
RETURN_FALSE;
700779
}
701780

702-
load_sheet_current_row_data(obj->read_ptr.sheet_t, return_value, READ_ROW);
781+
load_sheet_current_row_data(obj->read_ptr.sheet_t, return_value, zv_type, READ_ROW);
703782
}
704783
/* }}} */
705784

@@ -719,6 +798,7 @@ zend_function_entry xls_methods[] = {
719798
PHP_ME(vtiful_xls, getHandle, NULL, ZEND_ACC_PUBLIC)
720799
PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
721800
PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
801+
PHP_ME(vtiful_xls, insertDate, xls_insert_date_arginfo, ZEND_ACC_PUBLIC)
722802
PHP_ME(vtiful_xls, insertChart, xls_insert_chart_arginfo, ZEND_ACC_PUBLIC)
723803
PHP_ME(vtiful_xls, insertUrl, xls_insert_url_arginfo, ZEND_ACC_PUBLIC)
724804
PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
@@ -754,6 +834,11 @@ VTIFUL_STARTUP_FUNCTION(excel) {
754834
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
755835
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
756836

837+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_INT, READ_TYPE_INT);
838+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_DOUBLE, READ_TYPE_DOUBLE);
839+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_STRING, READ_TYPE_STRING);
840+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_DATETIME, READ_TYPE_DATETIME);
841+
757842
return SUCCESS;
758843
}
759844
/* }}} */

kernel/read.c

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,11 @@ int sheet_read_row(xlsxioreadersheet sheet)
4949
/* }}} */
5050

5151
/* {{{ */
52-
char* sheet_read_column(xlsxioreadersheet sheet)
53-
{
54-
return xlsxioread_sheet_next_cell(sheet);
55-
}
56-
/* }}} */
57-
58-
/* {{{ */
59-
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, unsigned int flag)
52+
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type_arr_t, unsigned int flag)
6053
{
54+
zend_long _type = READ_TYPE_EMPTY;
6155
char *_string_value = NULL;
56+
Bucket *_start = NULL, *_end = NULL;
6257

6358
if (flag && !sheet_read_row(sheet_t)) {
6459
return XLSWRITER_FALSE;
@@ -68,8 +63,47 @@ unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_res
6863
array_init(zv_result_t);
6964
}
7065

71-
while ((_string_value = sheet_read_column(sheet_t)) != NULL)
66+
if (zv_type_arr_t != NULL && Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
67+
_start = zv_type_arr_t->value.arr->arData;
68+
_end = _start + zv_type_arr_t->value.arr->nNumUsed;
69+
}
70+
71+
while ((_string_value = xlsxioread_sheet_next_cell(sheet_t)) != NULL)
7272
{
73+
if (_start != _end) {
74+
zval *_zv_type = &_start->val;
75+
76+
if (Z_TYPE_P(_zv_type) == IS_LONG) {
77+
_type = Z_LVAL_P(_zv_type);
78+
}
79+
80+
_start++;
81+
}
82+
83+
if (_type & READ_TYPE_DATETIME) {
84+
double value = strtod(_string_value, NULL);
85+
86+
if (value != 0) {
87+
value = (value - 25569) * 86400;
88+
}
89+
90+
add_next_index_double(zv_result_t, value);
91+
continue;
92+
}
93+
94+
if (_type & READ_TYPE_DOUBLE) {
95+
add_next_index_double(zv_result_t, strtod(_string_value, NULL));
96+
continue;
97+
}
98+
99+
if (_type & READ_TYPE_INT) {
100+
zend_long _long_value;
101+
102+
sscanf(_string_value, "%" PRIi64, &_long_value);
103+
add_next_index_long(zv_result_t, _long_value);
104+
continue;
105+
}
106+
73107
add_next_index_stringl(zv_result_t, _string_value, strlen(_string_value));
74108
}
75109

@@ -89,7 +123,7 @@ void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t)
89123
zval _zv_tmp_row;
90124
ZVAL_NULL(&_zv_tmp_row);
91125

92-
load_sheet_current_row_data(sheet_t, &_zv_tmp_row, READ_SKIP_ROW);
126+
load_sheet_current_row_data(sheet_t, &_zv_tmp_row, NULL, READ_SKIP_ROW);
93127
add_next_index_zval(zv_result_t, &_zv_tmp_row);
94128
}
95129
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = [
8+
'path' => './tests'
9+
];
10+
11+
$fileObject = new \Vtiful\Kernel\Excel($config);
12+
$fileObject = $fileObject->fileName('tutorial.xlsx');
13+
14+
$filePath = $fileObject->header(['date'])
15+
->insertDate(1, 0, time(), 'mmm d yyyy hh:mm AM/PM')
16+
->output();
17+
18+
var_dump($filePath);
19+
?>
20+
--CLEAN--
21+
<?php
22+
@unlink(__DIR__ . '/tutorial.xlsx');
23+
?>
24+
--EXPECT--
25+
string(21) "./tests/tutorial.xlsx"

0 commit comments

Comments
 (0)