Skip to content

Commit 6e18bba

Browse files
committed
Feat: Customize to enable or disable zip64, as WPS cannot open zip64 files
1 parent ce27075 commit 6e18bba

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

include/xlswriter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ typedef struct _vtiful_rich_string_object {
231231
Z_PARAM_LONG_EX(dest, is_null, 1, 0)
232232
#define Z_PARAM_ARRAY_OR_NULL(dest) \
233233
Z_PARAM_ARRAY_EX(dest, 1, 0)
234+
#define Z_PARAM_BOOL_OR_NULL(dest, is_null) \
235+
Z_PARAM_BOOL_EX(dest, is_null, 1, 0)
234236
#endif
235237

236238
static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {

kernel/excel.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ ZEND_END_ARG_INFO()
7676
ZEND_BEGIN_ARG_INFO_EX(xls_const_memory_arginfo, 0, 0, 1)
7777
ZEND_ARG_INFO(0, file_name)
7878
ZEND_ARG_INFO(0, sheet_name)
79+
ZEND_ARG_INFO(0, use_zip64)
7980
ZEND_END_ARG_INFO()
8081

8182
ZEND_BEGIN_ARG_INFO_EX(xls_file_add_sheet, 0, 0, 1)
@@ -507,13 +508,15 @@ PHP_METHOD(vtiful_xls, activateSheet)
507508
PHP_METHOD(vtiful_xls, constMemory)
508509
{
509510
char *sheet_name = NULL;
511+
zend_bool use_zip64 = LXW_TRUE;
510512
zval file_path, *dir_path = NULL;
511513
zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
512514

513-
ZEND_PARSE_PARAMETERS_START(1, 2)
515+
ZEND_PARSE_PARAMETERS_START(1, 3)
514516
Z_PARAM_STR(zs_file_name)
515517
Z_PARAM_OPTIONAL
516518
Z_PARAM_STR_OR_NULL(zs_sheet_name)
519+
Z_PARAM_BOOL_OR_NULL(use_zip64, _dummy)
517520
ZEND_PARSE_PARAMETERS_END();
518521

519522
ZVAL_COPY(return_value, getThis());
@@ -528,7 +531,7 @@ PHP_METHOD(vtiful_xls, constMemory)
528531
lxw_workbook_options options = {
529532
.constant_memory = LXW_TRUE,
530533
.tmpdir = NULL,
531-
.use_zip64 = LXW_TRUE
534+
.use_zip64 = use_zip64
532535
};
533536

534537
if(zs_sheet_name != NULL) {

kernel/write.c

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,25 +451,58 @@ void margins(xls_resource_write_t *res, double left, double right, double top, d
451451
lxw_error
452452
workbook_file(xls_resource_write_t *self)
453453
{
454+
lxw_sheet *sheet = NULL;
454455
lxw_worksheet *worksheet = NULL;
455456
lxw_packager *packager = NULL;
456457
lxw_error error = LXW_NO_ERROR;
458+
char codename[LXW_MAX_SHEETNAME_LENGTH] = { 0 };
457459

458460
/* Add a default worksheet if non have been added. */
459461
if (!self->workbook->num_sheets)
460462
workbook_add_worksheet(self->workbook, NULL);
461463

462464
/* Ensure that at least one worksheet has been selected. */
463465
if (self->workbook->active_sheet == 0) {
464-
worksheet = STAILQ_FIRST(self->workbook->worksheets);
465-
worksheet->selected = 1;
466-
worksheet->hidden = 0;
466+
sheet = STAILQ_FIRST(self->workbook->sheets);
467+
if (!sheet->is_chartsheet) {
468+
worksheet = sheet->u.worksheet;
469+
worksheet->selected = 1;
470+
worksheet->hidden = 0;
471+
}
467472
}
468473

469-
/* Set the active sheet. */
470-
STAILQ_FOREACH(worksheet, self->workbook->worksheets, list_pointers) {
474+
/* Set the active sheet and check if a metadata file is needed. */
475+
STAILQ_FOREACH(sheet, self->workbook->sheets, list_pointers) {
476+
if (sheet->is_chartsheet)
477+
continue;
478+
else
479+
worksheet = sheet->u.worksheet;
480+
471481
if (worksheet->index == self->workbook->active_sheet)
472482
worksheet->active = 1;
483+
484+
if (worksheet->has_dynamic_arrays)
485+
self->workbook->has_metadata = LXW_TRUE;
486+
}
487+
488+
/* Set workbook and worksheet VBA codenames if a macro has been added. */
489+
if (self->workbook->vba_project) {
490+
if (!self->workbook->vba_codename)
491+
workbook_set_vba_name(self->workbook, "ThisWorkbook");
492+
493+
STAILQ_FOREACH(sheet, self->workbook->sheets, list_pointers) {
494+
if (sheet->is_chartsheet)
495+
continue;
496+
else
497+
worksheet = sheet->u.worksheet;
498+
499+
if (!worksheet->vba_codename) {
500+
lxw_snprintf(codename, LXW_MAX_SHEETNAME_LENGTH, "Sheet%d",
501+
worksheet->index + 1);
502+
503+
worksheet_set_vba_name(worksheet, codename);
504+
}
505+
}
473506
}
474507

475508
/* Prepare the worksheet VML elements such as comments. */
@@ -485,13 +518,15 @@ workbook_file(xls_resource_write_t *self)
485518
_add_chart_cache_data(self->workbook);
486519

487520
/* Create a packager object to assemble sub-elements into a zip file. */
488-
packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, self->workbook->options.use_zip64);
521+
packager = lxw_packager_new(self->workbook->filename,
522+
self->workbook->options.tmpdir,
523+
self->workbook->options.use_zip64);
489524

490525
/* If the packager fails it is generally due to a zip permission error. */
491526
if (packager == NULL) {
492527
fprintf(stderr, "[ERROR] workbook_close(): "
493-
"Error creating '%s'. "
494-
"Error = %s\n", self->workbook->filename, strerror(errno));
528+
"Error creating '%s'. "
529+
"Error = %s\n", self->workbook->filename, strerror(errno));
495530

496531
error = LXW_ERROR_CREATING_XLSX_FILE;
497532
goto mem_error;
@@ -506,15 +541,15 @@ workbook_file(xls_resource_write_t *self)
506541
/* Error and non-error conditions fall through to the cleanup code. */
507542
if (error == LXW_ERROR_CREATING_TMPFILE) {
508543
fprintf(stderr, "[ERROR] workbook_close(): "
509-
"Error creating tmpfile(s) to assemble '%s'. "
510-
"Error = %s\n", self->workbook->filename, strerror(errno));
544+
"Error creating tmpfile(s) to assemble '%s'. "
545+
"Error = %s\n", self->workbook->filename, strerror(errno));
511546
}
512547

513548
/* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zlib. */
514549
if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
515550
fprintf(stderr, "[ERROR] workbook_close(): "
516-
"Zlib error while creating xlsx file '%s'. "
517-
"Error = %s\n", self->workbook->filename, strerror(errno));
551+
"Zlib error while creating xlsx file '%s'. "
552+
"Error = %s\n", self->workbook->filename, strerror(errno));
518553
}
519554

520555
/* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
@@ -548,11 +583,11 @@ workbook_file(xls_resource_write_t *self)
548583

549584
if (error == LXW_ERROR_ZIP_CLOSE) {
550585
fprintf(stderr, "[ERROR] workbook_close(): "
551-
"Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
586+
"Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
552587
}
553588

554589
mem_error:
555-
lxw_packager_free(packager);
590+
lxw_packager_free(packager);
556591

557592
return error;
558593
}

0 commit comments

Comments
 (0)