Skip to content

Commit

Permalink
Feat: margins
Browse files Browse the repository at this point in the history
  • Loading branch information
viest committed Jul 18, 2021
1 parent c742b71 commit 862d1fc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 33 additions & 1 deletion kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/margins.phpt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 862d1fc

Please sign in to comment.