Skip to content

Commit

Permalink
Set outline level for rows/columns
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiD84 committed Oct 11, 2023
1 parent 6cd9720 commit 33d8c20
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 57 deletions.
1 change: 1 addition & 0 deletions include/help.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
unsigned int file_exists(const char *path);
unsigned int directory_exists(const char *path);
zend_long date_double_to_timestamp(double value);
lxw_row_col_options* default_row_col_options();

#endif //PHP_EXT_XLS_EXPORT_HELP_H
12 changes: 9 additions & 3 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ typedef struct _vtiful_xls_object {
xls_resource_write_t write_ptr;
zend_long write_line;
xls_resource_format_t format_ptr;
lxw_row_col_options *row_options;
zend_object zo;
} xls_object;

Expand Down Expand Up @@ -294,6 +295,11 @@ static inline void php_vtiful_close_resource(zend_object *obj) {
intern->format_ptr.format = NULL;
}

if (intern->row_options != NULL) {
efree(intern->row_options);
intern->row_options = NULL;
}

#ifdef ENABLE_READER
if (intern->read_ptr.sheet_t != NULL) {
xlsxioread_sheet_close(intern->read_ptr.sheet_t);
Expand Down Expand Up @@ -340,14 +346,14 @@ 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 set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options);
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);
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options);
void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw_format *format);
void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res);
void call_object_method(zval *object, const char *function_name, uint32_t param_count, zval *params, zval *ret_val);
void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_write_t *res);
void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format);
void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options);
void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res);
void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format);
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle);
Expand Down
173 changes: 127 additions & 46 deletions kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)

intern->format_ptr.format = NULL;
intern->write_ptr.workbook = NULL;
intern->row_options = NULL;

intern->read_ptr.data_type_default = READ_TYPE_EMPTY;

Expand Down Expand Up @@ -182,16 +183,22 @@ ZEND_BEGIN_ARG_INFO_EX(xls_merge_cells_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, format_handle)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_set_column_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, format_handle)
ZEND_BEGIN_ARG_INFO_EX(xls_set_column_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, range)
ZEND_ARG_INFO(0, width)
ZEND_ARG_INFO(0, format_handle)
ZEND_ARG_INFO(0, level)
ZEND_ARG_INFO(0, collapsed)
ZEND_ARG_INFO(0, hidden)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, format_handle)
ZEND_BEGIN_ARG_INFO_EX(xls_set_row_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, range)
ZEND_ARG_INFO(0, height)
ZEND_ARG_INFO(0, format_handle)
ZEND_ARG_INFO(0, level)
ZEND_ARG_INFO(0, collapsed)
ZEND_ARG_INFO(0, hidden)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_set_curr_line_arginfo, 0, 0, 1)
Expand All @@ -216,6 +223,12 @@ ZEND_BEGIN_ARG_INFO_EX(xls_set_global_format, 0, 0, 1)
ZEND_ARG_INFO(0, format_handle)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_set_default_row_options_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, level)
ZEND_ARG_INFO(0, collapsed)
ZEND_ARG_INFO(0, hidden)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_open_file_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, zs_file_name)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -650,6 +663,11 @@ PHP_METHOD(vtiful_xls, data)
continue;
}

if (obj->row_options != NULL) {
WORKSHEET_WRITER_EXCEPTION(
worksheet_set_row_opt(obj->write_ptr.worksheet, SHEET_CURRENT_LINE(obj), LXW_DEF_ROW_HEIGHT, NULL, obj->row_options));
}

