Skip to content

Commit

Permalink
Feat: cell comment
Browse files Browse the repository at this point in the history
  • Loading branch information
viest committed Jun 8, 2020
1 parent 545d323 commit e59fc8f
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 49 deletions.
3 changes: 3 additions & 0 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ STATIC int _compare_defined_names(lxw_defined_name *a, lxw_defined_name *b);

STATIC void _prepare_drawings(lxw_workbook *self);
STATIC void _add_chart_cache_data(lxw_workbook *self);
STATIC void _prepare_vml(lxw_workbook *self);
STATIC void _prepare_defined_names(lxw_workbook *self);
STATIC void _populate_range(lxw_workbook *self, lxw_series_range *range);
STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *range);

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);
Expand All @@ -206,6 +208,7 @@ void freeze_panes(xls_resource_write_t *res, zend_long row, zend_long column);
void set_row(zend_string *range, double height, xls_resource_write_t *res, lxw_format *format);
void set_column(zend_string *range, double width, xls_resource_write_t *res, lxw_format *format);
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 url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *url, lxw_format *format);
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);
Expand Down
50 changes: 49 additions & 1 deletion kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ ZEND_BEGIN_ARG_INFO_EX(xls_insert_formula_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, format_handle)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_insert_comment_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, row)
ZEND_ARG_INFO(0, column)
ZEND_ARG_INFO(0, comment)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_show_comment_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(xls_auto_filter_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, range)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -705,7 +714,7 @@ PHP_METHOD(vtiful_xls, insertImage)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::insertImage(int $row, int $column, string $imagePath)
/** {{{ \Vtiful\Kernel\Excel::insertFormula(int $row, int $column, string $formula)
*/
PHP_METHOD(vtiful_xls, insertFormula)
{
Expand Down Expand Up @@ -739,6 +748,43 @@ PHP_METHOD(vtiful_xls, insertFormula)
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::insertComment(int $row, int $column, string $comment)
*/
PHP_METHOD(vtiful_xls, insertComment)
{
zend_string *comment = NULL;
zend_long row = 0, column = 0;

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_LONG(row)
Z_PARAM_LONG(column)
Z_PARAM_STR(comment)
ZEND_PARSE_PARAMETERS_END();

ZVAL_COPY(return_value, getThis());

xls_object *obj = Z_XLS_P(getThis());

WORKBOOK_NOT_INITIALIZED(obj);

comment_writer(comment, row, column, &obj->write_ptr);
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::showComment()
*/
PHP_METHOD(vtiful_xls, showComment)
{
ZVAL_COPY(return_value, getThis());

xls_object *obj = Z_XLS_P(getThis());

WORKBOOK_NOT_INITIALIZED(obj);

comment_show(&obj->write_ptr);
}
/* }}} */

/** {{{ \Vtiful\Kernel\Excel::autoFilter(int $rowStart, int $rowEnd, int $columnStart, int $columnEnd)
*/
PHP_METHOD(vtiful_xls, autoFilter)
Expand Down Expand Up @@ -1282,6 +1328,8 @@ zend_function_entry xls_methods[] = {
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)
Expand Down
92 changes: 91 additions & 1 deletion kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, z
worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, value_format);
}

/*
* Write the comment to the cell
*/
void comment_writer(zend_string *comment, zend_long row, zend_long columns, xls_resource_write_t *res)
{
int error = worksheet_write_comment(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, ZSTR_VAL(comment));

WORKSHEET_WRITER_EXCEPTION(error);
}

/*
* Show all comments
*/
void comment_show(xls_resource_write_t *res)
{
worksheet_show_comments(res->worksheet);
}

/*
* Add the autofilter.
*/
Expand Down Expand Up @@ -333,6 +351,9 @@ workbook_file(xls_resource_write_t *self)
worksheet->active = 1;
}

/* Prepare the worksheet VML elements such as comments. */
_prepare_vml(self->workbook);

/* Set the defined names for the worksheets such as Print Titles. */
_prepare_defined_names(self->workbook);

Expand All @@ -343,7 +364,7 @@ workbook_file(xls_resource_write_t *self)
_add_chart_cache_data(self->workbook);

/* Create a packager object to assemble sub-elements into a zip file. */
packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, 0);
packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, self->workbook->options.use_zip64);

/* If the packager fails it is generally due to a zip permission error. */
if (packager == NULL) {
Expand Down Expand Up @@ -375,6 +396,28 @@ workbook_file(xls_resource_write_t *self)
"Error = %s\n", self->workbook->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_PARAMETER_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_PARAMERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->workbook->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_BAD_ZIP_FILE then errno is set by zip. */
if (error == LXW_ERROR_ZIP_BAD_ZIP_FILE) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_BADZIPFILE error while creating xlsx file '%s'. "
"This may require the use_zip64 option for large files. "
"System error = %s\n", self->workbook->filename, strerror(errno));
}

/* If LXW_ERROR_ZIP_INTERNAL_ERROR then errno is set by zip. */
if (error == LXW_ERROR_ZIP_INTERNAL_ERROR) {
fprintf(stderr, "[ERROR] workbook_close(): "
"Zip ZIP_INTERNALERROR error while creating xlsx file '%s'. "
"System error = %s\n", self->workbook->filename, strerror(errno));
}

/* The next 2 error conditions don't set errno. */
if (error == LXW_ERROR_ZIP_FILE_ADD) {
fprintf(stderr, "[ERROR] workbook_close(): "
Expand All @@ -398,6 +441,53 @@ void _php_vtiful_xls_close(zend_resource *rsrc TSRMLS_DC)

}

/*
* Iterate through the worksheets and set up the VML objects.
*/

STATIC void
_prepare_vml(lxw_workbook *self)
{
lxw_worksheet *worksheet;
lxw_sheet *sheet;
uint32_t comment_id = 0;
uint32_t vml_drawing_id = 0;
uint32_t vml_data_id = 1;
uint32_t vml_shape_id = 1024;
uint32_t comment_count = 0;

STAILQ_FOREACH(sheet, self->sheets, list_pointers) {
if (sheet->is_chartsheet)
continue;
else
worksheet = sheet->u.worksheet;

if (!worksheet->has_vml && !worksheet->has_header_vml)
continue;

if (worksheet->has_vml) {
self->has_vml = LXW_TRUE;
if (worksheet->has_comments) {
self->comment_count += 1;
comment_id += 1;
self->has_comments = LXW_TRUE;
}

vml_drawing_id += 1;

comment_count = lxw_worksheet_prepare_vml_objects(worksheet,
vml_data_id,
vml_shape_id,
vml_drawing_id,
comment_id);

/* Each VML should start with a shape id incremented by 1024. */
vml_data_id += 1 * ((1024 + comment_count) / 1024);
vml_shape_id += 1024 * ((1024 + comment_count) / 1024);
}
}
}

/*
* Iterate through the worksheets and store any defined names used for print
* ranges or repeat rows/columns.
Expand Down
Loading

0 comments on commit e59fc8f

Please sign in to comment.