Skip to content

Commit

Permalink
format: fix identation and alignment property mismatch
Browse files Browse the repository at this point in the history
Fix issue where a horizontal alignment format was ignored if the
indentation was also set.
  • Loading branch information
jmcnamara committed Feb 3, 2024
1 parent d3c1443 commit b0c76b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,13 +1045,16 @@ _write_alignment(lxw_styles *self, lxw_format *format)

LXW_INIT_ATTRIBUTES();

/* Indent is only allowed for horizontal left, right and distributed. */
/* Indent is only allowed for some alignment properties. */
/* If it is defined for any other alignment or no alignment has been */
/* set then default to left alignment. */
if (format->indent
&& format->text_h_align != LXW_ALIGN_LEFT
&& format->text_h_align != LXW_ALIGN_RIGHT
&& format->text_h_align != LXW_ALIGN_DISTRIBUTED) {
&& format->text_h_align != LXW_ALIGN_DISTRIBUTED
&& format->text_v_align != LXW_ALIGN_VERTICAL_TOP
&& format->text_v_align != LXW_ALIGN_VERTICAL_BOTTOM
&& format->text_v_align != LXW_ALIGN_VERTICAL_DISTRIBUTED) {
format->text_h_align = LXW_ALIGN_LEFT;
}

Expand Down Expand Up @@ -1110,9 +1113,6 @@ _write_alignment(lxw_styles *self, lxw_format *format)
if (format->text_v_align == LXW_ALIGN_VERTICAL_DISTRIBUTED)
LXW_PUSH_ATTRIBUTES_STR("vertical", "distributed");

if (format->indent)
LXW_PUSH_ATTRIBUTES_INT("indent", format->indent);

/* Map rotation to Excel values. */
if (rotation) {
if (rotation == 270)
Expand All @@ -1123,6 +1123,9 @@ _write_alignment(lxw_styles *self, lxw_format *format)
LXW_PUSH_ATTRIBUTES_INT("textRotation", rotation);
}

if (format->indent)
LXW_PUSH_ATTRIBUTES_INT("indent", format->indent);

if (format->text_wrap)
LXW_PUSH_ATTRIBUTES_STR("wrapText", "1");

Expand Down
37 changes: 37 additions & 0 deletions test/functional/src/test_format24.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2024, John McNamara, [email protected]
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = workbook_new("test_format24.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

lxw_format *format1 = workbook_add_format(workbook);

/*
'rotation': 270,
'indent': 1,
'align': "center",
'valign': "top"
*/


format_set_rotation(format1, 270);
format_set_indent(format1, 1);
format_set_align(format1, LXW_ALIGN_CENTER);
format_set_align(format1, LXW_ALIGN_VERTICAL_TOP);

worksheet_set_row(worksheet, 0, 75, NULL);

worksheet_write_string(worksheet, 0, 0, "ABCD", format1);

return workbook_close(workbook);
}
3 changes: 3 additions & 0 deletions test/functional/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_format17(self):
def test_format18(self):
self.run_exe_test('test_format18')

def test_format24(self):
self.run_exe_test('test_format24')

def test_format50(self):
self.run_exe_test('test_format50')

Expand Down
Binary file added test/functional/xlsx_files/format24.xlsx
Binary file not shown.

0 comments on commit b0c76b3

Please sign in to comment.