Skip to content

Commit d5f632c

Browse files
authored
Merge pull request #220 from viest/dev
Feat: exception in const memory mode, you cannot modify the placed cells
2 parents c4e886c + 991221b commit d5f632c

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

include/xlswriter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
125125
} \
126126
} while(0);
127127

128+
#define WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(xls_resource_write_t, error) \
129+
do { \
130+
if(xls_resource_write_t->worksheet->optimize && error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
131+
zend_throw_exception(vtiful_exception_ce, "In const memory mode, you cannot modify the placed cells", 170); \
132+
return; \
133+
} \
134+
} while(0);
135+
136+
#define WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error) \
137+
do { \
138+
if(error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
139+
zend_throw_exception(vtiful_exception_ce, "Worksheet row or column index out of range", 180); \
140+
return; \
141+
} \
142+
} while(0);
143+
128144
#define FCALL_TWO_ARGS(bucket) \
129145
ZVAL_COPY_VALUE(&args[0], &bucket->val); \
130146
if (bucket->key) { \

kernel/write.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,27 @@ void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_
193193
*/
194194
void auto_filter(zend_string *range, xls_resource_write_t *res)
195195
{
196-
worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));
196+
int error = worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));
197+
198+
// Cells that have been placed cannot be modified using optimization mode
199+
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
200+
201+
// Worksheet row or column index out of range
202+
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
197203
}
198204

199205
/*
200206
* Merge cells.
201207
*/
202208
void merge_cells(zend_string *range, zend_string *value, xls_resource_write_t *res, lxw_format *format)
203209
{
204-
worksheet_merge_range(res->worksheet, RANGE(ZSTR_VAL(range)), ZSTR_VAL(value), format);
210+
int error = worksheet_merge_range(res->worksheet, RANGE(ZSTR_VAL(range)), ZSTR_VAL(value), format);
211+
212+
// Cells that have been placed cannot be modified using optimization mode
213+
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
214+
215+
// Worksheet row or column index out of range
216+
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
205217
}
206218

207219
/*
@@ -222,7 +234,13 @@ void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_f
222234
if (strchr(rows, ':')) {
223235
worksheet_set_rows(ROWS(rows), height, res, format);
224236
} else {
225-
worksheet_set_row(res->worksheet, ROW(rows), height, format);
237+
int error = worksheet_set_row(res->worksheet, ROW(rows), height, format);
238+
239+
// Cells that have been placed cannot be modified using optimization mode
240+
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
241+
242+
// Worksheet row or column index out of range
243+
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
226244
}
227245
}
228246

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
try {
8+
$excel = new \Vtiful\Kernel\Excel([
9+
'path' => './',
10+
]);
11+
12+
$fileObject = $excel->constMemory('test.xlsx');
13+
$fileHandle = $fileObject->getHandle();
14+
15+
$format = new \Vtiful\Kernel\Format($fileHandle);
16+
$boldStyle = $format->bold()->toResource();
17+
18+
$fileObject->header(['name', 'age'])
19+
->data([['viest', 21]])
20+
->mergeCells('A1:C1', 'aaaa')
21+
->output();
22+
} catch (\Vtiful\Kernel\Exception $exception) {
23+
echo $exception->getCode() . PHP_EOL;
24+
echo $exception->getMessage() . PHP_EOL;
25+
}
26+
27+
try {
28+
$excel = new \Vtiful\Kernel\Excel([
29+
'path' => './',
30+
]);
31+
32+
$fileObject = $excel->constMemory('test.xlsx');
33+
$fileHandle = $fileObject->getHandle();
34+
35+
$format = new \Vtiful\Kernel\Format($fileHandle);
36+
$boldStyle = $format->bold()->toResource();
37+
38+
$fileObject->header(['name', 'age'])
39+
->data([['viest', 21]])
40+
->setRow('A1', 200)
41+
->output();
42+
} catch (\Vtiful\Kernel\Exception $exception) {
43+
echo $exception->getCode() . PHP_EOL;
44+
echo $exception->getMessage() . PHP_EOL;
45+
}
46+
47+
try {
48+
$excel = new \Vtiful\Kernel\Excel([
49+
'path' => './',
50+
]);
51+
52+
$fileObject = $excel->constMemory('test.xlsx');
53+
$fileHandle = $fileObject->getHandle();
54+
55+
$format = new \Vtiful\Kernel\Format($fileHandle);
56+
$boldStyle = $format->bold()->toResource();
57+
58+
$fileObject->header(['name', 'age'])
59+
->data([['viest', 21]])
60+
->autoFilter('A1:C1')
61+
->output();
62+
} catch (\Vtiful\Kernel\Exception $exception) {
63+
echo $exception->getCode() . PHP_EOL;
64+
echo $exception->getMessage() . PHP_EOL;
65+
}
66+
?>
67+
--CLEAN--
68+
<?php
69+
@unlink(__DIR__ . '/tutorial.xlsx');
70+
?>
71+
--EXPECT--
72+
170
73+
In const memory mode, you cannot modify the placed cells
74+
170
75+
In const memory mode, you cannot modify the placed cells
76+
170
77+
In const memory mode, you cannot modify the placed cells

0 commit comments

Comments
 (0)