Skip to content

Commit

Permalink
Merge pull request #220 from viest/dev
Browse files Browse the repository at this point in the history
Feat: exception in const memory mode, you cannot modify the placed cells
  • Loading branch information
viest committed Dec 28, 2019
2 parents c4e886c + 991221b commit d5f632c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
16 changes: 16 additions & 0 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
} \
} while(0);

#define WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(xls_resource_write_t, error) \
do { \
if(xls_resource_write_t->worksheet->optimize && error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
zend_throw_exception(vtiful_exception_ce, "In const memory mode, you cannot modify the placed cells", 170); \
return; \
} \
} while(0);

#define WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error) \
do { \
if(error == LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE) { \
zend_throw_exception(vtiful_exception_ce, "Worksheet row or column index out of range", 180); \
return; \
} \
} while(0);

#define FCALL_TWO_ARGS(bucket) \
ZVAL_COPY_VALUE(&args[0], &bucket->val); \
if (bucket->key) { \
Expand Down
24 changes: 21 additions & 3 deletions kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,27 @@ void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_
*/
void auto_filter(zend_string *range, xls_resource_write_t *res)
{
worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));
int error = worksheet_autofilter(res->worksheet, RANGE(ZSTR_VAL(range)));

// Cells that have been placed cannot be modified using optimization mode
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)

// Worksheet row or column index out of range
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
}

/*
* Merge cells.
*/
void merge_cells(zend_string *range, zend_string *value, xls_resource_write_t *res, lxw_format *format)
{
worksheet_merge_range(res->worksheet, RANGE(ZSTR_VAL(range)), ZSTR_VAL(value), format);
int error = worksheet_merge_range(res->worksheet, RANGE(ZSTR_VAL(range)), ZSTR_VAL(value), format);

// Cells that have been placed cannot be modified using optimization mode
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)

// Worksheet row or column index out of range
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
}

/*
Expand All @@ -222,7 +234,13 @@ void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_f
if (strchr(rows, ':')) {
worksheet_set_rows(ROWS(rows), height, res, format);
} else {
worksheet_set_row(res->worksheet, ROW(rows), height, format);
int error = worksheet_set_row(res->worksheet, ROW(rows), height, format);

// Cells that have been placed cannot be modified using optimization mode
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)

// Worksheet row or column index out of range
WORKSHEET_INDEX_OUT_OF_CHANGE_EXCEPTION(error)
}
}

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

$fileObject = $excel->constMemory('test.xlsx');
$fileHandle = $fileObject->getHandle();

$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold()->toResource();

$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->mergeCells('A1:C1', 'aaaa')
->output();
} catch (\Vtiful\Kernel\Exception $exception) {
echo $exception->getCode() . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
}

try {
$excel = new \Vtiful\Kernel\Excel([
'path' => './',
]);

$fileObject = $excel->constMemory('test.xlsx');
$fileHandle = $fileObject->getHandle();

$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold()->toResource();

$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->setRow('A1', 200)
->output();
} catch (\Vtiful\Kernel\Exception $exception) {
echo $exception->getCode() . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
}

try {
$excel = new \Vtiful\Kernel\Excel([
'path' => './',
]);

$fileObject = $excel->constMemory('test.xlsx');
$fileHandle = $fileObject->getHandle();

$format = new \Vtiful\Kernel\Format($fileHandle);
$boldStyle = $format->bold()->toResource();

$fileObject->header(['name', 'age'])
->data([['viest', 21]])
->autoFilter('A1:C1')
->output();
} catch (\Vtiful\Kernel\Exception $exception) {
echo $exception->getCode() . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/tutorial.xlsx');
?>
--EXPECT--
170
In const memory mode, you cannot modify the placed cells
170
In const memory mode, you cannot modify the placed cells
170
In const memory mode, you cannot modify the placed cells

0 comments on commit d5f632c

Please sign in to comment.