libxlsxwriter
Loading...
Searching...
No Matches
conditional_format1.c
<< data_validate.c conditional_format2.c >>

A simple example of how to add a conditional format a libxlsxwriter file. Conditional formatting allows you to apply a format to a cell or a range of cells based on certain criteria.

/*
* An a simple example of how to add conditional formatting to an
* libxlsxwriter file.
*
* See conditional_format.c for a more comprehensive example.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("conditional_format_simple.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some sample data. */
worksheet_write_number(worksheet, CELL("B1"), 34, NULL);
worksheet_write_number(worksheet, CELL("B2"), 32, NULL);
worksheet_write_number(worksheet, CELL("B3"), 31, NULL);
worksheet_write_number(worksheet, CELL("B4"), 35, NULL);
worksheet_write_number(worksheet, CELL("B5"), 36, NULL);
worksheet_write_number(worksheet, CELL("B6"), 30, NULL);
worksheet_write_number(worksheet, CELL("B7"), 38, NULL);
worksheet_write_number(worksheet, CELL("B8"), 38, NULL);
worksheet_write_number(worksheet, CELL("B9"), 32, NULL);
/* Add a format with red text. */
lxw_format *custom_format = workbook_add_format(workbook);
/* Create a conditional format object. A static object would also work. */
lxw_conditional_format *conditional_format =
/* Set the format type: a cell conditional: */
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
/* Set the criteria to use: */
/* Set the value to which the criteria will be applied: */
conditional_format->value = 33;
/* Set the format to use if the criteria/value applies: */
conditional_format->format = custom_format;
/* Now apply the format to data range. */
worksheet_conditional_format_range(worksheet, RANGE("B1:B9"), conditional_format);
/* Free the object and close the file. */
free(conditional_format);
return workbook_close(workbook);
}
void format_set_font_color(lxw_format *format, lxw_color_t color)
Set the color of the font used in the cell.
@ LXW_COLOR_RED
Definition: format.h:218
Worksheet conditional formatting options.
Definition: worksheet.h:1123
uint8_t criteria
Definition: worksheet.h:1136
lxw_format * format
Definition: worksheet.h:1159
double value
Definition: worksheet.h:1140
uint8_t type
Definition: worksheet.h:1128
Struct to represent the formatting properties of an Excel format.
Definition: format.h:359
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2108
#define RANGE(range)
Convert an Excel A1:B2 range into a (first_row, first_col, last_row, last_col) sequence.
Definition: utility.h:83
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:46
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
@ LXW_CONDITIONAL_CRITERIA_LESS_THAN
Definition: worksheet.h:318
lxw_error worksheet_write_number(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format)
Write a number to a worksheet cell.
lxw_error worksheet_conditional_format_range(lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col, lxw_conditional_format *conditional_format)
Add a conditional format to a worksheet range.
@ LXW_CONDITIONAL_TYPE_CELL
Definition: worksheet.h:238