Skip to content

Commit 71b0b5e

Browse files
committed
Feat(insertText): support resource format
1 parent b272940 commit 71b0b5e

File tree

4 files changed

+183
-30
lines changed

4 files changed

+183
-30
lines changed

include/xlswriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ STATIC int _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b);
116116
STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
117117
STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);
118118

119-
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format);
119+
void format_copy(lxw_format *new_format, lxw_format *other_format);
120+
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format, lxw_format *format_handle);
120121
void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_t *res);
121122
void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format);
122123
void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_t *res);

kernel/excel.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ PHP_METHOD(vtiful_xls, header)
323323
xls_object *obj = Z_XLS_P(getThis());
324324

325325
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
326-
type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
326+
type_writer(header_value, 0, header_l_key, &obj->ptr, NULL, NULL);
327327
zval_ptr_dtor(header_value);
328328
ZEND_HASH_FOREACH_END();
329329
}
@@ -348,7 +348,7 @@ PHP_METHOD(vtiful_xls, data)
348348
SHEET_LINE_ADD(obj)
349349

350350
ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
351-
type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->ptr, NULL);
351+
type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->ptr, NULL, NULL);
352352
zval_ptr_dtor(&bucket->val);
353353
ZEND_HASH_FOREACH_END();
354354
}
@@ -385,20 +385,21 @@ PHP_METHOD(vtiful_xls, getHandle)
385385
}
386386
/* }}} */
387387

388-
/** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data)
388+
/** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data[, string $format, resource $formatHandle])
389389
*/
390390
PHP_METHOD(vtiful_xls, insertText)
391391
{
392-
zval *data;
392+
zval *data, *format_handle = NULL;
393393
zend_long row, column;
394394
zend_string *format = NULL;
395395

396-
ZEND_PARSE_PARAMETERS_START(3, 4)
396+
ZEND_PARSE_PARAMETERS_START(3, 5)
397397
Z_PARAM_LONG(row)
398398
Z_PARAM_LONG(column)
399399
Z_PARAM_ZVAL(data)
400400
Z_PARAM_OPTIONAL
401401
Z_PARAM_STR(format)
402+
Z_PARAM_RESOURCE(format_handle)
402403
ZEND_PARSE_PARAMETERS_END();
403404

404405
ZVAL_COPY(return_value, getThis());
@@ -407,7 +408,12 @@ PHP_METHOD(vtiful_xls, insertText)
407408

408409
SHEET_LINE_SET(obj, row);
409410

410-
type_writer(data, row, column, &obj->ptr, format);
411+
if (format_handle) {
412+
type_writer(data, row, column, &obj->ptr, format, zval_get_format(format_handle));
413+
} else {
414+
type_writer(data, row, column, &obj->ptr, format, NULL);
415+
}
416+
411417
}
412418
/* }}} */
413419

kernel/write.c

