Add support for pixel sizing of rows and columns.

Added support for pixel sizing in worksheet_set_row() and
worksheet_set_column() via new functions called
worksheet_set_row_pixels() and worksheet_set_column_pixels().
This commit is contained in:
John McNamara 2021-04-15 19:53:01 +01:00
parent 2354d27646
commit e2795f601d
31 changed files with 1031 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View file

@ -69,12 +69,18 @@
* breaks is 1026. However, in practice it is actually 1023. */
#define LXW_BREAKS_MAX 1023
/** Default column width in Excel */
/** Default Excel column width in character units. */
#define LXW_DEF_COL_WIDTH (double)8.43
/** Default row height in Excel */
/** Default Excel column height in character units. */
#define LXW_DEF_ROW_HEIGHT (double)15.0
/** Default Excel column width in pixels. */
#define LXW_DEF_COL_WIDTH_PIXELS 64
/** Default Excel column height in pixels. */
#define LXW_DEF_ROW_HEIGHT_PIXELS 20
/** Gridline options using in `worksheet_gridlines()`. */
enum lxw_gridlines {
/** Hide screen and print gridlines. */
@ -2559,9 +2565,11 @@ lxw_error worksheet_write_comment_opt(lxw_worksheet *worksheet,
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
* @param row The zero indexed row number.
* @param height The row height.
* @param height The row height, in character units.
* @param format A pointer to a Format instance or NULL.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_row()` function is used to change the default
* properties of a row. The most common use for this function is to change the
* height of a row:
@ -2571,6 +2579,9 @@ lxw_error worksheet_write_comment_opt(lxw_worksheet *worksheet,
* worksheet_set_row(worksheet, 0, 20, NULL);
* @endcode
*
* The height is specified in character units. To specify the height in pixels
* use the `worksheet_set_row_pixels()` function.
*
* The other common use for `%worksheet_set_row()` is to set the a @ref
* format.h "Format" for all cells in the row:
*
@ -2618,6 +2629,8 @@ lxw_error worksheet_set_row(lxw_worksheet *worksheet,
* @param format A pointer to a Format instance or NULL.
* @param options Optional row parameters: hidden, level, collapsed.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_row_opt()` function is the same as
* `worksheet_set_row()` with an additional `options` parameter.
*
@ -2674,6 +2687,52 @@ lxw_error worksheet_set_row_opt(lxw_worksheet *worksheet,
lxw_format *format,
lxw_row_col_options *options);
/**
* @brief Set the properties for a row of cells, with the height in pixels.
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
* @param row The zero indexed row number.
* @param pixels The row height in pixels.
* @param format A pointer to a Format instance or NULL.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_row_pixels()` function is the same as the
* `worksheet_set_row()` function except that the height can be set in pixels
*
* @code
* // Set the height of Row 1 to 20 pixels.
* worksheet_set_row_pixels(worksheet, 0, 20, NULL);
* @endcode
*
* If you wish to set the format of a row without changing the height you can
* pass the default row height in pixels: #LXW_DEF_ROW_HEIGHT_PIXELS.
*/
lxw_error worksheet_set_row_pixels(lxw_worksheet *worksheet,
lxw_row_t row, uint32_t pixels,
lxw_format *format);
/**
* @brief Set the properties for a row of cells, with the height in pixels.
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
* @param row The zero indexed row number.
* @param pixels The row height in pixels.
* @param format A pointer to a Format instance or NULL.
* @param options Optional row parameters: hidden, level, collapsed.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_row_pixels_opt()` function is the same as the
* `worksheet_set_row_opt()` function except that the height can be set in
* pixels.
*
*/
lxw_error worksheet_set_row_pixels_opt(lxw_worksheet *worksheet,
lxw_row_t row,
uint32_t pixels,
lxw_format *format,
lxw_row_col_options *options);
/**
* @brief Set the properties for one or more columns of cells.
*
@ -2683,6 +2742,8 @@ lxw_error worksheet_set_row_opt(lxw_worksheet *worksheet,
* @param width The width of the column(s).
* @param format A pointer to a Format instance or NULL.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_column()` function can be used to change the default
* properties of a single column or a range of columns:
*
@ -2719,7 +2780,8 @@ lxw_error worksheet_set_row_opt(lxw_worksheet *worksheet,
* is 8.43 in the default font of Calibri 11. The actual relationship between
* a string width and a column width in Excel is complex. See the
* [following explanation of column widths](https://support.microsoft.com/en-us/kb/214123)
* from the Microsoft support documentation for more details.
* from the Microsoft support documentation for more details. To set the width
* in pixels use the `worksheet_set_column_pixels()` function.
*
* There is no way to specify "AutoFit" for a column in the Excel file
* format. This feature is only available at runtime from within Excel. It is
@ -2784,6 +2846,8 @@ lxw_error worksheet_set_column(lxw_worksheet *worksheet,
* @param format A pointer to a Format instance or NULL.
* @param options Optional row parameters: hidden, level, collapsed.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_column_opt()` function is the same as
* `worksheet_set_column()` with an additional `options` parameter.
*
@ -2823,6 +2887,62 @@ lxw_error worksheet_set_column_opt(lxw_worksheet *worksheet,
lxw_format *format,
lxw_row_col_options *options);
/**
* @brief Set the properties for one or more columns of cells, with the width
* in pixels.
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
* @param first_col The zero indexed first column.
* @param last_col The zero indexed last column.
* @param pixels The width of the column(s) in pixels.
* @param format A pointer to a Format instance or NULL.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_column_pixels()` function is the same as
* `worksheet_set_column()` function except that the width can be set in
* pixels:
*
* @code
* // Column width set to 75 pixels, the same as 10 character units.
* worksheet_set_column(worksheet, 5, 5, 75, NULL);
* @endcode
*
* @image html set_column_pixels.png
*
* If you wish to set the format of a column without changing the width you can
* pass the default column width in pixels: #LXW_DEF_COL_WIDTH_PIXELS.
*/
lxw_error worksheet_set_column_pixels(lxw_worksheet *worksheet,
lxw_col_t first_col,
lxw_col_t last_col,
uint32_t pixels, lxw_format *format);
/**
* @brief Set the properties for one or more columns of cells with options,
* with the width in pixels.
*
* @param worksheet Pointer to a lxw_worksheet instance to be updated.
* @param first_col The zero indexed first column.
* @param last_col The zero indexed last column.
* @param pixels The width of the column(s) in pixels.
* @param format A pointer to a Format instance or NULL.
* @param options Optional row parameters: hidden, level, collapsed.
*
* @return A #lxw_error code.
*
* The `%worksheet_set_column_pixels_opt()` function is the same as the
* `worksheet_set_column_opt()` function except that the width can be set in
* pixels.
*
*/
lxw_error worksheet_set_column_pixels_opt(lxw_worksheet *worksheet,
lxw_col_t first_col,
lxw_col_t last_col,
uint32_t pixels,
lxw_format *format,
lxw_row_col_options *options);
/**
* @brief Insert an image in a worksheet cell.
*
@ -4767,6 +4887,9 @@ STATIC void _worksheet_write_tab_color(lxw_worksheet *worksheet);
STATIC void _worksheet_write_sheet_protection(lxw_worksheet *worksheet,
lxw_protection_obj *protect);
STATIC void _worksheet_write_data_validations(lxw_worksheet *self);
STATIC double _pixels_to_height(double pixels);
STATIC double _pixels_to_width(double pixels);
#endif /* TESTING */
/* *INDENT-OFF* */

View file

@ -1365,6 +1365,32 @@ _validation_list_to_csv(char **list)
return str;
}
STATIC double
_pixels_to_width(double pixels)
{
double max_digit_width = 7.0;
double padding = 5.0;
double width;
if (pixels == LXW_DEF_COL_WIDTH_PIXELS)
width = LXW_DEF_COL_WIDTH;
else if (pixels <= 12.0)
width = pixels / (max_digit_width + padding);
else
width = (pixels - padding) / max_digit_width;
return width;
}
STATIC double
_pixels_to_height(double pixels)
{
if (pixels == LXW_DEF_ROW_HEIGHT_PIXELS)
return LXW_DEF_ROW_HEIGHT;
else
return pixels * 0.75;
}
/*****************************************************************************
*
* XML functions.
@ -7527,6 +7553,40 @@ worksheet_set_column(lxw_worksheet *self,
NULL);
}
/*
* Set the properties of a single column or a range of columns, with the
* width in pixels.
*/
lxw_error
worksheet_set_column_pixels(lxw_worksheet *self,
lxw_col_t firstcol,
lxw_col_t lastcol,
uint32_t pixels, lxw_format *format)
{
double width = _pixels_to_width(pixels);
return worksheet_set_column_opt(self, firstcol, lastcol, width, format,
NULL);
}
/*
* Set the properties of a single column or a range of columns with options,
* with the width in pixels.
*/
lxw_error
worksheet_set_column_pixels_opt(lxw_worksheet *self,
lxw_col_t firstcol,
lxw_col_t lastcol,
uint32_t pixels,
lxw_format *format,
lxw_row_col_options *user_options)
{
double width = _pixels_to_width(pixels);
return worksheet_set_column_opt(self, firstcol, lastcol, width, format,
user_options);
}
/*
* Set the properties of a row with options.
*/
@ -7599,6 +7659,34 @@ worksheet_set_row(lxw_worksheet *self,
return worksheet_set_row_opt(self, row_num, height, format, NULL);
}
/*
* Set the properties of a row, with the height in pixels.
*/
lxw_error
worksheet_set_row_pixels(lxw_worksheet *self,
lxw_row_t row_num, uint32_t pixels,
lxw_format *format)
{
double height = _pixels_to_height(pixels);
return worksheet_set_row_opt(self, row_num, height, format, NULL);
}
/*
* Set the properties of a row with options, with the height in pixels.
*/
lxw_error
worksheet_set_row_pixels_opt(lxw_worksheet *self,
lxw_row_t row_num,
uint32_t pixels,
lxw_format *format,
lxw_row_col_options *user_options)
{
double height = _pixels_to_height(pixels);
return worksheet_set_row_opt(self, row_num, height, format, user_options);
}
/*
* Merge a range of cells. The first cell should contain the data and the others
* should be blank. All cells should contain the same format.

View file

@ -0,0 +1,51 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column01.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_column(worksheet, COLS("A:A"), 0.08, NULL);
worksheet_set_column(worksheet, COLS("B:B"), 0.17, NULL);
worksheet_set_column(worksheet, COLS("C:C"), 0.25, NULL);
worksheet_set_column(worksheet, COLS("D:D"), 0.33, NULL);
worksheet_set_column(worksheet, COLS("E:E"), 0.42, NULL);
worksheet_set_column(worksheet, COLS("F:F"), 0.5, NULL);
worksheet_set_column(worksheet, COLS("G:G"), 0.58, NULL);
worksheet_set_column(worksheet, COLS("H:H"), 0.67, NULL);
worksheet_set_column(worksheet, COLS("I:I"), 0.75, NULL);
worksheet_set_column(worksheet, COLS("J:J"), 0.83, NULL);
worksheet_set_column(worksheet, COLS("K:K"), 0.92, NULL);
worksheet_set_column(worksheet, COLS("L:L"), 1, NULL);
worksheet_set_column(worksheet, COLS("M:M"), 1.14, NULL);
worksheet_set_column(worksheet, COLS("N:N"), 1.29, NULL);
worksheet_set_column(worksheet, COLS("O:O"), 1.43, NULL);
worksheet_set_column(worksheet, COLS("P:P"), 1.57, NULL);
worksheet_set_column(worksheet, COLS("Q:Q"), 1.71, NULL);
worksheet_set_column(worksheet, COLS("R:R"), 1.86, NULL);
worksheet_set_column(worksheet, COLS("S:S"), 2, NULL);
worksheet_set_column(worksheet, COLS("T:T"), 2.14, NULL);
worksheet_set_column(worksheet, COLS("U:U"), 2.29, NULL);
worksheet_set_column(worksheet, COLS("V:V"), 2.43, NULL);
worksheet_set_column(worksheet, COLS("W:W"), 2.57, NULL);
worksheet_set_column(worksheet, COLS("X:X"), 2.71, NULL);
worksheet_set_column(worksheet, COLS("Y:Y"), 2.86, NULL);
worksheet_set_column(worksheet, COLS("Z:Z"), 3, NULL);
worksheet_set_column(worksheet, COLS("AB:AB"), 8.57, NULL);
worksheet_set_column(worksheet, COLS("AC:AC"), 8.71, NULL);
worksheet_set_column(worksheet, COLS("AD:AD"), 8.86, NULL);
worksheet_set_column(worksheet, COLS("AE:AE"), 9, NULL);
worksheet_set_column(worksheet, COLS("AF:AF"), 9.14, NULL);
worksheet_set_column(worksheet, COLS("AG:AG"), 9.29, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,51 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column02.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_column(worksheet, COLS("A:A"), 0.083333333333333, NULL);
worksheet_set_column(worksheet, COLS("B:B"), 0.166666666666667, NULL);
worksheet_set_column(worksheet, COLS("C:C"), 0.250000000000000, NULL);
worksheet_set_column(worksheet, COLS("D:D"), 0.333333333333333, NULL);
worksheet_set_column(worksheet, COLS("E:E"), 0.416666666666667, NULL);
worksheet_set_column(worksheet, COLS("F:F"), 0.500000000000000, NULL);
worksheet_set_column(worksheet, COLS("G:G"), 0.583333333333333, NULL);
worksheet_set_column(worksheet, COLS("H:H"), 0.666666666666666, NULL);
worksheet_set_column(worksheet, COLS("I:I"), 0.750000000000000, NULL);
worksheet_set_column(worksheet, COLS("J:J"), 0.833333333333333, NULL);
worksheet_set_column(worksheet, COLS("K:K"), 0.916666666666666, NULL);
worksheet_set_column(worksheet, COLS("L:L"), 1.000000000000000, NULL);
worksheet_set_column(worksheet, COLS("M:M"), 1.142857142857140, NULL);
worksheet_set_column(worksheet, COLS("N:N"), 1.285714285714290, NULL);
worksheet_set_column(worksheet, COLS("O:O"), 1.428571428571430, NULL);
worksheet_set_column(worksheet, COLS("P:P"), 1.571428571428570, NULL);
worksheet_set_column(worksheet, COLS("Q:Q"), 1.714285714285710, NULL);
worksheet_set_column(worksheet, COLS("R:R"), 1.857142857142860, NULL);
worksheet_set_column(worksheet, COLS("S:S"), 2.000000000000000, NULL);
worksheet_set_column(worksheet, COLS("T:T"), 2.142857142857140, NULL);
worksheet_set_column(worksheet, COLS("U:U"), 2.285714285714290, NULL);
worksheet_set_column(worksheet, COLS("V:V"), 2.428571428571430, NULL);
worksheet_set_column(worksheet, COLS("W:W"), 2.571428571428570, NULL);
worksheet_set_column(worksheet, COLS("X:X"), 2.714285714285710, NULL);
worksheet_set_column(worksheet, COLS("Y:Y"), 2.857142857142860, NULL);
worksheet_set_column(worksheet, COLS("Z:Z"), 3.000000000000000, NULL);
worksheet_set_column(worksheet, COLS("AB:AB"), 8.571428571428570, NULL);
worksheet_set_column(worksheet, COLS("AC:AC"), 8.711428571428570, NULL);
worksheet_set_column(worksheet, COLS("AD:AD"), 8.857142857142860, NULL);
worksheet_set_column(worksheet, COLS("AE:AE"), 9.000000000000000, NULL);
worksheet_set_column(worksheet, COLS("AF:AF"), 9.142857142857140, NULL);
worksheet_set_column(worksheet, COLS("AG:AG"), 9.285714285714290, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,42 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column03.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", italic);
worksheet_write_string(worksheet, CELL("B1"), "Bar", bold);
worksheet_set_column(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, bold);
return workbook_close(workbook);
}

View file

@ -0,0 +1,49 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column04.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
lxw_format *bold_italic = workbook_add_format(workbook);
format_set_bold(bold_italic);
format_set_italic(bold_italic);
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", italic);
worksheet_write_string(worksheet, CELL("B1"), "Bar", bold);
worksheet_set_row(worksheet, 12, LXW_DEF_ROW_HEIGHT, italic);
worksheet_set_column(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, bold);
worksheet_write_blank(worksheet, CELL("F13"), bold_italic);
return workbook_close(workbook);
}

View file

@ -0,0 +1,60 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column05.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
lxw_format *bold_italic = workbook_add_format(workbook);
format_set_bold(bold_italic);
format_set_italic(bold_italic);
/* For testing, copy the randomly generated axis ids in the target file. */
chart->axis_id_1 = 68311296;
chart->axis_id_2 = 69198208;
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", italic);
worksheet_write_string(worksheet, CELL("B1"), "Bar", bold);
worksheet_set_row(worksheet, 12, LXW_DEF_ROW_HEIGHT, italic);
worksheet_set_column(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, bold);
worksheet_write_blank(worksheet, CELL("F13"), bold_italic);
chart_add_series(chart, NULL, "=Sheet1!$A$2:$A$6");
chart_add_series(chart, NULL, "=Sheet1!$B$2:$B$6");
chart_add_series(chart, NULL, "=Sheet1!$C$2:$C$6");
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View file

@ -0,0 +1,56 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column06.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
lxw_row_col_options options = {.hidden = LXW_TRUE};
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
/* For testing, copy the randomly generated axis ids in the target file. */
chart->axis_id_1 = 69197824;
chart->axis_id_2 = 69199360;
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("B1"), "Bar", italic);
worksheet_set_row_opt(worksheet, 12, LXW_DEF_ROW_HEIGHT, NULL, &options);
worksheet_set_column_opt(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, NULL, &options);
chart_add_series(chart, NULL, "=Sheet1!$A$2:$A$6");
chart_add_series(chart, NULL, "=Sheet1!$B$2:$B$6");
chart_add_series(chart, NULL, "=Sheet1!$C$2:$C$6");
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View file

