Skip to content

Commit 1f7b2de

Browse files
committed
Feat: work sheet protection
1 parent 66ca9ea commit 1f7b2de

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

include/xlswriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ void comment_show(xls_resource_write_t *res);
211211
void zoom(xls_resource_write_t *res, zend_long zoom);
212212
void gridlines(xls_resource_write_t *res, zend_long option);
213213
void auto_filter(zend_string *range, xls_resource_write_t *res);
214+
void protection(xls_resource_write_t *res, zend_string *password);
214215
void format_copy(lxw_format *new_format, lxw_format *other_format);
215216
void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
216217
void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);

kernel/excel.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ ZEND_END_ARG_INFO()
261261
ZEND_BEGIN_ARG_INFO_EX(xls_sheet_zoom_arginfo, 0, 0, 1)
262262
ZEND_ARG_INFO(0, scale)
263263
ZEND_END_ARG_INFO()
264+
265+
ZEND_BEGIN_ARG_INFO_EX(xls_protection_arginfo, 0, 0, 0)
266+
ZEND_ARG_INFO(0, password)
267+
ZEND_END_ARG_INFO()
264268
/* }}} */
265269

266270
/** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
@@ -1078,6 +1082,25 @@ PHP_METHOD(vtiful_xls, zoom)
10781082
}
10791083
/* }}} */
10801084

1085+
/** {{{ \Vtiful\Kernel\Excel::protection(string $password)
1086+
*/
1087+
PHP_METHOD(vtiful_xls, protection)
1088+
{
1089+
zend_string *password = NULL;
1090+
1091+
ZEND_PARSE_PARAMETERS_START(0, 1)
1092+
Z_PARAM_OPTIONAL
1093+
Z_PARAM_STR(password)
1094+
ZEND_PARSE_PARAMETERS_END();
1095+
1096+
ZVAL_COPY(return_value, getThis());
1097+
1098+
xls_object* obj = Z_XLS_P(getThis());
1099+
1100+
protection(&obj->write_ptr, password);
1101+
}
1102+
/* }}} */
1103+
10811104
#ifdef ENABLE_READER
10821105

10831106
/** {{{ \Vtiful\Kernel\Excel::openFile()
@@ -1391,6 +1414,8 @@ zend_function_entry xls_methods[] = {
13911414
PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format, ZEND_ACC_PUBLIC)
13921415
PHP_ME(vtiful_xls, freezePanes, xls_freeze_panes_arginfo, ZEND_ACC_PUBLIC)
13931416

1417+
PHP_ME(vtiful_xls, protection, xls_protection_arginfo, ZEND_ACC_PUBLIC)
1418+
13941419
PHP_ME(vtiful_xls, zoom, xls_sheet_zoom_arginfo, ZEND_ACC_PUBLIC)
13951420
PHP_ME(vtiful_xls, gridline, xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
13961421

kernel/write.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ void zoom(xls_resource_write_t *res, zend_long zoom)
339339
worksheet_set_zoom(res->worksheet, zoom);
340340
}
341341

342+
/*
343+
* Set the worksheet protection
344+
*/
345+
void protection(xls_resource_write_t *res, zend_string *password)
346+
{
347+
if (password == NULL) {
348+
worksheet_protect(res->worksheet, NULL, NULL);
349+
} else {
350+
worksheet_protect(res->worksheet, ZSTR_VAL(password), NULL);
351+
}
352+
}
353+
342354
/*
343355
* Call finalization code and close file.
344356
*/

tests/protection.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
->protection()
20+
->output();
21+
22+
var_dump($filePath);
23+
?>
24+
--CLEAN--
25+
<?php
26+
@unlink(__DIR__ . '/tutorial.xlsx');
27+
?>
28+
--EXPECT--
29+
string(21) "./tests/tutorial.xlsx"

0 commit comments

Comments
 (0)