Bladeren bron

Feat: work sheet protection

viest 4 jaren geleden
bovenliggende
commit
1f7b2deb36
4 gewijzigde bestanden met toevoegingen van 67 en 0 verwijderingen
  1. 1 0
      include/xlswriter.h
  2. 25 0
      kernel/excel.c
  3. 12 0
      kernel/write.c
  4. 29 0
      tests/protection.phpt

+ 1 - 0
include/xlswriter.h

@@ -211,6 +211,7 @@ void comment_show(xls_resource_write_t *res);
 void zoom(xls_resource_write_t *res, zend_long zoom);
 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 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);

+ 25 - 0
kernel/excel.c

@@ -261,6 +261,10 @@ ZEND_END_ARG_INFO()
 ZEND_BEGIN_ARG_INFO_EX(xls_sheet_zoom_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, scale)
 ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(xls_protection_arginfo, 0, 0, 0)
+                ZEND_ARG_INFO(0, password)
+ZEND_END_ARG_INFO()
 /* }}} */
 
 /** {{{ \Vtiful\Kernel\Excel::__construct(array $config)
@@ -1078,6 +1082,25 @@ PHP_METHOD(vtiful_xls, zoom)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Excel::protection(string $password)
+ */
+PHP_METHOD(vtiful_xls, protection)
+{
+    zend_string *password = NULL;
+
+    ZEND_PARSE_PARAMETERS_START(0, 1)
+            Z_PARAM_OPTIONAL
+            Z_PARAM_STR(password)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object* obj = Z_XLS_P(getThis());
+
+    protection(&obj->write_ptr, password);
+}
+/* }}} */
+
 #ifdef ENABLE_READER
 
 /** {{{ \Vtiful\Kernel\Excel::openFile()
@@ -1391,6 +1414,8 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format,      ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, freezePanes,   xls_freeze_panes_arginfo,   ZEND_ACC_PUBLIC)
 
+        PHP_ME(vtiful_xls, protection,    xls_protection_arginfo,     ZEND_ACC_PUBLIC)
+
         PHP_ME(vtiful_xls, zoom,          xls_sheet_zoom_arginfo,     ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, gridline,      xls_sheet_gridline_arginfo, ZEND_ACC_PUBLIC)
 

+ 12 - 0
kernel/write.c

@@ -339,6 +339,18 @@ void zoom(xls_resource_write_t *res, zend_long zoom)
     worksheet_set_zoom(res->worksheet, zoom);
 }
 
+/*
+ * Set the worksheet protection
+ */
+void protection(xls_resource_write_t *res, zend_string *password)
+{
+    if (password == NULL) {
+        worksheet_protect(res->worksheet, NULL, NULL);
+    } else {
+        worksheet_protect(res->worksheet, ZSTR_VAL(password), NULL);
+    }
+}
+
 /*
  * Call finalization code and close file.
  */

+ 29 - 0
tests/protection.phpt

@@ -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()
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"