Skip to content

Commit 862d1fc

Browse files
committed
Feat: margins
1 parent c742b71 commit 862d1fc

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

include/xlswriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ void format_copy(lxw_format *new_format, lxw_format *other_format);
315315
void printed_direction(xls_resource_write_t *res, unsigned int direction);
316316
void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
317317
void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
318+
void margins(xls_resource_write_t *res, double left, double right, double top, double bottom);
318319
void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
319320
void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validation *validation);
320321
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);

kernel/excel.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ ZEND_BEGIN_ARG_INFO_EX(xls_set_paper_arginfo, 0, 0, 1)
190190
ZEND_ARG_INFO(0, paper)
191191
ZEND_END_ARG_INFO()
192192

193+
ZEND_BEGIN_ARG_INFO_EX(xls_set_margins_arginfo, 0, 0, 4)
194+
ZEND_ARG_INFO(0, left)
195+
ZEND_ARG_INFO(0, right)
196+
ZEND_ARG_INFO(0, top)
197+
ZEND_ARG_INFO(0, bottom)
198+
ZEND_END_ARG_INFO()
199+
193200
ZEND_BEGIN_ARG_INFO_EX(xls_set_global_format, 0, 0, 1)
194201
ZEND_ARG_INFO(0, format_handle)
195202
ZEND_END_ARG_INFO()
@@ -992,6 +999,30 @@ PHP_METHOD(vtiful_xls, setPaper)
992999

9931000
paper(&obj->write_ptr, type);
9941001
}
1002+
/* }}} */
1003+
1004+
/** {{{ \Vtiful\Kernel\Excel::setMargins(double|null $left, double|null $right, double|null $top, double|null $bottom)
1005+
*/
1006+
PHP_METHOD(vtiful_xls, setMargins)
1007+
{
1008+
double left = 0.7, right = 0.7, top = 0.75, bottom = 0.75;
1009+
1010+
ZEND_PARSE_PARAMETERS_START(0, 4)
1011+
Z_PARAM_OPTIONAL
1012+
Z_PARAM_DOUBLE_OR_NULL(left, _dummy)
1013+
Z_PARAM_DOUBLE_OR_NULL(right, _dummy)
1014+
Z_PARAM_DOUBLE_OR_NULL(top, _dummy)
1015+
Z_PARAM_DOUBLE_OR_NULL(bottom, _dummy)
1016+
ZEND_PARSE_PARAMETERS_END();
1017+
1018+
ZVAL_COPY(return_value, getThis());
1019+
1020+
xls_object *obj = Z_XLS_P(getThis());
1021+
1022+
// units: inches to cm
1023+
margins(&obj->write_ptr, left / 2.54, right / 2.54, top / 2.54, bottom / 2.54);
1024+
}
1025+
/* }}} */
9951026

9961027
/** {{{ \Vtiful\Kernel\Excel::defaultFormat(resource $format)
9971028
*/
@@ -1572,7 +1603,6 @@ zend_function_entry xls_methods[] = {
15721603
PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
15731604
PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
15741605
PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
1575-
PHP_ME(vtiful_xls, setPaper, xls_set_paper_arginfo, ZEND_ACC_PUBLIC)
15761606
PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format, ZEND_ACC_PUBLIC)
15771607
PHP_ME(vtiful_xls, freezePanes, xls_freeze_panes_arginfo, ZEND_ACC_PUBLIC)
15781608

@@ -1582,6 +1612,8 @@ zend_function_entry xls_methods[] = {
15821612
PHP_ME(vtiful_xls, zoom, xls_sheet_zoom_arginfo, ZEND_ACC_PUBLIC)
15831613
PHP_ME(vtiful_xls, gridline, xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
15841614

1615+
PHP_ME(vtiful_xls, setPaper, xls_set_paper_arginfo, ZEND_ACC_PUBLIC)
1616+
PHP_ME(vtiful_xls, setMargins, xls_set_margins_arginfo, ZEND_ACC_PUBLIC)
15851617
PHP_ME(vtiful_xls, setPortrait, xls_set_printed_portrait_arginfo, ZEND_ACC_PUBLIC)
15861618
PHP_ME(vtiful_xls, setLandscape, xls_set_printed_landscape_arginfo, ZEND_ACC_PUBLIC)
15871619

kernel/write.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ void paper(xls_resource_write_t *res, zend_long type)
394394
worksheet_set_paper(res->worksheet, type);
395395
}
396396

397+
/*
398+
* Set margins
399+
*/
400+
void margins(xls_resource_write_t *res, double left, double right, double top, double bottom)
401+
{
402+
worksheet_set_margins(res->worksheet, left, right, top, bottom);
403+
}
404+
397405
/*
398406
* Call finalization code and close file.
399407
*/

tests/margins.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = [
8+
'path' => './tests'
9+
];
10+
11+
$fileObject = new \Vtiful\Kernel\Excel($config);
12+
$fileObject = $fileObject->fileName('tutorial.xlsx');
13+
14+
$filePath = $fileObject->header(['name', 'age'])
15+
->data([
16+
['viest', 21],
17+
['wjx', 21]
18+
])
19+
->setPaper(\Vtiful\Kernel\Excel::PAPER_A3)
20+
->setLandscape()
21+
->setMargins(1, 1, 2, 2)
22+
->output();
23+
24+
var_dump($filePath);
25+
?>
26+
--CLEAN--
27+
<?php
28+
@unlink(__DIR__ . '/tutorial.xlsx');
29+
?>
30+
--EXPECT--
31+
string(21) "./tests/tutorial.xlsx"

0 commit comments

Comments
 (0)