Skip to content

Commit a8bd153

Browse files
authored
Merge pull request #122 from viest/dev
wrap, background format and checkout sheet
2 parents 456c516 + f617d07 commit a8bd153

File tree

7 files changed

+240
-14
lines changed

7 files changed

+240
-14
lines changed

include/xlswriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
9898
#define SHEET_LINE_ADD(obj_p) \
9999
++obj_p->line;
100100

101+
#define SHEET_LINE_SET(obj_p, line) \
102+
obj_p->line = line;
103+
101104
#define SHEET_CURRENT_LINE(obj_p) obj_p->line
102105

103106
lxw_format * zval_get_format(zval *handle);

kernel/excel.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
7171
ZEND_ARG_INFO(0, sheet_name)
7272
ZEND_END_ARG_INFO()
7373

74+
ZEND_BEGIN_ARG_INFO_EX(xls_file_checkout_sheet, 0, 0, 1)
75+
ZEND_ARG_INFO(0, sheet_name)
76+
ZEND_END_ARG_INFO()
77+
7478
ZEND_BEGIN_ARG_INFO_EX(xls_header_arginfo, 0, 0, 1)
7579
ZEND_ARG_INFO(0, header)
7680
ZEND_END_ARG_INFO()
@@ -229,6 +233,40 @@ PHP_METHOD(vtiful_xls, addSheet)
229233
}
230234
/* }}} */
231235

236+
/** {{{ \Vtiful\Kernel\xls::checkoutSheet(string $sheetName)
237+
*/
238+
PHP_METHOD(vtiful_xls, checkoutSheet)
239+
{
240+
int line = 0;
241+
lxw_worksheet *sheet_t = NULL;
242+
zend_string *zs_sheet_name = NULL;
243+
244+
ZEND_PARSE_PARAMETERS_START(1, 1)
245+
Z_PARAM_STR(zs_sheet_name)
246+
ZEND_PARSE_PARAMETERS_END();
247+
248+
ZVAL_COPY(return_value, getThis());
249+
250+
xls_object *obj = Z_XLS_P(getThis());
251+
252+
if(obj->ptr.workbook == NULL) {
253+
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
254+
return;
255+
}
256+
257+
if ((sheet_t = workbook_get_worksheet_by_name(obj->ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
258+
zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
259+
return;
260+
}
261+
262+
line = sheet_t->table->cached_row_num;
263+
264+
SHEET_LINE_SET(obj, line);
265+
266+
obj->ptr.worksheet = sheet_t;
267+
}
268+
/* }}} */
269+
232270
/** {{{ \Vtiful\Kernel\xls::constMemory(string $fileName [, string $sheetName])
233271
*/
234272
PHP_METHOD(vtiful_xls, constMemory)
@@ -575,6 +613,7 @@ zend_function_entry xls_methods[] = {
575613
PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC)
576614
PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
577615
PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
616+
PHP_ME(vtiful_xls, checkoutSheet, xls_file_checkout_sheet, ZEND_ACC_PUBLIC)
578617
PHP_ME(vtiful_xls, constMemory, xls_const_memory_arginfo, ZEND_ACC_PUBLIC)
579618
PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
580619
PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)

kernel/format.c

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ ZEND_END_ARG_INFO()
7474
ZEND_BEGIN_ARG_INFO_EX(format_number_arginfo, 0, 0, 1)
7575
ZEND_ARG_INFO(0, format)
7676
ZEND_END_ARG_INFO()
77+
78+
ZEND_BEGIN_ARG_INFO_EX(format_background_arginfo, 0, 0, 2)
79+
ZEND_ARG_INFO(0, pattern)
80+
ZEND_ARG_INFO(0, color)
81+
ZEND_END_ARG_INFO()
7782
/* }}} */
7883

7984
/** {{{ \Vtiful\Kernel\Format::__construct()
@@ -106,7 +111,10 @@ PHP_METHOD(vtiful_format, bold)
106111
ZVAL_COPY(return_value, getThis());
107112

108113
format_object *obj = Z_FORMAT_P(getThis());
109-
format_set_bold(obj->ptr.format);
114+
115+
if (obj->ptr.format) {
116+
format_set_bold(obj->ptr.format);
117+
}
110118
}
111119
/* }}} */
112120

@@ -117,7 +125,10 @@ PHP_METHOD(vtiful_format, italic)
117125
ZVAL_COPY(return_value, getThis());
118126

119127
format_object *obj = Z_FORMAT_P(getThis());
120-
format_set_italic(obj->ptr.format);
128+
129+
if (obj->ptr.format) {
130+
format_set_italic(obj->ptr.format);
131+
}
121132
}
122133
/* }}} */
123134

@@ -134,7 +145,10 @@ PHP_METHOD(vtiful_format, underline)
134145
ZVAL_COPY(return_value, getThis());
135146

