Skip to content

Commit

Permalink
Merge pull request #70 from viest/dev
Browse files Browse the repository at this point in the history
 FEAT: Incremental add with default sheet name
  • Loading branch information
viest committed May 31, 2018
2 parents 010cd68 + ee8bdb6 commit dee5471
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
46 changes: 30 additions & 16 deletions kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
{
xls_object *intern = vtiful_object_alloc(sizeof(xls_object), ce);

SHEET_LINE_INIT(intern)

zend_object_std_init(&intern->zo, ce);
object_properties_init(&intern->zo, ce);

Expand Down Expand Up @@ -59,8 +61,9 @@ ZEND_BEGIN_ARG_INFO_EX(xls_construct_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, config)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 1)
ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, file_name)
ZEND_ARG_INFO(0, sheet_name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
Expand Down Expand Up @@ -146,11 +149,14 @@ PHP_METHOD(vtiful_xls, __construct)
*/
PHP_METHOD(vtiful_xls, fileName)
{
zval file_path, *dir_path;
zend_string *file_name;
zval file_path, *dir_path = NULL;
zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
char *sheet_name = NULL;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(file_name)
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(zs_file_name)
Z_PARAM_OPTIONAL
Z_PARAM_STR(zs_sheet_name)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());
Expand All @@ -160,10 +166,14 @@ PHP_METHOD(vtiful_xls, fileName)
xls_object *obj = Z_XLS_P(getThis());

if(obj->ptr.workbook == NULL) {
xls_file_path(file_name, dir_path, &file_path);
xls_file_path(zs_file_name, dir_path, &file_path);

if(zs_sheet_name != NULL) {
sheet_name = ZSTR_VAL(zs_sheet_name);
}

obj->ptr.workbook = workbook_new(Z_STRVAL(file_path));
obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, NULL);
obj->ptr.worksheet = workbook_add_worksheet(obj->ptr.workbook, sheet_name);

add_property_zval(return_value, V_XLS_FIL, &file_path);

Expand All @@ -188,6 +198,8 @@ PHP_METHOD(vtiful_xls, addSheet)

xls_object *obj = Z_XLS_P(getThis());

SHEET_LINE_INIT(obj)

if(obj->ptr.workbook == NULL) {
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
return;
Expand Down Expand Up @@ -249,19 +261,19 @@ PHP_METHOD(vtiful_xls, header)

xls_object *obj = Z_XLS_P(getThis());

ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
zval_ptr_dtor(header_value);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_FOREACH_END();
}
/* }}} */

/** {{{ \Vtiful\Kernel\xls::data(array $data)
*/
PHP_METHOD(vtiful_xls, data)
{
zval *data, *data_r_value, *data_l_value;
zend_long data_r_key, data_l_key;
zval *data = NULL, *data_r_value = NULL, *data_l_value = NULL;
zend_long data_l_key;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(data)
Expand All @@ -271,14 +283,16 @@ PHP_METHOD(vtiful_xls, data)

xls_object *obj = Z_XLS_P(getThis());

ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
type_writer(data_l_value, data_r_key+1, data_l_key, &obj->ptr, NULL);
SHEET_LINE_ADD(obj)

ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value)
type_writer(data_l_value, SHEET_CURRENT_LINE(obj), data_l_key, &obj->ptr, NULL);
zval_ptr_dtor(data_l_value);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_FOREACH_END();
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_FOREACH_END();
}
/* }}} */

Expand Down
9 changes: 9 additions & 0 deletions kernel/include.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct {

typedef struct _vtiful_xls_object {
xls_resource_t ptr;
zend_long line;
zend_object zo;
} xls_object;

Expand All @@ -54,6 +55,14 @@ static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
#define ROW(range) \
lxw_name_to_row(range)

#define SHEET_LINE_INIT(obj_p) \
obj_p->line = 0;

#define SHEET_LINE_ADD(obj_p) \
++obj_p->line;

#define SHEET_CURRENT_LINE(obj_p) obj_p->line

xls_resource_t * zval_get_resource(zval *handle);
lxw_format * zval_get_format(zval *handle);

Expand Down
2 changes: 1 addition & 1 deletion php_xls_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
extern zend_module_entry xlswriter_module_entry;
#define phpext_xlswriter_ptr &xlswriter_module_entry

#define PHP_XLSWRITER_VERSION "1.1.0"
#define PHP_XLSWRITER_VERSION "1.2.0"

#ifdef PHP_WIN32
# define PHP_VTIFUL_API __declspec(dllexport)
Expand Down
25 changes: 25 additions & 0 deletions tests/018.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Check for vtiful presence
--SKIPIF--
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
--FILE--
<?php
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);

$fileObject = $excel->fileName("tutorial01.xlsx");

$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->data([['wjx', 21]]);

$filePath = $fileObject->output();

var_dump($filePath);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/tutorial01.xlsx');
?>
--EXPECT--
string(23) "./tests/tutorial01.xlsx"

0 comments on commit dee5471

Please sign in to comment.