Browse Source

Feat(Exception): work book is not initialized

viest 5 years ago
parent
commit
81bc959840
2 changed files with 35 additions and 9 deletions
  1. 8 0
      include/xlswriter.h
  2. 27 9
      kernel/excel.c

+ 8 - 0
include/xlswriter.h

@@ -109,6 +109,14 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
 #define Z_CHART_P(zv)  php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
 #define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
 
+#define WORKBOOK_NOT_INITIALIZED(xls_object_t)                                                                       \
+    do {                                                                                                             \
+        if(obj->write_ptr.workbook == NULL) {                                                                        \
+            zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);   \
+            return;                                                                                                  \
+        }                                                                                                            \
+    } while(0);
+
 #define ROW(range) \
     lxw_name_to_row(range)
 

+ 27 - 9
kernel/excel.c

@@ -243,13 +243,9 @@ PHP_METHOD(vtiful_xls, addSheet)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
     SHEET_LINE_INIT(obj)
 
-    if(obj->write_ptr.workbook == NULL) {
-        zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
-        return;
-    }
-
     if(zs_sheet_name != NULL) {
         sheet_name = ZSTR_VAL(zs_sheet_name);
     }
@@ -274,10 +270,7 @@ PHP_METHOD(vtiful_xls, checkoutSheet)
 
     xls_object *obj = Z_XLS_P(getThis());
 
-    if(obj->write_ptr.workbook == NULL) {
-        zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
-        return;
-    }
+    WORKBOOK_NOT_INITIALIZED(obj);
 
     if ((sheet_t = workbook_get_worksheet_by_name(obj->write_ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
         zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
@@ -347,6 +340,8 @@ PHP_METHOD(vtiful_xls, header)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
          type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, NULL);
          zval_ptr_dtor(header_value);
@@ -368,6 +363,8 @@ PHP_METHOD(vtiful_xls, data)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
         if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
             SHEET_LINE_ADD(obj)
@@ -391,6 +388,8 @@ PHP_METHOD(vtiful_xls, output)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     workbook_file(&obj->write_ptr);
 
     ZVAL_COPY(return_value, file_path);
@@ -428,6 +427,8 @@ PHP_METHOD(vtiful_xls, insertText)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     SHEET_LINE_SET(obj, row);
 
     if (format_handle) {
@@ -459,6 +460,7 @@ PHP_METHOD(vtiful_xls, insertDate)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
     SHEET_LINE_SET(obj, row);
 
     if (Z_TYPE_P(data) != IS_LONG) {
@@ -502,6 +504,8 @@ PHP_METHOD(vtiful_xls, insertChart)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     chart_writer(row, column, zval_get_chart(chart_resource), &obj->write_ptr);
 }
 /* }}} */
@@ -528,6 +532,8 @@ PHP_METHOD(vtiful_xls, insertUrl)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     if (argc == 4) {
         url_writer(row, column, &obj->write_ptr, url, zval_get_format(format_handle));
     }
@@ -559,6 +565,8 @@ PHP_METHOD(vtiful_xls, insertImage)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     image_writer(image, row, column, width, height,  &obj->write_ptr);
 }
 /* }}} */
@@ -580,6 +588,8 @@ PHP_METHOD(vtiful_xls, insertFormula)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     formula_writer(formula, row, column, &obj->write_ptr);
 }
 /* }}} */
@@ -598,6 +608,8 @@ PHP_METHOD(vtiful_xls, autoFilter)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     auto_filter(range, &obj->write_ptr);
 }
 /* }}} */
@@ -617,6 +629,8 @@ PHP_METHOD(vtiful_xls, mergeCells)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     merge_cells(range, data, &obj->write_ptr);
 }
 /* }}} */
@@ -642,6 +656,8 @@ PHP_METHOD(vtiful_xls, setColumn)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     if (argc == 3) {
         set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
     }
@@ -673,6 +689,8 @@ PHP_METHOD(vtiful_xls, setRow)
 
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     if (argc == 3) {
         set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
     }