136147
format_object *obj = Z_FORMAT_P(getThis());
137-
format_set_underline(obj->ptr.format, style);
148+
149+
if (obj->ptr.format) {
150+
format_set_underline(obj->ptr.format, style);
151+
}
138152
}
139153
/* }}} */
140154

@@ -160,7 +174,9 @@ PHP_METHOD(vtiful_format, align)
160174
zend_throw_exception(vtiful_exception_ce, "Format exception, please view the manual", 150);
161175
}
162176

163-
format_set_align(obj->ptr.format, Z_LVAL_P(arg));
177+
if (obj->ptr.format) {
178+
format_set_align(obj->ptr.format, Z_LVAL_P(arg));
179+
}
164180
}
165181
}
166182
/* }}} */
@@ -179,7 +195,9 @@ PHP_METHOD(vtiful_format, color)
179195

180196
format_object *obj = Z_FORMAT_P(getThis());
181197

182-
format_set_font_color(obj->ptr.format, color);
198+
if (obj->ptr.format) {
199+
format_set_font_color(obj->ptr.format, color);
200+
}
183201
}
184202
/* }}} */
185203

@@ -197,7 +215,45 @@ PHP_METHOD(vtiful_format, number)
197215

198216
format_object *obj = Z_FORMAT_P(getThis());
199217

200-
format_set_num_format(obj->ptr.format, ZSTR_VAL(format));
218+
if (obj->ptr.format) {
219+
format_set_num_format(obj->ptr.format, ZSTR_VAL(format));
220+
}
221+
}
222+
/* }}} */
223+
224+
/** {{{ \Vtiful\Kernel\Format::background(int $pattern, int $color)
225+
*/
226+
PHP_METHOD(vtiful_format, background)
227+
{
228+
zend_long pattern, color;
229+
230+
ZEND_PARSE_PARAMETERS_START(2, 2)
231+
Z_PARAM_LONG(pattern)
232+
Z_PARAM_LONG(color)
233+
ZEND_PARSE_PARAMETERS_END();
234+
235+
ZVAL_COPY(return_value, getThis());
236+
237+
format_object *obj = Z_FORMAT_P(getThis());
238+
239+
if (obj->ptr.format) {
240+
format_set_pattern(obj->ptr.format, pattern);
241+
format_set_bg_color(obj->ptr.format, color);
242+
}
243+
}
244+
/* }}} */
245+
246+
/** {{{ \Vtiful\Kernel\Format::wrap()
247+
*/
248+
PHP_METHOD(vtiful_format, wrap)
249+
{
250+
ZVAL_COPY(return_value, getThis());
251+
252+
format_object *obj = Z_FORMAT_P(getThis());
253+
254+
if (obj->ptr.format) {
255+
format_set_text_wrap(obj->ptr.format);
256+
}
201257
}
202258
/* }}} */
203259

@@ -215,14 +271,16 @@ PHP_METHOD(vtiful_format, toResource)
215271
/** {{{ format_methods
216272
*/
217273
zend_function_entry format_methods[] = {
218-
PHP_ME(vtiful_format, __construct, format_construct_arginfo, ZEND_ACC_PUBLIC)
219-
PHP_ME(vtiful_format, bold, NULL, ZEND_ACC_PUBLIC)
220-
PHP_ME(vtiful_format, italic, NULL, ZEND_ACC_PUBLIC)
221-
PHP_ME(vtiful_format, underline, format_underline_arginfo, ZEND_ACC_PUBLIC)
222-
PHP_ME(vtiful_format, align, format_align_arginfo, ZEND_ACC_PUBLIC)
223-
PHP_ME(vtiful_format, color, format_color_arginfo, ZEND_ACC_PUBLIC)
224-
PHP_ME(vtiful_format, number, format_number_arginfo, ZEND_ACC_PUBLIC)
225-
PHP_ME(vtiful_format, toResource, NULL, ZEND_ACC_PUBLIC)
274+
PHP_ME(vtiful_format, __construct, format_construct_arginfo, ZEND_ACC_PUBLIC)
275+
PHP_ME(vtiful_format, wrap, NULL, ZEND_ACC_PUBLIC)
276+
PHP_ME(vtiful_format, bold, NULL, ZEND_ACC_PUBLIC)
277+
PHP_ME(vtiful_format, italic, NULL, ZEND_ACC_PUBLIC)
278+
PHP_ME(vtiful_format, align, format_align_arginfo, ZEND_ACC_PUBLIC)
279+
PHP_ME(vtiful_format, color, format_color_arginfo, ZEND_ACC_PUBLIC)
280+
PHP_ME(vtiful_format, number, format_number_arginfo, ZEND_ACC_PUBLIC)
281+
PHP_ME(vtiful_format, underline, format_underline_arginfo, ZEND_ACC_PUBLIC)
282+
PHP_ME(vtiful_format, toResource, NULL, ZEND_ACC_PUBLIC)
283+
PHP_ME(vtiful_format, background, format_background_arginfo, ZEND_ACC_PUBLIC)
226284
PHP_FE_END
227285
};
228286
/* }}} */
@@ -275,6 +333,26 @@ VTIFUL_STARTUP_FUNCTION(format) {
275333
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "COLOR_WHITE", LXW_COLOR_WHITE)
276334
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "COLOR_YELLOW", LXW_COLOR_YELLOW)
277335