@ -0,0 +1,51 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column07.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
lxw_format *bold_italic = workbook_add_format(workbook);
format_set_bold(bold_italic);
format_set_italic(bold_italic);
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", italic);
worksheet_write_string(worksheet, CELL("B1"), "Bar", bold);
worksheet_set_row(worksheet, 12, LXW_DEF_ROW_HEIGHT, italic);
worksheet_set_column(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, bold);
worksheet_write_blank(worksheet, CELL("F13"), bold_italic);
worksheet_insert_image(worksheet, CELL("E12"), "images/logo.png");
return workbook_close(workbook);
}

View file

@ -0,0 +1,47 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column08.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_row_col_options options = {.hidden = LXW_TRUE};
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("B1"), "Bar", italic);
worksheet_set_row_opt(worksheet, 12, LXW_DEF_ROW_HEIGHT, NULL, &options);
worksheet_set_column_opt(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH, NULL, &options);
worksheet_insert_image(worksheet, CELL("E12"), "images/logo.png");
return workbook_close(workbook);
}

View file

@ -0,0 +1,25 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column09.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_column(worksheet, COLS("A:A"), 100, NULL);
worksheet_set_column(worksheet, COLS("F:H"), 8, NULL);
worksheet_set_column(worksheet, COLS("C:D"), 12, NULL);
worksheet_set_column(worksheet, COLS("A:A"), 10, NULL);
worksheet_set_column(worksheet, COLS("XFD:XFD"), 5, NULL);
worksheet_set_column(worksheet, COLS("ZZ:ZZ"), 3, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,51 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column10.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_column_pixels(worksheet, COLS("A:A"), 1, NULL);
worksheet_set_column_pixels(worksheet, COLS("B:B"), 2, NULL);
worksheet_set_column_pixels(worksheet, COLS("C:C"), 3, NULL);
worksheet_set_column_pixels(worksheet, COLS("D:D"), 4, NULL);
worksheet_set_column_pixels(worksheet, COLS("E:E"), 5, NULL);
worksheet_set_column_pixels(worksheet, COLS("F:F"), 6, NULL);
worksheet_set_column_pixels(worksheet, COLS("G:G"), 7, NULL);
worksheet_set_column_pixels(worksheet, COLS("H:H"), 8, NULL);
worksheet_set_column_pixels(worksheet, COLS("I:I"), 9, NULL);
worksheet_set_column_pixels(worksheet, COLS("J:J"), 10, NULL);
worksheet_set_column_pixels(worksheet, COLS("K:K"), 11, NULL);
worksheet_set_column_pixels(worksheet, COLS("L:L"), 12, NULL);
worksheet_set_column_pixels(worksheet, COLS("M:M"), 13, NULL);
worksheet_set_column_pixels(worksheet, COLS("N:N"), 14, NULL);
worksheet_set_column_pixels(worksheet, COLS("O:O"), 15, NULL);
worksheet_set_column_pixels(worksheet, COLS("P:P"), 16, NULL);
worksheet_set_column_pixels(worksheet, COLS("Q:Q"), 17, NULL);
worksheet_set_column_pixels(worksheet, COLS("R:R"), 18, NULL);
worksheet_set_column_pixels(worksheet, COLS("S:S"), 19, NULL);
worksheet_set_column_pixels(worksheet, COLS("T:T"), 20, NULL);
worksheet_set_column_pixels(worksheet, COLS("U:U"), 21, NULL);
worksheet_set_column_pixels(worksheet, COLS("V:V"), 22, NULL);
worksheet_set_column_pixels(worksheet, COLS("W:W"), 23, NULL);
worksheet_set_column_pixels(worksheet, COLS("X:X"), 24, NULL);
worksheet_set_column_pixels(worksheet, COLS("Y:Y"), 25, NULL);
worksheet_set_column_pixels(worksheet, COLS("Z:Z"), 26, NULL);
worksheet_set_column_pixels(worksheet, COLS("AB:AB"), 65, NULL);
worksheet_set_column_pixels(worksheet, COLS("AC:AC"), 66, NULL);
worksheet_set_column_pixels(worksheet, COLS("AD:AD"), 67, NULL);
worksheet_set_column_pixels(worksheet, COLS("AE:AE"), 68, NULL);
worksheet_set_column_pixels(worksheet, COLS("AF:AF"), 69, NULL);
worksheet_set_column_pixels(worksheet, COLS("AG:AG"), 70, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,56 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_column11.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
lxw_row_col_options options = {.hidden = LXW_TRUE};
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
/* For testing, copy the randomly generated axis ids in the target file. */
chart->axis_id_1 = 69197824;
chart->axis_id_2 = 69199360;
uint8_t data[5][3] = {
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col], NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("B1"), "Bar", italic);
worksheet_set_row_pixels_opt(worksheet, 12, LXW_DEF_ROW_HEIGHT_PIXELS, NULL, &options);
worksheet_set_column_pixels_opt(worksheet, COLS("F:F"), LXW_DEF_COL_WIDTH_PIXELS, NULL, &options);
chart_add_series(chart, NULL, "=Sheet1!$A$2:$A$6");
chart_add_series(chart, NULL, "=Sheet1!$B$2:$B$6");
chart_add_series(chart, NULL, "=Sheet1!$C$2:$C$6");
worksheet_insert_chart(worksheet, CELL("E9"), chart);
return workbook_close(workbook);
}

