Переглянути джерело

Feat: exception in const memory mode, you cannot modify the placed cells

viest 5 роки тому
батько
коміт
cb2a80f2e2
3 змінених файлів з 111 додано та 7 видалено
  1. 16 0
      include/xlswriter.h
  2. 18 7
      kernel/write.c
  3. 77 0
      tests/const_memory_index_out_range.phpt

+ 16 - 0
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) {                       \

+ 18 - 7
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)
 }
 
 /*
@@ -204,11 +210,10 @@ void merge_cells(zend_string *range, zend_string *value, xls_resource_write_t *r
     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
-    if(res->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;
-    }
+    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)
 }
 
 /*
@@ -229,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)
     }
 }
 

+ 77 - 0
tests/const_memory_index_out_range.phpt

@@ -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