File: conditional_format1.c

package info (click to toggle)
libxlsxwriter 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,864 kB
  • sloc: ansic: 65,601; python: 2,106; makefile: 693; perl: 229; sh: 224; xml: 168; cpp: 73; javascript: 5
file content (56 lines) | stat: -rw-r--r-- 2,053 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * 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-2025, 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);
    format_set_font_color(custom_format, LXW_COLOR_RED);

    /* Create a conditional format object. A static object would also work. */
    lxw_conditional_format *conditional_format =
        (lxw_conditional_format *)calloc(1, sizeof(lxw_conditional_format));

    /* Set the format type: a cell conditional: */
    conditional_format->type     = LXW_CONDITIONAL_TYPE_CELL;

    /* Set the criteria to use: */
    conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_LESS_THAN;

    /* 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);
}