Lines changed: 137 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,147 @@
1515
/*
1616
* According to the zval type written to the file
1717
*/
18-
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format)
18+
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format, lxw_format *format_handle)
1919
{
2020
lxw_format *value_format = NULL;
2121

22-
switch (Z_TYPE_P(value)) {
23-
case IS_STRING:
24-
worksheet_write_string(res->worksheet, row, columns, ZSTR_VAL(zval_get_string(value)), NULL);
25-
break;
26-
case IS_LONG:
27-
if(format) {
28-
value_format = workbook_add_format(res->workbook);
29-
format_set_num_format(value_format, ZSTR_VAL(format));
30-
worksheet_write_number(res->worksheet, row, columns, zval_get_long(value), value_format);
31-
} else {
32-
worksheet_write_number(res->worksheet, row, columns, zval_get_long(value), NULL);
33-
}
34-
break;
35-
case IS_DOUBLE:
36-
if(format) {
37-
value_format = workbook_add_format(res->workbook);
38-
format_set_num_format(value_format, ZSTR_VAL(format));
39-
worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), value_format);
40-
} else {
41-
worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), NULL);
42-
}
43-
break;
22+
lxw_col_t lxw_col = (lxw_col_t)columns;
23+
lxw_row_t lxw_row = (lxw_row_t)row;
24+
25+
zend_uchar value_type = Z_TYPE_P(value);
26+
27+
if (value_type == IS_STRING) {
28+
worksheet_write_string(res->worksheet, lxw_row, lxw_col, ZSTR_VAL(zval_get_string(value)), format_handle);
29+
return;
30+
}
31+
32+
if (value_type == IS_LONG) {
33+
if (format != NULL && format_handle == NULL) {
34+
value_format = workbook_add_format(res->workbook);
35+
36+
format_set_num_format(value_format, ZSTR_VAL(format));
37+
38+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), value_format);
39+
return;
40+
}
41+
42+
if (format == NULL && format_handle != NULL) {
43+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), format_handle);
44+
return;
45+
}
46+
47+
if(format != NULL && format_handle != NULL) {
48+
value_format = workbook_add_format(res->workbook);
49+
50+
format_copy(value_format, format_handle);
51+
format_set_num_format(value_format, ZSTR_VAL(format));
52+
53+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), value_format);
54+
return;
55+
}
56+
57+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), NULL);
4458
}
59+
60+
if (value_type == IS_DOUBLE) {
61+
if (format != NULL && format_handle == NULL) {
62+
value_format = workbook_add_format(res->workbook);
63+
64+
format_set_num_format(value_format, ZSTR_VAL(format));
65+
66+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format);
67+
return;
68+
}
69+
70+
if (format == NULL && format_handle != NULL) {
71+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), format_handle);
72+
return;
73+
}
74+
75+
if(format != NULL && format_handle != NULL) {
76+
value_format = workbook_add_format(res->workbook);
77+
78+
format_copy(value_format, format_handle);
79+
format_set_num_format(value_format, ZSTR_VAL(format));
80+
81+
worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format);
82+
return;
83+
}
84+
85+
worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), NULL);
86+
return;
87+
}
88+
}
89+
90+
void format_copy(lxw_format *new_format, lxw_format *other_format)
91+
{
92+
new_format->bold = other_format->bold;
93+
new_format->bg_color = other_format->bg_color;
94+
new_format->border_count = other_format->border_count;
95+
new_format->border_index = other_format->border_index;
96+
new_format->bottom = other_format->bottom;
97+
new_format->bottom_color = other_format->bottom_color;
98+
new_format->color_indexed = other_format->color_indexed;
99+
new_format->diag_border = other_format->diag_border;
100+
new_format->diag_color = other_format->diag_color;
101+
102+
new_format->font_size = other_format->font_size;
103+
new_format->bold = other_format->bold;
104+
new_format->italic = other_format->italic;
105+
new_format->font_color = other_format->font_color;
106+
new_format->underline = other_format->underline;
107+
new_format->font_strikeout = other_format->font_strikeout;
108+
new_format->font_outline = other_format->font_outline;
109+
new_format->font_shadow = other_format->font_shadow;
110+
new_format->font_script = other_format->font_script;
111+
new_format->font_family = other_format->font_family;
112+
new_format->font_charset = other_format->font_charset;
113+
new_format->font_condense = other_format->font_condense;
114+
new_format->font_extend = other_format->font_extend;
115+
new_format->theme = other_format->theme;
116+
new_format->hyperlink = other_format->hyperlink;
117+
118+
new_format->hidden = other_format->hidden;
119+
new_format->locked = other_format->locked;
120+
121+
new_format->text_h_align = other_format->text_h_align;
122+
new_format->text_wrap = other_format->text_wrap;
123+
new_format->text_v_align = other_format->text_v_align;
124+
new_format->text_justlast = other_format->text_justlast;
125+
new_format->rotation = other_format->rotation;
126+
127+
new_format->fg_color = other_format->fg_color;
128+
new_format->bg_color = other_format->bg_color;
129+
new_format->pattern = other_format->pattern;
130+
new_format->has_fill = other_format->has_fill;
131+
new_format->has_dxf_fill = other_format->has_dxf_fill;
132+
new_format->fill_index = other_format->fill_index;
133+
new_format->fill_count = other_format->fill_count;
134+
135+
new_format->border_index = other_format->border_index;
136+
new_format->has_border = other_format->has_border;
137+
new_format->has_dxf_border = other_format->has_dxf_border;
138+
new_format->border_count = other_format->border_count;
139+
140+
new_format->bottom = other_format->bottom;
141+
new_format->diag_border = other_format->diag_border;
142+
new_format->diag_type = other_format->diag_type;
143+
new_format->left = other_format->left;
144+
new_format->right = other_format->right;
145+
new_format->top = other_format->top;
146+
new_format->bottom_color = other_format->bottom_color;
147+
new_format->diag_color = other_format->diag_color;
148+
new_format->left_color = other_format->left_color;
149+
new_format->right_color = other_format->right_color;
150+
new_format->top_color = other_format->top_color;
151+
152+
new_format->indent = other_format->indent;
153+
new_format->shrink = other_format->shrink;
154+
new_format->merge_range = other_format->merge_range;
155+
new_format->reading_order = other_format->reading_order;
156+
new_format->just_distrib = other_format->just_distrib;
157+
new_format->color_indexed = other_format->color_indexed;
158+
new_format->font_only = other_format->font_only;
45159
}
46160

47161
void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = ['path' => './tests'];
8+
$excel = new \Vtiful\Kernel\Excel($config);
9+
10+
$textFile = $excel->fileName("tutorial01.xlsx")
11+
->header(['name', 'age']);
12+
13+
$fileHandle = $textFile->getHandle();
14+
15+
$format = new \Vtiful\Kernel\Format($fileHandle);
16+
$colorStyle = $format->fontColor(\Vtiful\Kernel\Format::COLOR_ORANGE)->toResource();
17+
18+
for ($index = 0; $index < 10; $index++) {
19+
$textFile->insertText($index+1, 0, 'vikin');
20+
$textFile->insertText($index+1, 1, 1000, '#,##0', $colorStyle);
21+
}
22+
23+
$filePath = $textFile->output();
24+
25+
var_dump($filePath);
26+
?>
27+
--CLEAN--
28+
<?php
29+
@unlink(__DIR__ . '/tutorial01.xlsx');
30+
?>
31+
--EXPECT--
32+
string(23) "./tests/tutorial01.xlsx"

0 commit comments

Comments
 (0)