Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add addDefinedName Method to Define Named Ranges in Excel #541

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 79,11 @@ ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, sheet_name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_add_defined_name_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, formula)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_const_memory_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, file_name)
ZEND_ARG_INFO(0, sheet_name)
Expand Down Expand Up @@ -1771,6 1776,33 @@ PHP_METHOD(vtiful_xls, nextCellCallback)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::addDefinedName(string $name, string $formula)
*/
PHP_METHOD(vtiful_xls, addDefinedName)
{
zend_string *name = NULL, *formula = NULL;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(name)
Z_PARAM_STR(formula)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

xls_object *obj = Z_XLS_P(getThis());

WORKBOOK_NOT_INITIALIZED(obj);

lxw_error error = workbook_define_name(
obj->write_ptr.workbook,
ZSTR_VAL(name),
ZSTR_VAL(formula)
);

WORKSHEET_WRITER_EXCEPTION(error);
}
/* }}} */

#endif

/** {{{ xls_methods
Expand All @@ -1779,6 1811,7 @@ zend_function_entry xls_methods[] = {
PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, close, xls_close_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, addDefinedName, xls_add_defined_name_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, existSheet, xls_file_exist_sheet, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, checkoutSheet, xls_file_checkout_sheet, ZEND_ACC_PUBLIC)
Expand Down
26 changes: 26 additions & 0 deletions tests/add_defined_name.phpt
Original file line number Diff line number Diff line change
@@ -0,0 1,26 @@
--TEST--
Test addDefinedName method
--SKIPIF--
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
--FILE--
<?php
$config = ['path' => './tests'];
$excel = new \Vtiful\Kernel\Excel($config);

$filePath = $excel->fileName("add_defined_name_test.xlsx")
->addDefinedName('MyRange', '=Sheet1!$A$1:$A$10')
->data([
[1],
[2],
[3],
])
->output();

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