Skip to content

Commit

Permalink
Merge pull request #389 from viest/dev
Browse files Browse the repository at this point in the history
Feat: paper,margins
  • Loading branch information
viest committed Jul 18, 2021
2 parents e95a68a + 862d1fc commit 2b789e2
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,15 @@ void comment_show(xls_resource_write_t *res);
void hide_worksheet(xls_resource_write_t *res);
void first_worksheet(xls_resource_write_t *res);
void zoom(xls_resource_write_t *res, zend_long zoom);
void paper(xls_resource_write_t *res, zend_long type);
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 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
108 changes: 101 additions & 7 deletions kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, height)
ZEND_END_ARG_INFO()

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 @@ -972,6 +983,47 @@ PHP_METHOD(vtiful_xls, setRow)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::setPaper(int $paper)
*/
PHP_METHOD(vtiful_xls, setPaper)
{
zend_long type = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(type)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

xls_object *obj = Z_XLS_P(getThis());

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)
*/
PHP_METHOD(vtiful_xls, defaultFormat)
Expand Down Expand Up @@ -1149,10 +1201,9 @@ PHP_METHOD(vtiful_xls, protection)
}
/* }}} */


/** {{{ \Vtiful\Kernel\Excel::setPrintedPortrait()
/** {{{ \Vtiful\Kernel\Excel::setPortrait()
*/
PHP_METHOD(vtiful_xls, setPrintedPortrait)
PHP_METHOD(vtiful_xls, setPortrait)
{
ZVAL_COPY(return_value, getThis());

Expand All @@ -1165,9 +1216,9 @@ PHP_METHOD(vtiful_xls, setPrintedPortrait)
/* }}} */


/** {{{ \Vtiful\Kernel\Excel::setPrintedLandscape()
/** {{{ \Vtiful\Kernel\Excel::setLandscape()
*/
PHP_METHOD(vtiful_xls, setPrintedLandscape)
PHP_METHOD(vtiful_xls, setLandscape)
{
ZVAL_COPY(return_value, getThis());

Expand Down Expand Up @@ -1561,8 +1612,10 @@ 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, setPrintedPortrait, xls_set_printed_portrait_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setPrintedLandscape, xls_set_printed_landscape_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)

PHP_ME(vtiful_xls, setCurrentSheetHide, xls_hide_sheet_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setCurrentSheetIsFirst, xls_first_sheet_arginfo, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -1619,6 +1672,47 @@ VTIFUL_STARTUP_FUNCTION(excel) {
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "GRIDLINES_SHOW_PRINT", LXW_SHOW_PRINT_GRIDLINES)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "GRIDLINES_SHOW_SCREEN", LXW_SHOW_SCREEN_GRIDLINES)

REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_DEFAULT", 0)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_LETTER", 1)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_LETTER_SMALL", 2)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_TABLOID", 3)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_LEDGER", 4)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_LEGAL", 5)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_STATEMENT", 6)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_EXECUTIVE", 7)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_A3", 8)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_A4", 9)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_A4_SMALL", 10)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_A5", 11)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_B4", 12)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_B5", 13)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_FOLIO", 14)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_QUARTO", 15)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_NOTE", 18)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_9", 19)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_10", 20)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_11", 21)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_12", 22)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_14", 23)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_C_SIZE_SHEET", 24)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_D_SIZE_SHEET", 25)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_E_SIZE_SHEET", 26)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_DL", 27)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_C3", 28)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_C4", 29)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_C5", 30)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_C6", 31)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_C65", 32)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_B4", 33)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_B5", 34)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_B6", 35)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_1", 36)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_MONARCH", 37)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_ENVELOPE_2", 38)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_FANFOLD", 39)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_GERMAN_STD_FANFOLD", 40)
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, "PAPER_GERMAN_LEGAL_FANFOLD", 41)

REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_INT, READ_TYPE_INT);
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_DOUBLE, READ_TYPE_DOUBLE);
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_STRING, READ_TYPE_STRING);
Expand Down
16 changes: 16 additions & 0 deletions kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,22 @@ void first_worksheet(xls_resource_write_t *res)
worksheet_set_first_sheet(res->worksheet);
}

/*
* Paper format
*/
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"
30 changes: 30 additions & 0 deletions tests/paper.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--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()
->output();

var_dump($filePath);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/tutorial.xlsx');
?>
--EXPECT--
string(21) "./tests/tutorial.xlsx"
6 changes: 3 additions & 3 deletions tests/printed.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ try {
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);

$excel->setPrintedPortrait();
$excel->setPortrait();
} catch (\Exception $exception) {
var_dump($exception->getCode());
var_dump($exception->getMessage());
Expand All @@ -18,7 +18,7 @@ $config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);

$excel->fileName('printed_portrait.xlsx', 'sheet1')
->setPrintedPortrait()
->setPortrait()
->output();

var_dump($excel);
Expand All @@ -27,7 +27,7 @@ $config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);

$excel->fileName('printed_landscape.xlsx', 'sheet1')
->setPrintedLandscape()
->setLandscape()
->output();

var_dump($excel);
Expand Down
29 changes: 29 additions & 0 deletions tests/protection_password.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--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]
])
->protection('password')
->output();

var_dump($filePath);
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/tutorial.xlsx');
?>
--EXPECT--
string(21) "./tests/tutorial.xlsx"

0 comments on commit 2b789e2

Please sign in to comment.