Explorar o código

Feat: default format

viest %!s(int64=5) %!d(string=hai) anos
pai
achega
60f447b762
Modificáronse 3 ficheiros con 45 adicións e 15 borrados
  1. 6 5
      include/xlswriter.h
  2. 37 8
      kernel/excel.c
  3. 2 2
      kernel/write.c

+ 6 - 5
include/xlswriter.h

@@ -78,10 +78,11 @@ typedef struct {
 } xls_resource_chart_t;
 
 typedef struct _vtiful_xls_object {
-    xls_resource_read_t  read_ptr;
-    xls_resource_write_t write_ptr;
-    zend_long            write_line;
-    zend_object          zo;
+    xls_resource_read_t   read_ptr;
+    xls_resource_write_t  write_ptr;
+    zend_long             write_line;
+    xls_resource_format_t format_ptr;
+    zend_object           zo;
 } xls_object;
 
 typedef struct _vtiful_format_object {
@@ -179,11 +180,11 @@ void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
 void merge_cells(zend_string *range, zend_string *value, xls_resource_write_t *res);
 void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
 void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);
-void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res);
 void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *url, lxw_format *format);
 void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res);
 void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format);
 void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res);
+void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format);
 void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle);
 
 lxw_error workbook_file(xls_resource_write_t *self);

+ 37 - 8
kernel/excel.c

@@ -35,8 +35,9 @@ PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
 
     intern->zo.handlers = &vtiful_xls_handlers;
 
-    intern->read_ptr.file_t = NULL;
-    intern->read_ptr.sheet_t = NULL;
+    intern->read_ptr.file_t   = NULL;
+    intern->read_ptr.sheet_t  = NULL;
+    intern->format_ptr.format = NULL;
 
     return &intern->zo;
 }
@@ -55,6 +56,10 @@ static void vtiful_xls_objects_free(zend_object *object)
     xlsxioread_close(intern->read_ptr.file_t);
 #endif
 
+    if (intern->format_ptr.format != NULL) {
+        intern->format_ptr.format = NULL;
+    }
+
     zend_object_std_dtor(&intern->zo);
 }
 /* }}} */
@@ -155,6 +160,10 @@ ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 3)
                 ZEND_ARG_INFO(0, height)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(xls_set_global_format, 0, 0, 1)
+                ZEND_ARG_INFO(0, format_handle)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(xls_open_file_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, zs_file_name)
 ZEND_END_ARG_INFO()
@@ -377,7 +386,7 @@ PHP_METHOD(vtiful_xls, header)
     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);
+         type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, obj->format_ptr.format);
          zval_ptr_dtor(header_value);
     ZEND_HASH_FOREACH_END();
 
@@ -404,7 +413,7 @@ PHP_METHOD(vtiful_xls, data)
     ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
         if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
             ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
-                type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->write_ptr, NULL, NULL);
+                type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->write_ptr, NULL, obj->format_ptr.format);
                 zval_ptr_dtor(&bucket->val);
             ZEND_HASH_FOREACH_END();
 
@@ -438,6 +447,8 @@ PHP_METHOD(vtiful_xls, getHandle)
 {
     xls_object *obj = Z_XLS_P(getThis());
 
+    WORKBOOK_NOT_INITIALIZED(obj);
+
     RETURN_RES(zend_register_resource(&obj->write_ptr, le_xls_writer));
 }
 /* }}} */
@@ -470,7 +481,7 @@ PHP_METHOD(vtiful_xls, insertText)
     if (format_handle) {
         type_writer(data, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
     } else {
-        type_writer(data, row, column, &obj->write_ptr, format, NULL);
+        type_writer(data, row, column, &obj->write_ptr, format, obj->format_ptr.format);
     }
 }
 /* }}} */
@@ -515,7 +526,7 @@ PHP_METHOD(vtiful_xls, insertDate)
     if (format_handle) {
         type_writer(&_zv_double_time, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
     } else {
-        type_writer(&_zv_double_time, row, column, &obj->write_ptr, format, NULL);
+        type_writer(&_zv_double_time, row, column, &obj->write_ptr, format, obj->format_ptr.format);
     }
 
     zend_string_release(format);
@@ -575,7 +586,7 @@ PHP_METHOD(vtiful_xls, insertUrl)
     }
 
     if (argc == 3) {
-        url_writer(row, column, &obj->write_ptr, url, NULL);
+        url_writer(row, column, &obj->write_ptr, url, obj->format_ptr.format);
     }
 }
 /* }}} */
@@ -626,7 +637,7 @@ PHP_METHOD(vtiful_xls, insertFormula)
 
     WORKBOOK_NOT_INITIALIZED(obj);
 
-    formula_writer(formula, row, column, &obj->write_ptr);
+    formula_writer(formula, row, column, &obj->write_ptr, obj->format_ptr.format);
 }
 /* }}} */
 
@@ -737,6 +748,23 @@ PHP_METHOD(vtiful_xls, setRow)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Excel::defaultFormat(resource $format)
+ */
+PHP_METHOD(vtiful_xls, defaultFormat)
+{
+    zval *format_handle = NULL;
+
+    ZEND_PARSE_PARAMETERS_START(1, 1)
+            Z_PARAM_RESOURCE(format_handle)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object *obj = Z_XLS_P(getThis());
+    obj->format_ptr.format = zval_get_format(format_handle);
+}
+/* }}} */
+
 /** {{{ \Vtiful\Kernel\Excel::freezePanes(int $row, int $column)
  */
 PHP_METHOD(vtiful_xls, freezePanes)
@@ -1056,6 +1084,7 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, mergeCells,    xls_merge_cells_arginfo,    ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, setColumn,     xls_set_column_arginfo,     ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, setRow,        xls_set_row_arginfo,        ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format,      ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, freezePanes,   xls_freeze_panes_arginfo,   ZEND_ACC_PUBLIC)
 
         PHP_ME(vtiful_xls, zoom,          xls_sheet_zoom_arginfo,     ZEND_ACC_PUBLIC)

+ 2 - 2
kernel/write.c

@@ -176,9 +176,9 @@ void image_writer(zval *value, zend_long row, zend_long columns, double width, d
 /*
  * Write the image to the file
  */
-void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res)
+void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format)
 {
-    worksheet_write_formula(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(value), NULL);
+    worksheet_write_formula(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(value), format);
 }
 
 void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res)