Przeglądaj źródła

Feat(insertText): support resource format

viest 6 lat temu
rodzic
commit
71b0b5e9fc
4 zmienionych plików z 183 dodań i 30 usunięć
  1. 2 1
      include/xlswriter.h
  2. 12 6
      kernel/excel.c
  3. 137 23
      kernel/write.c
  4. 32 0
      tests/insert_text_resource_format.phpt

+ 2 - 1
include/xlswriter.h

@@ -116,7 +116,8 @@ STATIC int  _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b);
 STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
 STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);
 
-void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format);
+void format_copy(lxw_format *new_format, lxw_format *other_format);
+void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format, lxw_format *format_handle);
 void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_t *res);
 void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format);
 void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_t *res);

+ 12 - 6
kernel/excel.c

@@ -323,7 +323,7 @@ PHP_METHOD(vtiful_xls, header)
     xls_object *obj = Z_XLS_P(getThis());
 
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
-         type_writer(header_value, 0, header_l_key, &obj->ptr, NULL);
+         type_writer(header_value, 0, header_l_key, &obj->ptr, NULL, NULL);
          zval_ptr_dtor(header_value);
     ZEND_HASH_FOREACH_END();
 }
@@ -348,7 +348,7 @@ PHP_METHOD(vtiful_xls, data)
             SHEET_LINE_ADD(obj)
 
             ZEND_HASH_FOREACH_BUCKET(Z_ARRVAL_P(data_r_value), Bucket *bucket)
-                type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->ptr, NULL);
+                type_writer(&bucket->val, SHEET_CURRENT_LINE(obj), bucket->h, &obj->ptr, NULL, NULL);
                 zval_ptr_dtor(&bucket->val);
             ZEND_HASH_FOREACH_END();
         }
@@ -385,20 +385,21 @@ PHP_METHOD(vtiful_xls, getHandle)
 }
 /* }}} */
 
-/** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data)
+/** {{{ \Vtiful\Kernel\xls::insertText(int $row, int $column, string|int|double $data[, string $format, resource $formatHandle])
  */
 PHP_METHOD(vtiful_xls, insertText)
 {
-    zval *data;
+    zval *data, *format_handle = NULL;
     zend_long row, column;
     zend_string *format = NULL;
 
-    ZEND_PARSE_PARAMETERS_START(3, 4)
+    ZEND_PARSE_PARAMETERS_START(3, 5)
             Z_PARAM_LONG(row)
             Z_PARAM_LONG(column)
             Z_PARAM_ZVAL(data)
             Z_PARAM_OPTIONAL
             Z_PARAM_STR(format)
+            Z_PARAM_RESOURCE(format_handle)
     ZEND_PARSE_PARAMETERS_END();
 
     ZVAL_COPY(return_value, getThis());
@@ -407,7 +408,12 @@ PHP_METHOD(vtiful_xls, insertText)
 
     SHEET_LINE_SET(obj, row);
 
-    type_writer(data, row, column, &obj->ptr, format);
+    if (format_handle) {
+        type_writer(data, row, column, &obj->ptr, format, zval_get_format(format_handle));
+    } else {
+        type_writer(data, row, column, &obj->ptr, format, NULL);
+    }
+
 }
 /* }}} */
 

+ 137 - 23
kernel/write.c

@@ -15,33 +15,147 @@
 /*
  * According to the zval type written to the file
  */
-void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format)
+void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format, lxw_format *format_handle)
 {
     lxw_format *value_format = NULL;
 
-    switch (Z_TYPE_P(value)) {
-        case IS_STRING:
-            worksheet_write_string(res->worksheet, row, columns, ZSTR_VAL(zval_get_string(value)), NULL);
-            break;
-        case IS_LONG:
-            if(format) {
-                value_format = workbook_add_format(res->workbook);
-                format_set_num_format(value_format, ZSTR_VAL(format));
-                worksheet_write_number(res->worksheet, row, columns, zval_get_long(value), value_format);
-            } else {
-                worksheet_write_number(res->worksheet, row, columns, zval_get_long(value), NULL);
-            }
-            break;
-        case IS_DOUBLE:
-            if(format) {
-                value_format = workbook_add_format(res->workbook);
-                format_set_num_format(value_format, ZSTR_VAL(format));
-                worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), value_format);
-            } else {
-                worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), NULL);
-            }
-            break;
+    lxw_col_t lxw_col = (lxw_col_t)columns;
+    lxw_row_t lxw_row = (lxw_row_t)row;
+
+    zend_uchar value_type = Z_TYPE_P(value);
+
+    if (value_type == IS_STRING) {
+        worksheet_write_string(res->worksheet, lxw_row, lxw_col, ZSTR_VAL(zval_get_string(value)), format_handle);
+        return;
+    }
+
+    if (value_type == IS_LONG) {
+        if (format != NULL && format_handle == NULL) {
+            value_format = workbook_add_format(res->workbook);
+
+            format_set_num_format(value_format, ZSTR_VAL(format));
+
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), value_format);
+            return;
+        }
+
+        if (format == NULL && format_handle != NULL) {
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), format_handle);
+            return;
+        }
+
+        if(format != NULL && format_handle != NULL) {
+            value_format = workbook_add_format(res->workbook);
+
+            format_copy(value_format, format_handle);
+            format_set_num_format(value_format, ZSTR_VAL(format));
+
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), value_format);
+            return;
+        }
+
+        worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_long(value), NULL);
     }