336+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_NONE", LXW_PATTERN_NONE)
337+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_SOLID", LXW_PATTERN_SOLID)
338+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_MEDIUM_GRAY", LXW_PATTERN_MEDIUM_GRAY)
339+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_GRAY", LXW_PATTERN_DARK_GRAY)
340+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_GRAY", LXW_PATTERN_LIGHT_GRAY)
341+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_HORIZONTAL", LXW_PATTERN_DARK_HORIZONTAL)
342+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_VERTICAL", LXW_PATTERN_DARK_VERTICAL)
343+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_DOWN", LXW_PATTERN_DARK_DOWN)
344+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_UP", LXW_PATTERN_DARK_UP)
345+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_GRID", LXW_PATTERN_DARK_GRID)
346+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_DARK_TRELLIS", LXW_PATTERN_DARK_TRELLIS)
347+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_HORIZONTAL", LXW_PATTERN_LIGHT_HORIZONTAL)
348+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_VERTICAL", LXW_PATTERN_LIGHT_VERTICAL)
349+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_DOWN", LXW_PATTERN_LIGHT_DOWN)
350+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_UP", LXW_PATTERN_LIGHT_UP)
351+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_GRID", LXW_PATTERN_LIGHT_GRID)
352+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_LIGHT_TRELLIS", LXW_PATTERN_LIGHT_TRELLIS)
353+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_GRAY_125", LXW_PATTERN_GRAY_125)
354+
REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "PATTERN_GRAY_0625", LXW_PATTERN_GRAY_0625)
355+
278356
return SUCCESS;
279357
}
280358
/* }}} */

tests/format_background.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
$fileHandle = $fileObject->getHandle();
15+
16+
$format = new \Vtiful\Kernel\Format($fileHandle);
17+
$style = $format->background(
18+
\Vtiful\Kernel\Format::PATTERN_LIGHT_UP,
19+
\Vtiful\Kernel\Format::COLOR_RED
20+
)->toResource();
21+
22+
$filePath = $fileObject->header(['name', 'age'])
23+
->data([
24+
['viest', 21],
25+
['wjx', 21]
26+
])
27+
->setRow('A1', 50, $style)
28+
->output();
29+
30+
var_dump($filePath);
31+
?>
32+
--CLEAN--
33+
<?php
34+
@unlink(__DIR__ . '/tutorial.xlsx');
35+
?>
36+
--EXPECT--
37+
string(21) "./tests/tutorial.xlsx"

tests/format_wrap.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
13+
$fileObject = $fileObject->fileName('tutorial.xlsx');
14+
$fileHandle = $fileObject->getHandle();
15+
16+
$format = new \Vtiful\Kernel\Format($fileHandle);
17+
$wrapStyle = $format->wrap()->toResource();
18+
19+
$filePath = $fileObject->header(['name', 'age'])
20+
->data([
21+
["vvvvvvvvvvvvvvvvvvvvvvvvvv\nvvvvvvvvvvvvvvvvvvvvvvvvvvv", 21],
22+
['wjx', 21]
23+
])
24+
->setRow('A2', 50, $wrapStyle)
25+
->output();
26+
27+
var_dump($filePath);
28+
?>
29+
--CLEAN--
30+
<?php
31+
@unlink(__DIR__ . '/tutorial.xlsx');
32+
?>
33+
--EXPECT--
34+
string(21) "./tests/tutorial.xlsx"
File renamed without changes.

tests/sheet_checkout.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
$fileObject = $excel->fileName("tutorial01.xlsx");
11+
12+
$fileObject->header(['name', 'age'])
13+
->data([
14+
['viest', 21],
15+
['viest', 22],
16+
['viest', 23],
17+
]);
18+
19+
$fileObject->addSheet('twoSheet')
20+
->header(['name', 'age'])
21+
->data([['vikin', 22]]);
22+
23+
$fileObject->checkoutSheet('Sheet1')
24+
->data([['sheet1']]);
25+
26+
$filePath = $fileObject->output();
27+
28+
var_dump($filePath);
29+
?>
30+
--CLEAN--
31+
<?php
32+
@unlink(__DIR__ . '/tutorial01.xlsx');
33+
?>
34+
--EXPECT--
35+
string(23) "./tests/tutorial01.xlsx"

0 commit comments

Comments
 (0)