View file

@ -0,0 +1,32 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_row01.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_row(worksheet, 0, 0.75, NULL);
worksheet_set_row(worksheet, 1, 1.50, NULL);
worksheet_set_row(worksheet, 2, 2.25, NULL);
worksheet_set_row(worksheet, 3, 3, NULL);
worksheet_set_row(worksheet, 11, 9, NULL);
worksheet_set_row(worksheet, 12, 9.75, NULL);
worksheet_set_row(worksheet, 13, 10.50, NULL);
worksheet_set_row(worksheet, 14, 11.25, NULL);
worksheet_set_row(worksheet, 18, 14.25, NULL);
worksheet_set_row(worksheet, 20, 15.75, NULL);
worksheet_set_row(worksheet, 21, 16.50, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,32 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_row02.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_row_pixels(worksheet, 0, 1, NULL);
worksheet_set_row_pixels(worksheet, 1, 2, NULL);
worksheet_set_row_pixels(worksheet, 2, 3, NULL);
worksheet_set_row_pixels(worksheet, 3, 4, NULL);
worksheet_set_row_pixels(worksheet, 11, 12, NULL);
worksheet_set_row_pixels(worksheet, 12, 13, NULL);
worksheet_set_row_pixels(worksheet, 13, 14, NULL);
worksheet_set_row_pixels(worksheet, 14, 15, NULL);
worksheet_set_row_pixels(worksheet, 18, 19, NULL);
worksheet_set_row_pixels(worksheet, 20, 21, NULL);
worksheet_set_row_pixels(worksheet, 21, 22, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,34 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_row03.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_row_col_options options = {.hidden = LXW_TRUE};
worksheet_set_row(worksheet, 0, 0.75, NULL);
worksheet_set_row(worksheet, 1, 1.50, NULL);
worksheet_set_row(worksheet, 2, 2.25, NULL);
worksheet_set_row(worksheet, 3, 3, NULL);
worksheet_set_row(worksheet, 11, 9, NULL);
worksheet_set_row(worksheet, 12, 9.75, NULL);
worksheet_set_row(worksheet, 13, 10.50, NULL);
worksheet_set_row(worksheet, 14, 11.25, NULL);
worksheet_set_row(worksheet, 18, 14.25, NULL);
worksheet_set_row_opt(worksheet, 20, 15.75, NULL, &options);
worksheet_set_row(worksheet, 21, 16.50, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,34 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_set_row04.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_row_col_options options = {.hidden = LXW_TRUE};
worksheet_set_row_pixels(worksheet, 0, 1, NULL);
worksheet_set_row_pixels(worksheet, 1, 2, NULL);
worksheet_set_row_pixels(worksheet, 2, 3, NULL);
worksheet_set_row_pixels(worksheet, 3, 4, NULL);
worksheet_set_row_pixels(worksheet, 11, 12, NULL);
worksheet_set_row_pixels(worksheet, 12, 13, NULL);
worksheet_set_row_pixels(worksheet, 13, 14, NULL);
worksheet_set_row_pixels(worksheet, 14, 15, NULL);
worksheet_set_row_pixels(worksheet, 18, 19, NULL);
worksheet_set_row_pixels_opt(worksheet, 20, 21, NULL, &options);
worksheet_set_row_pixels(worksheet, 21, 22, NULL);
return workbook_close(workbook);
}

View file

@ -0,0 +1,53 @@
###############################################################################
#
# Tests for libxlsxwriter.
#
# Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
#
import base_test_class
class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
"""
Test file created with libxlsxwriter against a file created by Excel.
"""
def test_set_column01(self):
self.run_exe_test('test_set_column01')
def test_set_column02(self):
self.run_exe_test('test_set_column02', 'set_column01.xlsx')
def test_set_column03(self):
self.run_exe_test('test_set_column03')
def test_set_column04(self):
self.run_exe_test('test_set_column04')
def test_set_column05(self):
self.run_exe_test('test_set_column05')
def test_set_column06(self):
# TODO. Fix span calculation for this condition.
self.ignore_elements = {'xl/worksheets/sheet1.xml': ['<row r="13"']}
self.run_exe_test('test_set_column06')
def test_set_column07(self):
self.run_exe_test('test_set_column07')
def test_set_column08(self):
# TODO. Fix span calculation for this condition.
self.ignore_elements = {'xl/worksheets/sheet1.xml': ['<row r="13"']}
self.run_exe_test('test_set_column08')
def test_set_column09(self):
self.run_exe_test('test_set_column09')
def test_set_column10(self):
self.run_exe_test('test_set_column10', 'set_column01.xlsx')
def test_set_column11(self):
# TODO. Fix span calculation for this condition.
self.ignore_elements = {'xl/worksheets/sheet1.xml': ['<row r="13"']}
self.run_exe_test('test_set_column11', 'set_column06.xlsx')

View file

@ -0,0 +1,28 @@
###############################################################################
#
# Tests for libxlsxwriter.
#
# Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
#
import base_test_class
class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
"""
Test file created with libxlsxwriter against a file created by Excel.
"""
def test_set_row01(self):
self.run_exe_test('test_set_row01')
def test_set_row02(self):
self.run_exe_test('test_set_row02', 'set_row01.xlsx')
def test_set_row03(self):
self.run_exe_test('test_set_row03')
def test_set_row04(self):
self.run_exe_test('test_set_row04', 'set_row03.xlsx')

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,64 @@
/*
* Tests for the lib_xlsx_writer library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "../ctest.h"
#include "../helper.h"
#include "../../../include/xlsxwriter/worksheet.h"
// Function used for testing.
uint32_t
width_to_pixels(double width)
{
double max_digit_width = 7.0;
double padding = 5.0;
double pixels;
if (width < 1.0)
pixels = (uint32_t) (width * (max_digit_width + padding) + 0.5);
else
pixels = (uint32_t) (width * max_digit_width + 0.5) + 5;
return pixels;
}
// Function used for testing.
uint32_t
height_to_pixels(double height)
{
return (uint32_t) (height / 0.75);
}
// Test the Worksheet _pixels_to_width() function.
CTEST(worksheet, pixel_to_width01) {
int pixels;
double got;
double exp;
for (pixels = 0; pixels <= 1790; pixels++) {
exp = pixels;
got = width_to_pixels(_pixels_to_width(pixels));
ASSERT_DOUBLE(exp, got);
}
}
// Test the Worksheet _pixels_to_height() function.
CTEST(worksheet, pixel_to_height01) {
int pixels;
double got;
double exp;
for (pixels = 0; pixels <= 545; pixels++) {
exp = pixels;
got = height_to_pixels(_pixels_to_height(pixels));
ASSERT_DOUBLE(exp, got);
}
}