viest 3 лет назад
Родитель
Сommit
862d1fc6e8
4 измененных файлов с 73 добавлено и 1 удалено
  1. 1 0
      include/xlswriter.h
  2. 33 1
      kernel/excel.c
  3. 8 0
      kernel/write.c
  4. 31 0
      tests/margins.phpt

+ 1 - 0
include/xlswriter.h

@@ -315,6 +315,7 @@ 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 margins(xls_resource_write_t *res, double left, double right, double top, double bottom);
 void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
 void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validation *validation);
 void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);

+ 33 - 1
kernel/excel.c

@@ -190,6 +190,13 @@ ZEND_BEGIN_ARG_INFO_EX(xls_set_paper_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, paper)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(xls_set_margins_arginfo, 0, 0, 4)
+                ZEND_ARG_INFO(0, left)
+                ZEND_ARG_INFO(0, right)
+                ZEND_ARG_INFO(0, top)
+                ZEND_ARG_INFO(0, bottom)
+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()
@@ -992,6 +999,30 @@ PHP_METHOD(vtiful_xls, setPaper)
 
     paper(&obj->write_ptr, type);
 }
+/* }}} */
+
+/** {{{ \Vtiful\Kernel\Excel::setMargins(double|null $left, double|null $right, double|null $top, double|null $bottom)
+ */
+PHP_METHOD(vtiful_xls, setMargins)
+{
+    double left = 0.7, right = 0.7, top = 0.75, bottom = 0.75;
+
+    ZEND_PARSE_PARAMETERS_START(0, 4)
+            Z_PARAM_OPTIONAL
+            Z_PARAM_DOUBLE_OR_NULL(left, _dummy)
+            Z_PARAM_DOUBLE_OR_NULL(right, _dummy)
+            Z_PARAM_DOUBLE_OR_NULL(top, _dummy)
+            Z_PARAM_DOUBLE_OR_NULL(bottom, _dummy)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object *obj = Z_XLS_P(getThis());
+
+    // units: inches to cm
+    margins(&obj->write_ptr, left / 2.54, right / 2.54, top / 2.54, bottom / 2.54);
+}
+/* }}} */
 
 /** {{{ \Vtiful\Kernel\Excel::defaultFormat(resource $format)
  */
@@ -1572,7 +1603,6 @@ 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, setPaper,      xls_set_paper_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)
 
@@ -1582,6 +1612,8 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, zoom,          xls_sheet_zoom_arginfo,     ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, gridline,      xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
 
+        PHP_ME(vtiful_xls, setPaper,     xls_set_paper_arginfo,             ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_xls, setMargins,   xls_set_margins_arginfo,           ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, setPortrait,  xls_set_printed_portrait_arginfo,  ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, setLandscape, xls_set_printed_landscape_arginfo, ZEND_ACC_PUBLIC)
 

+ 8 - 0
kernel/write.c

@@ -394,6 +394,14 @@ void paper(xls_resource_write_t *res, zend_long type)
     worksheet_set_paper(res->worksheet, type);
 }
 
+/*
+ * Set margins
+ */
+void margins(xls_resource_write_t *res, double left, double right, double top, double bottom)
+{
+    worksheet_set_margins(res->worksheet, left, right, top, bottom);
+}
+
 /*
  * Call finalization code and close file.
  */

+ 31 - 0
tests/margins.phpt

@@ -0,0 +1,31 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = [
+    'path' => './tests'
+];
+
+$fileObject = new \Vtiful\Kernel\Excel($config);
+$fileObject = $fileObject->fileName('tutorial.xlsx');
+
+$filePath = $fileObject->header(['name', 'age'])
+    ->data([
+        ['viest', 21],
+        ['wjx',   21]
+    ])
+    ->setPaper(\Vtiful\Kernel\Excel::PAPER_A3)
+    ->setLandscape()
+    ->setMargins(1, 1, 2, 2)
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"