فهرست منبع

Merge pull request #334 from viest/dev

Dev
viest 4 سال پیش
والد
کامیت
a6c1562cff
4فایلهای تغییر یافته به همراه130 افزوده شده و 1 حذف شده
  1. 15 1
      include/xlswriter.h
  2. 39 0
      kernel/excel.c
  3. 12 0
      kernel/write.c
  4. 64 0
      tests/printed.phpt

+ 15 - 1
include/xlswriter.h

@@ -68,6 +68,11 @@ enum xlswriter_boolean {
     XLSWRITER_TRUE
 };
 
+enum xlswirter_printed_direction {
+    XLSWRITER_PRINTED_LANDSCAPE,
+    XLSWRITER_PRINTED_PORTRAIT,
+};
+
 typedef struct {
     lxw_workbook  *workbook;
     lxw_worksheet *worksheet;
@@ -124,12 +129,20 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
 
 #define WORKBOOK_NOT_INITIALIZED(xls_object_t)                                                                       \
     do {                                                                                                             \
-        if(obj->write_ptr.workbook == NULL) {                                                                        \
+        if(xls_object_t->write_ptr.workbook == NULL) {                                                               \
             zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);   \
             return;                                                                                                  \
         }                                                                                                            \
     } while(0);
 
+#define WORKSHEET_NOT_INITIALIZED(xls_object_t)                                                                     \
+    do {                                                                                                            \
+        if (xls_object_t->write_ptr.worksheet == NULL) {                                                            \
+            zend_throw_exception(vtiful_exception_ce, "worksheet not initialized", 200);                            \
+            return;                                                                                                 \
+        }                                                                                                           \
+    } 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) {              \
@@ -214,6 +227,7 @@ void gridlines(xls_resource_write_t *res, zend_long option);
 void auto_filter(zend_string *range, xls_resource_write_t *res);
 void protection(xls_resource_write_t *res, zend_string *password);
 void format_copy(lxw_format *new_format, lxw_format *other_format);
+void printed_direction(xls_resource_write_t *res, unsigned int direction);
 void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
 void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
 void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);

+ 39 - 0
kernel/excel.c

@@ -266,6 +266,12 @@ ZEND_END_ARG_INFO()
 ZEND_BEGIN_ARG_INFO_EX(xls_protection_arginfo, 0, 0, 0)
                 ZEND_ARG_INFO(0, password)
 ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(xls_set_printed_portrait_arginfo, 0, 0, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(xls_set_printed_landscape_arginfo, 0, 0, 0)
+ZEND_END_ARG_INFO()
 /* }}} */
 
 /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
@@ -1102,6 +1108,36 @@ PHP_METHOD(vtiful_xls, protection)
 }
 /* }}} */
 
+
+/** {{{ \Vtiful\Kernel\Excel::setPrintedPortrait()
+ */
+PHP_METHOD(vtiful_xls, setPrintedPortrait)
+{
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object* obj = Z_XLS_P(getThis());
+
+    WORKBOOK_NOT_INITIALIZED(obj);
+
+    printed_direction(&obj->write_ptr, XLSWRITER_PRINTED_PORTRAIT);
+}
+/* }}} */
+
+
+/** {{{ \Vtiful\Kernel\Excel::setPrintedLandscape()
+ */
+PHP_METHOD(vtiful_xls, setPrintedLandscape)
+{
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object* obj = Z_XLS_P(getThis());
+
+    WORKBOOK_NOT_INITIALIZED(obj);
+
+    printed_direction(&obj->write_ptr, XLSWRITER_PRINTED_LANDSCAPE);
+}
+/* }}} */
+
 #ifdef ENABLE_READER
 
 /** {{{ \Vtiful\Kernel\Excel::openFile()
@@ -1435,6 +1471,9 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, stringFromColumnIndex,   xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
         PHP_ME(vtiful_xls, timestampFromDateDouble, xls_string_to_index, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
 
+        PHP_ME(vtiful_xls, setPrintedPortrait, xls_set_printed_portrait_arginfo,   ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_xls, setPrintedLandscape, xls_set_printed_landscape_arginfo, ZEND_ACC_PUBLIC)
+
 #ifdef ENABLE_READER
         PHP_ME(vtiful_xls, openFile,         xls_open_file_arginfo,          ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, openSheet,        xls_open_sheet_arginfo,         ZEND_ACC_PUBLIC)

+ 12 - 0
kernel/write.c

@@ -351,6 +351,18 @@ void protection(xls_resource_write_t *res, zend_string *password)
     }
 }
 
+/*
+ * Set the worksheet printed direction
+ */
+void printed_direction(xls_resource_write_t *res, unsigned int direction)
+{
+    if (direction == XLSWRITER_PRINTED_PORTRAIT) {
+        worksheet_set_portrait(res->worksheet);
+    }
+
+    worksheet_set_landscape(res->worksheet);
+}
+
 /*
  * Call finalization code and close file.
  */

+ 64 - 0
tests/printed.phpt

@@ -0,0 +1,64 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+try {
+    $config = ['path' => './tests'];
+    $excel  = new \Vtiful\Kernel\Excel($config);
+
+    $excel->setPrintedPortrait();
+} catch (\Exception $exception) {
+    var_dump($exception->getCode());
+    var_dump($exception->getMessage());
+}
+
+$config = ['path' => './tests'];
+$excel  = new \Vtiful\Kernel\Excel($config);
+
+$excel->fileName('printed_portrait.xlsx', 'sheet1')
+    ->setPrintedPortrait()
+    ->output();
+
+var_dump($excel);
+
+$config = ['path' => './tests'];
+$excel  = new \Vtiful\Kernel\Excel($config);
+
+$excel->fileName('printed_landscape.xlsx', 'sheet1')
+    ->setPrintedLandscape()
+    ->output();
+
+var_dump($excel);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/printed_portrait.xlsx');
+@unlink(__DIR__ . '/printed_landscape.xlsx');
+?>
+--EXPECT--
+int(130)
+string(51) "Please create a file first, use the filename method"
+object(Vtiful\Kernel\Excel)#3 (3) {
+  ["config":"Vtiful\Kernel\Excel":private]=>
+  array(1) {
+    ["path"]=>
+    string(7) "./tests"
+  }
+  ["fileName":"Vtiful\Kernel\Excel":private]=>
+  string(29) "./tests/printed_portrait.xlsx"
+  ["read_row_type":"Vtiful\Kernel\Excel":private]=>
+  NULL
+}
+object(Vtiful\Kernel\Excel)#1 (3) {
+  ["config":"Vtiful\Kernel\Excel":private]=>
+  array(1) {
+    ["path"]=>
+    string(7) "./tests"
+  }
+  ["fileName":"Vtiful\Kernel\Excel":private]=>
+  string(30) "./tests/printed_landscape.xlsx"
+  ["read_row_type":"Vtiful\Kernel\Excel":private]=>
+  NULL
+}