Skip to content

Commit 6f0f4ec

Browse files
authored
Merge pull request #411 from viest/dev
Feat: Customize to enable or disable zip64, as WPS cannot open zip64 …
2 parents 9d0300f + 6e18bba commit 6f0f4ec

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
@@ -457,25 +457,58 @@ void margins(xls_resource_write_t *res, double left, double right, double top, d
457457
lxw_error
458458
workbook_file(xls_resource_write_t *self)
459459
{
460+
lxw_sheet *sheet = NULL;
460461
lxw_worksheet *worksheet = NULL;
461462
lxw_packager *packager = NULL;
462463
lxw_error error = LXW_NO_ERROR;
464+
char codename[LXW_MAX_SHEETNAME_LENGTH] = { 0 };
463465

464466
/* Add a default worksheet if non have been added. */
465467
if (!self->workbook->num_sheets)
466468
workbook_add_worksheet(self->workbook, NULL);
467469

468470
/* Ensure that at least one worksheet has been selected. */
469471
if (self->workbook->active_sheet == 0) {
470-
worksheet = STAILQ_FIRST(self->workbook->worksheets);
471-
worksheet->selected = 1;
472-
worksheet->hidden = 0;
472+
sheet = STAILQ_FIRST(self->workbook->sheets);
473+
if (!sheet->is_chartsheet) {
474+
worksheet = sheet->u.worksheet;
475+
worksheet->selected = 1;
476+
worksheet->hidden = 0;
477+
}
473478
}
474479

475-
/* Set the active sheet. */
476-
STAILQ_FOREACH(worksheet, self->workbook->worksheets, list_pointers) {
480+
/* Set the active sheet and check if a metadata file is needed. */
481+
STAILQ_FOREACH(sheet, self->workbook->sheets, list_pointers) {
482+
if (sheet->is_chartsheet)
483+
continue;
484+
else
485+
worksheet = sheet->u.worksheet;
486+
477487
if (worksheet->index == self->workbook->active_sheet)
478488
worksheet->active = 1;
489+
490+
if (worksheet->has_dynamic_arrays)
491+
self->workbook->has_metadata = LXW_TRUE;
492+
}
493+
494+
/* Set workbook and worksheet VBA codenames if a macro has been added. */
495+
if (self->workbook->vba_project) {
496+
if (!self->workbook->vba_codename)
497+
workbook_set_vba_name(self->workbook, "ThisWorkbook");
498+
499+
STAILQ_FOREACH(sheet, self->workbook->sheets, list_pointers) {
500+
if (sheet->is_chartsheet)
501+
continue;
502+
else
503+
worksheet = sheet->u.worksheet;
504+
505+
if (!worksheet->vba_codename) {
506+
lxw_snprintf(codename, LXW_MAX_SHEETNAME_LENGTH, "Sheet%d",
507+
worksheet->index + 1);
508+
509+
worksheet_set_vba_name(worksheet, codename);
510+
}
511+
}
479512
}
480513

481514
/* Prepare the worksheet VML elements such as comments. */
@@ -491,13 +524,15 @@ workbook_file(xls_resource_write_t *self)
491524
_add_chart_cache_data(self->workbook);
492525

493526
/* Create a packager object to assemble sub-elements into a zip file. */
494-
packager = lxw_packager_new(self->workbook->filename, self->workbook->options.tmpdir, self->workbook->options.use_zip64);
527+
packager = lxw_packager_new(self->workbook->filename,
528+
self->workbook->options.tmpdir,
529+
self->workbook->options.use_zip64);
495530

496531
/* If the packager fails it is generally due to a zip permission error. */
497532
if (packager == NULL) {
498533
fprintf(stderr, "[ERROR] workbook_close(): "
499-
"Error creating '%s'. "
500-
"Error = %s\n", self->workbook->filename, strerror(errno));
534+
"Error creating '%s'. "
535+
"Error = %s\n", self->workbook->filename, strerror(errno));
501536

502537
error = LXW_ERROR_CREATING_XLSX_FILE;
503538
goto mem_error;
@@ -512,15 +547,15 @@ workbook_file(xls_resource_write_t *self)
512547
/* Error and non-error conditions fall through to the cleanup code. */
513548
if (error == LXW_ERROR_CREATING_TMPFILE) {
514549
fprintf(stderr, "[ERROR] workbook_close(): "
515-
"Error creating tmpfile(s) to assemble '%s'. "
516-
"Error = %s\n", self->workbook->filename, strerror(errno));
550+
"Error creating tmpfile(s) to assemble '%s'. "
551+
"Error = %s\n", self->workbook->filename, strerror(errno));
517552
}
518553

519554
/* If LXW_ERROR_ZIP_FILE_OPERATION then errno is set by zlib. */
520555
if (error == LXW_ERROR_ZIP_FILE_OPERATION) {
521556
fprintf(stderr, "[ERROR] workbook_close(): "
522-
"Zlib error while creating xlsx file '%s'. "
523-
"Error = %s\n", self->workbook->filename, strerror(errno));
557+
"Zlib error while creating xlsx file '%s'. "
558+
"Error = %s\n", self->workbook->filename, strerror(errno));
524559
}
525560

526561
/* If LXW_ERROR_ZIP_PARAMETER_ERROR then errno is set by zip. */
@@ -554,11 +589,11 @@ workbook_file(xls_resource_write_t *self)
554589

555590
if (error == LXW_ERROR_ZIP_CLOSE) {
556591
fprintf(stderr, "[ERROR] workbook_close(): "
557-
"Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
592+
"Zlib error closing xlsx file '%s'.\n", self->workbook->filename);
558593
}
559594

560595
mem_error:
561-
lxw_packager_free(packager);
596+
lxw_packager_free(packager);
562597

563598
return error;
564599
}

0 commit comments

Comments
 (0)