Przeglądaj źródła

FEAT(set_column): set column format

viest 7 lat temu
rodzic
commit
eeb9fd0485
6 zmienionych plików z 70 dodań i 11 usunięć
  1. 13 0
      kernel/common/resource.c
  2. 42 6
      kernel/excel.c
  3. 5 2
      kernel/format.c
  4. 0 3
      kernel/format.h
  5. 5 0
      kernel/include.h
  6. 5 0
      kernel/write.c

+ 13 - 0
kernel/common/resource.c

@@ -11,4 +11,17 @@ excel_resource_t * zval_get_resource(zval *handle)
 
     return res;
 }
+/* }}} */
+
+/* {{{ */
+lxw_format * zval_get_format(zval *handle)
+{
+    lxw_format *res;
+
+    if((res = (lxw_format *)zend_fetch_resource(Z_RES_P(handle), VTIFUL_RESOURCE_NAME, le_excel_writer)) == NULL) {
+        zend_throw_exception(vtiful_exception_ce, "Excel resources resolution fail", 210);
+    }
+
+    return res;
+}
 /* }}} */

+ 42 - 6
kernel/excel.c

@@ -63,6 +63,12 @@ ZEND_BEGIN_ARG_INFO_EX(excel_merge_cells_arginfo, 0, 0, 2)
                 ZEND_ARG_INFO(0, range)
                 ZEND_ARG_INFO(0, data)
 ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(excel_set_column_arginfo, 0, 0, 3)
+                ZEND_ARG_INFO(0, format_handle)
+                ZEND_ARG_INFO(0, range)
+                ZEND_ARG_INFO(0, width)
+ZEND_END_ARG_INFO()
 /* }}} */
 
 /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
@@ -334,21 +340,51 @@ PHP_METHOD(vtiful_excel, mergeCells)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Excel::setColumn(resource $format, string $range [, int $width])
+ */
+PHP_METHOD(vtiful_excel, setColumn)
+{
+    zval *format_handle, res_handle;
+    zend_string *range;
+
+    double width = 0;
+    int    argc  = ZEND_NUM_ARGS();
+
+    ZEND_PARSE_PARAMETERS_START(2, 3)
+            Z_PARAM_RESOURCE(format_handle)
+            Z_PARAM_STR(range)
+            Z_PARAM_OPTIONAL
+            Z_PARAM_DOUBLE(width)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    if (argc == 2) {
+        width = 10;
+    }
+
+    set_column(range, width, excel_res, zval_get_format(format_handle));
+
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
+    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
+}
+/* }}} */
 
 /** {{{ excel_methods
 */
 zend_function_entry excel_methods[] = {
-        PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-        PHP_ME(vtiful_excel, fileName,    excel_file_name_arginfo, ZEND_ACC_PUBLIC)
-        PHP_ME(vtiful_excel, header,      excel_header_arginfo,    ZEND_ACC_PUBLIC)
-        PHP_ME(vtiful_excel, data,        excel_data_arginfo,      ZEND_ACC_PUBLIC)
-        PHP_ME(vtiful_excel, output,      NULL,                    ZEND_ACC_PUBLIC)
-        PHP_ME(vtiful_excel, getHandle,   NULL,                    ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, __construct,   excel_construct_arginfo,      ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+        PHP_ME(vtiful_excel, fileName,      excel_file_name_arginfo,      ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, header,        excel_header_arginfo,         ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, data,          excel_data_arginfo,           ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, output,        NULL,                         ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, getHandle,     NULL,                         ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, autoFilter,    excel_auto_filter_arginfo,    ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, insertText,    excel_insert_text_arginfo,    ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, insertImage,   excel_insert_image_arginfo,   ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, insertFormula, excel_insert_formula_arginfo, ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, mergeCells,    excel_merge_cells_arginfo,    ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, setColumn,     excel_set_column_arginfo,     ZEND_ACC_PUBLIC)
         PHP_FE_END
 };
 /* }}} */

+ 5 - 2
kernel/format.c

@@ -113,10 +113,13 @@ VTIFUL_STARTUP_FUNCTION(format) {
 
     INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", format_methods);
 
-//    zend_declare_class_constant_long(&ce, "UNDERLINE_SINGLE", sizeof("UNDERLINE_SINGLE")-1, (zend_long)LXW_UNDERLINE_SINGLE);
-
     vtiful_format_ce = zend_register_internal_class(&ce);
 
+    REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_SINGLE", LXW_UNDERLINE_SINGLE)
+    REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_DOUBLE ", LXW_UNDERLINE_DOUBLE)
+    REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_SINGLE_ACCOUNTING", LXW_UNDERLINE_SINGLE_ACCOUNTING)
+    REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_DOUBLE_ACCOUNTING", LXW_UNDERLINE_DOUBLE_ACCOUNTING)
+
     return SUCCESS;
 }
 /* }}} */

+ 0 - 3
kernel/format.h

@@ -13,9 +13,6 @@
 #ifndef PHP_EXT_EXCEL_EXPORT_FORMAT_H
 #define PHP_EXT_EXCEL_EXPORT_FORMAT_H
 
-#include "php_excel_writer.h"
-#include "xlsxwriter.h"
-
 extern zend_class_entry *vtiful_format_ce;
 
 VTIFUL_STARTUP_FUNCTION(format);

+ 5 - 0
kernel/include.h

@@ -17,6 +17,9 @@
 #include "exception.h"
 #include "format.h"
 
+#define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
+    zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
+
 typedef struct {
     lxw_workbook *workbook;
     lxw_worksheet *worksheet;
@@ -26,6 +29,7 @@ extern excel_resource_t *excel_res;
 
 
 excel_resource_t * zval_get_resource(zval *handle);
+lxw_format       * zval_get_format(zval *handle);
 
 STATIC lxw_error _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden);
 
@@ -41,6 +45,7 @@ void image_writer(zval *value, zend_long row, zend_long columns, excel_resource_
 void formula_writer(zval *value, zend_long row, zend_long columns, excel_resource_t *res);
 void auto_filter(zend_string *range, excel_resource_t *res);
 void merge_cells(zend_string *range, zend_string *value, excel_resource_t *res);
+void set_column(zend_string *range, double width, excel_resource_t *res, lxw_format *format);
 lxw_error workbook_file(excel_resource_t *self, zval *handle);
 
 #endif

+ 5 - 0
kernel/write.c

@@ -76,6 +76,11 @@ void merge_cells(zend_string *range, zend_string *value, excel_resource_t *res)
     worksheet_merge_range(res->worksheet, RANGE(ZSTR_VAL(range)), ZSTR_VAL(value), NULL);
 }
 
+void set_column(zend_string *range, double width, excel_resource_t *res, lxw_format *format)
+{
+    worksheet_set_column(res->worksheet, COLS(ZSTR_VAL(range)), (double)width, format);
+}
+
 /*
  * Call finalization code and close file.
  */