diff --git a/include/xlswriter.h b/include/xlswriter.h index eefea59..60f46ad 100644 --- a/include/xlswriter.h +++ b/include/xlswriter.h @@ -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) { \ diff --git a/kernel/write.c b/kernel/write.c index c3d785a..0467c61 100644 --- a/kernel/write.c +++ b/kernel/write.c @@ -193,7 +193,13 @@ 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) } /* @@ -201,7 +207,13 @@ void auto_filter(zend_string *range, xls_resource_write_t *res) */ 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) } /* @@ -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) } } diff --git a/tests/const_memory_index_out_range.phpt b/tests/const_memory_index_out_range.phpt new file mode 100644 index 0000000..ea116ec --- /dev/null +++ b/tests/const_memory_index_out_range.phpt @@ -0,0 +1,77 @@ +--TEST-- +Check for vtiful presence +--SKIPIF-- + +--FILE-- + './', + ]); + + $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-- + +--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 \ No newline at end of file