+
+    if (value_type == IS_DOUBLE) {
+        if (format != NULL && format_handle == NULL) {
+            value_format = workbook_add_format(res->workbook);
+
+            format_set_num_format(value_format, ZSTR_VAL(format));
+
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format);
+            return;
+        }
+
+        if (format == NULL && format_handle != NULL) {
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), format_handle);
+            return;
+        }
+
+        if(format != NULL && format_handle != NULL) {
+            value_format = workbook_add_format(res->workbook);
+
+            format_copy(value_format, format_handle);
+            format_set_num_format(value_format, ZSTR_VAL(format));
+
+            worksheet_write_number(res->worksheet, lxw_row, lxw_col, zval_get_double(value), value_format);
+            return;
+        }
+
+        worksheet_write_number(res->worksheet, row, columns, zval_get_double(value), NULL);
+        return;
+    }
+}
+
+void format_copy(lxw_format *new_format, lxw_format *other_format)
+{
+    new_format->bold = other_format->bold;
+    new_format->bg_color = other_format->bg_color;
+    new_format->border_count = other_format->border_count;
+    new_format->border_index = other_format->border_index;
+    new_format->bottom = other_format->bottom;
+    new_format->bottom_color = other_format->bottom_color;
+    new_format->color_indexed = other_format->color_indexed;
+    new_format->diag_border = other_format->diag_border;
+    new_format->diag_color = other_format->diag_color;
+
+    new_format->font_size = other_format->font_size;
+    new_format->bold = other_format->bold;
+    new_format->italic = other_format->italic;
+    new_format->font_color = other_format->font_color;
+    new_format->underline = other_format->underline;
+    new_format->font_strikeout = other_format->font_strikeout;
+    new_format->font_outline = other_format->font_outline;
+    new_format->font_shadow = other_format->font_shadow;
+    new_format->font_script = other_format->font_script;
+    new_format->font_family = other_format->font_family;
+    new_format->font_charset = other_format->font_charset;
+    new_format->font_condense = other_format->font_condense;
+    new_format->font_extend = other_format->font_extend;
+    new_format->theme = other_format->theme;
+    new_format->hyperlink = other_format->hyperlink;
+
+    new_format->hidden = other_format->hidden;
+    new_format->locked = other_format->locked;
+
+    new_format->text_h_align = other_format->text_h_align;
+    new_format->text_wrap = other_format->text_wrap;
+    new_format->text_v_align = other_format->text_v_align;
+    new_format->text_justlast = other_format->text_justlast;
+    new_format->rotation = other_format->rotation;
+
+    new_format->fg_color = other_format->fg_color;
+    new_format->bg_color = other_format->bg_color;
+    new_format->pattern = other_format->pattern;
+    new_format->has_fill = other_format->has_fill;
+    new_format->has_dxf_fill = other_format->has_dxf_fill;
+    new_format->fill_index = other_format->fill_index;
+    new_format->fill_count = other_format->fill_count;
+
+    new_format->border_index = other_format->border_index;
+    new_format->has_border = other_format->has_border;
+    new_format->has_dxf_border = other_format->has_dxf_border;
+    new_format->border_count = other_format->border_count;
+
+    new_format->bottom = other_format->bottom;
+    new_format->diag_border = other_format->diag_border;
+    new_format->diag_type = other_format->diag_type;
+    new_format->left = other_format->left;
+    new_format->right = other_format->right;
+    new_format->top = other_format->top;
+    new_format->bottom_color = other_format->bottom_color;
+    new_format->diag_color = other_format->diag_color;
+    new_format->left_color = other_format->left_color;
+    new_format->right_color = other_format->right_color;
+    new_format->top_color = other_format->top_color;
+
+    new_format->indent = other_format->indent;
+    new_format->shrink = other_format->shrink;
+    new_format->merge_range = other_format->merge_range;
+    new_format->reading_order = other_format->reading_order;
+    new_format->just_distrib = other_format->just_distrib;
+    new_format->color_indexed = other_format->color_indexed;
+    new_format->font_only = other_format->font_only;
 }
 
 void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format)

+ 32 - 0
tests/insert_text_resource_format.phpt

@@ -0,0 +1,32 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = ['path' => './tests'];
+$excel = new \Vtiful\Kernel\Excel($config);
+
+$textFile = $excel->fileName("tutorial01.xlsx")
+    ->header(['name', 'age']);
+
+$fileHandle = $textFile->getHandle();
+
+$format     = new \Vtiful\Kernel\Format($fileHandle);
+$colorStyle = $format->fontColor(\Vtiful\Kernel\Format::COLOR_ORANGE)->toResource();
+
+for ($index = 0; $index < 10; $index++) {
+    $textFile->insertText($index+1, 0, 'vikin');
+    $textFile->insertText($index+1, 1, 1000, '#,##0', $colorStyle);
+}
+
+$filePath = $textFile->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial01.xlsx');
+?>
+--EXPECT--
+string(23) "./tests/tutorial01.xlsx"