1 /*
2 * Tests for the libxlsxwriter library.
3 *
4 * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
5 *
6 */
7
8 #include "../ctest.h"
9 #include "../helper.h"
10
11 #include "../../../include/xlsxwriter/worksheet.h"
12 #include "../../../include/xlsxwriter/table.h"
13
14 // Test assembling a complete Table file.
CTEST(worksheet,worksheet_table14)15 CTEST(worksheet, worksheet_table14) {
16
17 char* got;
18 char exp[] =
19 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
20 "<table xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" id=\"1\" name=\"Table1\" displayName=\"Table1\" ref=\"B3:G7\" totalsRowShown=\"0\">"
21 "<autoFilter ref=\"B3:G7\"/>"
22 "<tableColumns count=\"6\">"
23 "<tableColumn id=\"1\" name=\"Product\"/>"
24 "<tableColumn id=\"2\" name=\"Quarter 1\"/>"
25 "<tableColumn id=\"3\" name=\"Quarter 2\"/>"
26 "<tableColumn id=\"4\" name=\"Quarter 3\"/>"
27 "<tableColumn id=\"5\" name=\"Quarter 4\"/>"
28 "<tableColumn id=\"6\" name=\"Year\">"
29 "<calculatedColumnFormula>SUM(Table1[[#This Row],[Quarter 1]:[Quarter 4]])</calculatedColumnFormula>"
30 "</tableColumn>"
31 "</tableColumns>"
32 "<tableStyleInfo name=\"TableStyleMedium9\" showFirstColumn=\"0\" showLastColumn=\"0\" showRowStripes=\"1\" showColumnStripes=\"0\"/>"
33 "</table>";
34
35 FILE* testfile = lxw_tmpfile(NULL);
36 FILE* testfile2 = lxw_tmpfile(NULL);
37
38 lxw_worksheet *worksheet = lxw_worksheet_new(NULL);
39 worksheet->file = testfile2;
40 worksheet->sst = lxw_sst_new();
41
42 lxw_table *table = lxw_table_new();
43 table->file = testfile;
44
45 /* Set the table options. */
46 lxw_table_column col8_1 = {.header = "Product"};
47 lxw_table_column col8_2 = {.header = "Quarter 1"};
48 lxw_table_column col8_3 = {.header = "Quarter 2"};
49 lxw_table_column col8_4 = {.header = "Quarter 3"};
50 lxw_table_column col8_5 = {.header = "Quarter 4"};
51 lxw_table_column col8_6 = {.header = "Year",
52 .formula = "=SUM(Table1[@[Quarter 1]:[Quarter 4]])"};
53
54 lxw_table_column *columns8[] = {&col8_1, &col8_2, &col8_3, &col8_4, &col8_5, &col8_6, NULL};
55
56 lxw_table_options options8 = {.columns = columns8};
57
58 /* Add a table to the worksheet. */
59 worksheet_add_table(worksheet, RANGE("B3:G7"), &options8);
60
61 table->table_obj = STAILQ_FIRST(worksheet->table_objs);
62 table->table_obj->id = 1;
63
64 lxw_table_assemble_xml_file(table);
65
66 RUN_XLSX_STREQ_SHORT(exp, got);
67
68 lxw_worksheet_free(worksheet);
69 lxw_table_free(table);
70 }
71
72
73