column_index = 0;

ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(data_r_value), index, key, data) {
Expand Down Expand Up @@ -1014,64 +1032,92 @@ PHP_METHOD(vtiful_xls, mergeCells)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::setColumn(resource $format, string $range [, int $width])
/** {{{ \Vtiful\Kernel\Excel::setColumn(string $range, float $width, resource $format = null, int $level = 0, bool $collapsed = false, bool $hidden = false)
*/
PHP_METHOD(vtiful_xls, setColumn)
{
zval *format_handle = NULL;
zend_string *range = NULL;

zend_long level = 0;
zend_bool collapsed = 0;
zend_bool hidden = 0;
double width = 0;
int argc = ZEND_NUM_ARGS();

ZEND_PARSE_PARAMETERS_START(2, 3)
ZEND_PARSE_PARAMETERS_START(2, 6)
Z_PARAM_STR(range)
Z_PARAM_DOUBLE(width)
Z_PARAM_OPTIONAL
Z_PARAM_RESOURCE_OR_NULL(format_handle)
Z_PARAM_LONG_OR_NULL(level, _dummy)
Z_PARAM_BOOL_OR_NULL(collapsed, _dummy)
Z_PARAM_BOOL_OR_NULL(hidden, _dummy)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

if (level < 0 || level > 7) {
LXW_WARN_FORMAT1("outline level must be in 0..7 range, '%d' given.", level);
level = 0;
}

xls_object *obj = Z_XLS_P(getThis());

WORKBOOK_NOT_INITIALIZED(obj);

if (argc == 3 && format_handle != NULL) {
set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
lxw_row_col_options* options = default_row_col_options();
options->level = level;
options->collapsed = collapsed;
options->hidden = hidden;

if (format_handle != NULL) {
set_column(range, width, &obj->write_ptr, zval_get_format(format_handle), options);
} else {
set_column(range, width, &obj->write_ptr, NULL);
set_column(range, width, &obj->write_ptr, NULL, options);
}
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::setRow(resource $format, string $range [, int $heitght])
/** {{{ \Vtiful\Kernel\Excel::setRow(string $range, float $height, resource $format = null, int $level = 0, bool $collapsed = false, bool $hidden = false)
*/
PHP_METHOD(vtiful_xls, setRow)
{
zval *format_handle = NULL;
zend_string *range = NULL;

zend_long level = 0;
zend_bool collapsed = 0;
zend_bool hidden = 0;
double height = 0;
int argc = ZEND_NUM_ARGS();

ZEND_PARSE_PARAMETERS_START(2, 3)
ZEND_PARSE_PARAMETERS_START(2, 6)
Z_PARAM_STR(range)
Z_PARAM_DOUBLE(height)
Z_PARAM_OPTIONAL
Z_PARAM_RESOURCE_OR_NULL(format_handle)
Z_PARAM_LONG_OR_NULL(level, _dummy)
Z_PARAM_BOOL_OR_NULL(collapsed, _dummy)
Z_PARAM_BOOL_OR_NULL(hidden, _dummy)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

if (level < 0 || level > 7) {
LXW_WARN_FORMAT1("outline level must be in 0..7 range, '%d' given.", level);
level = 0;
}

xls_object *obj = Z_XLS_P(getThis());

WORKBOOK_NOT_INITIALIZED(obj);

if (argc == 3 && format_handle != NULL) {
set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
lxw_row_col_options* options = default_row_col_options();
options->level = level;
options->collapsed = collapsed;
options->hidden = hidden;

if (format_handle != NULL) {
set_row(range, height, &obj->write_ptr, zval_get_format(format_handle), options);
} else {
set_row(range, height, &obj->write_ptr, NULL);
set_row(range, height, &obj->write_ptr, NULL, options);
}
}
/* }}} */
Expand Down Expand Up @@ -1134,6 +1180,39 @@ PHP_METHOD(vtiful_xls, defaultFormat)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::defaultRowOptions(int $level = 0, bool $collapsed = false, bool $hidden = false)
*/
PHP_METHOD(vtiful_xls, defaultRowOptions)
{
zend_long level = 0;
zend_bool collapsed = 0;
zend_bool hidden = 0;

ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(level, _dummy)
Z_PARAM_BOOL_OR_NULL(collapsed, _dummy)
Z_PARAM_BOOL_OR_NULL(hidden, _dummy)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

if (level < 0 || level > 7) {
LXW_WARN_FORMAT1("outline level must be in 0..7 range, '%d' given.", level);
level = 0;
}

xls_object *obj = Z_XLS_P(getThis());

if (obj->row_options == NULL) {
obj->row_options = default_row_col_options();
}
obj->row_options->level = level;
obj->row_options->collapsed = collapsed;
obj->row_options->hidden = hidden;
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::freezePanes(int $row, int $column)
*/
PHP_METHOD(vtiful_xls, freezePanes)
Expand Down Expand Up @@ -1691,34 +1770,36 @@ PHP_METHOD(vtiful_xls, nextCellCallback)
/** {{{ xls_methods
*/
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, 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)
PHP_ME(vtiful_xls, activateSheet, xls_file_activate_sheet, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, constMemory, xls_const_memory_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, output, xls_output_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, getHandle, xls_get_handle_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertRichText, xls_insert_rtext_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertDate, xls_insert_date_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertChart, xls_insert_chart_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertUrl, xls_insert_url_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertComment, xls_insert_comment_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, showComment, xls_show_comment_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, getCurrentLine, xls_get_curr_line_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setCurrentLine, xls_set_curr_line_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format, ZEND_ACC_PUBLIC)
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, 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)
PHP_ME(vtiful_xls, activateSheet, xls_file_activate_sheet, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, constMemory, xls_const_memory_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, header, xls_header_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, data, xls_data_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, output, xls_output_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, getHandle, xls_get_handle_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertRichText, xls_insert_rtext_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertDate, xls_insert_date_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertChart, xls_insert_chart_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertUrl, xls_insert_url_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertImage, xls_insert_image_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, insertComment, xls_insert_comment_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, showComment, xls_show_comment_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, mergeCells, xls_merge_cells_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setColumn, xls_set_column_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, getCurrentLine, xls_get_curr_line_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, setCurrentLine, xls_set_curr_line_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, defaultFormat, xls_set_global_format, ZEND_ACC_PUBLIC)
PHP_ME(vtiful_xls, defaultRowOptions, xls_set_default_row_options_arginfo, 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)
Expand Down
9 changes: 8 additions & 1 deletion kernel/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ unsigned int file_exists(const char *path) {
zval_ptr_dtor(&file_exists);
return XLSWRITER_TRUE;
}
/* }}} */
/* }}} */

lxw_row_col_options* default_row_col_options() {
size_t obj_size = sizeof(lxw_row_col_options);
void *obj = emalloc(obj_size);
memset(obj, 0, obj_size);
return obj;
}
14 changes: 7 additions & 7 deletions kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ void merge_cells(zend_string *range, zval *value, xls_resource_write_t *res, lxw
/*
* Set column format
*/
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format)
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options)
{
int error = worksheet_set_column(res->worksheet, COLS(ZSTR_VAL(range)), width, format);
int error = worksheet_set_column_opt(res->worksheet, COLS(ZSTR_VAL(range)), width, format, user_options);

// Cells that have been placed cannot be modified using optimization mode
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
Expand All @@ -333,14 +333,14 @@ void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw
/*
* Set row format
*/
void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format)
void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options)
{
char *rows = ZSTR_VAL(range);

if (strchr(rows, ':')) {
worksheet_set_rows(ROWS(rows), height, res, format);
worksheet_set_rows(ROWS(rows), height, res, format, user_options);
} else {
int error = worksheet_set_row(res->worksheet, ROW(rows), height, format);
int error = worksheet_set_row_opt(res->worksheet, ROW(rows), height, format, user_options);

// Cells that have been placed cannot be modified using optimization mode
WORKSHEET_INDEX_OUT_OF_CHANGE_IN_OPTIMIZE_EXCEPTION(res, error)
Expand All @@ -367,10 +367,10 @@ void validation(xls_resource_write_t *res, zend_string *range, lxw_data_validati
/*
* Set rows format
*/
void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format)
void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resource_write_t *res, lxw_format *format, lxw_row_col_options *user_options)
{
while (1) {
worksheet_set_row(res->worksheet, end, height, format);
worksheet_set_row_opt(res->worksheet, end, height, format, user_options);
if (end == start)
break;
end--;
Expand Down
Loading

0 comments on commit 33d8c20

Please sign in to comment.