1
1
#include " ExcelProjectWriter.h"
2
2
3
3
#include < Logging.h>
4
+
4
5
#include < xlnt/workbook/workbook_view.hpp>
5
6
#include < xlnt/xlnt.hpp>
6
7
@@ -28,8 +29,8 @@ static int get_index_for_column(xlnt::worksheet& ws, const std::string& name) {
28
29
}
29
30
30
31
// ---------------------------------------------------------------------------
31
- static void set_value (xlnt::worksheet& ws, int column, int subject_id , const std::string& value) {
32
- ws.cell (xlnt::cell_reference (column, subject_id )).value (value);
32
+ static void set_value (xlnt::worksheet& ws, int column, int row , const std::string& value) {
33
+ ws.cell (xlnt::cell_reference (column, row )).value (value);
33
34
}
34
35
35
36
// ---------------------------------------------------------------------------
@@ -38,18 +39,31 @@ static void set_value(xlnt::worksheet& ws, const std::string& column_name, int s
38
39
set_value (ws, column_index, subject_id + 2 , value); // +1 for header, +1 for 1-indexed
39
40
}
40
41
42
+ // ---------------------------------------------------------------------------
43
+ static void set_subject_value (xlnt::worksheet& ws, int column_index, int subject_id, const std::string& value) {
44
+ set_value (ws, column_index, subject_id + 2 , value); // +1 for header, +1 for 1-indexed
45
+ }
46
+
41
47
// ---------------------------------------------------------------------------
42
48
static void store_subjects (Project& project, xlnt::workbook& wb) {
43
49
xlnt::worksheet ws = wb.sheet_by_index (0 );
44
50
ws.title (" data" );
45
51
52
+ // cache column indices for performance, get_index_for_column can be expensive
53
+ std::map<std::string, int > column_map;
54
+
46
55
auto subjects = project.get_subjects ();
47
56
for (int i = 0 ; i < subjects.size (); i++) {
48
57
auto subject = subjects[i];
49
58
auto map = ProjectUtils::convert_subject_to_map (&project, subject.get ());
50
59
51
60
for (auto & [key, value] : map) {
52
- set_value (ws, key, i, value);
61
+ if (column_map.find (key) == column_map.end ()) {
62
+ int column_index = get_index_for_column (ws, key);
63
+ column_map[key] = column_index;
64
+ }
65
+
66
+ set_subject_value (ws, column_map[key], i, value);
53
67
}
54
68
}
55
69
}
0 commit comments