SimpleXlsx  r0.19
 All Classes Namespaces Functions Variables Enumerations Pages
Worksheet.h
1 #ifndef __WORKSHEET_H__
2 #define __WORKSHEET_H__
3 
4 #include "../xmlwriter.h"
5 
6 int32_t _stprintf(TCHAR *buffer, const TCHAR *format, ...);
7 
8 namespace SimpleXlsx {
9 // ****************************************************************************
12 // ****************************************************************************
13 class CWorksheet {
14 public:
15  enum EPageOrientation {
16  PAGE_PORTRAIT = 0,
17  PAGE_LANDSCAPE
18  };
19 
20 private:
21  _tofstream *m_f;
22  xmlw::XmlStream *m_xmlStream;
23  std::vector<_tstring> m_calcChain;
24  std::map<_tstring, uint64_t>*m_sharedStrings;
25  std::vector<Comment> *m_comments;
26  std::vector<_tstring> m_mergedCells;
27  int32_t m_index;
28  _tstring m_temp_path;
29  _tstring m_title;
32  bool m_isOk;
34  int32_t m_row_index;
35  bool m_row_opened;
36  int32_t m_current_column;
37  int32_t m_offset_column;
38 
39  EPageOrientation m_page_orientation;
40 
41 public:
42  // @section SEC_CONSTRUCTOR Constructors
43  CWorksheet();
44  CWorksheet(uint32_t index, const _tstring& temp_path);
45  CWorksheet(uint32_t index, std::vector<ColumnWidth>& colHeights, const _tstring& temp_path);
46  CWorksheet(uint32_t index, uint32_t width, uint32_t height, const _tstring& temp_path);
47  CWorksheet(uint32_t index, uint32_t width, uint32_t height, std::vector<ColumnWidth>& colHeights, const _tstring& temp_path);
48  virtual ~CWorksheet();
49 
50  // @section SEC_INTERNAL Interclass internal interface methods
51  bool IsThereComment() const { return m_withComments; }
52  bool IsThereFormula() const { return m_withFormula; }
53  int32_t GetIndex() const { return m_index; }
54  void GetCalcChain(std::vector<_tstring>& chain) const{ chain = m_calcChain; }
55  void SetSharedStr(std::map<_tstring, uint64_t>* share) { m_sharedStrings = share; }
56  void SetComments(std::vector<Comment>* share) {m_comments = share; }
57 
58  // @section SEC_USER User interface
59  _tstring GetTitle() const { return m_title; }
60  void SetTitle(const _tstring& title) { m_title = title.length() > 31 ? title.substr(0, 31) : title; }
61 
62  void AddComment(const Comment& comment) {
63  if (m_comments != NULL) {
64  m_comments->push_back(comment);
65  m_comments->at(m_comments->size() - 1).sheetIndex = m_index;
66 
67  m_withComments = true;
68  }
69  }
70  void SetPageOrientation(EPageOrientation orient) { m_page_orientation = orient; }
71 
72  void BeginRow(uint32_t height = 0);
73  void AddCell();
74  void AddCell(const CellDataStr& data);
75  void AddCells(const std::vector<CellDataStr>& data);
76  void AddCell(const CellDataTime& data);
77  void AddCells(const std::vector<CellDataTime>& data);
78  void AddCell(const CellDataInt& data);
79  void AddCells(const std::vector<CellDataInt>& data);
80  void AddCell(const CellDataUInt& data);
81  void AddCells(const std::vector<CellDataUInt>& data);
82  void AddCell(const CellDataDbl& data);
83  void AddCells(const std::vector<CellDataDbl>& data);
84  void AddCell(const CellDataFlt& data);
85  void AddCells(const std::vector<CellDataFlt>& data);
86  void EndRow();
87 
88  void AddRow(const std::vector<CellDataStr>& data, uint32_t offset = 0, uint32_t height = 0);
89  void AddRow(const std::vector<CellDataTime>& data, uint32_t offset = 0, uint32_t height = 0);
90  void AddRow(const std::vector<CellDataInt>& data, uint32_t offset = 0, uint32_t height = 0);
91  void AddRow(const std::vector<CellDataUInt>& data, uint32_t offset = 0, uint32_t height = 0);
92  void AddRow(const std::vector<CellDataDbl>& data, uint32_t offset = 0, uint32_t height = 0);
93  void AddRow(const std::vector<CellDataFlt>& data, uint32_t offset = 0, uint32_t height = 0);
94 
95  void MergeCells(CellCoord cellFrom, CellCoord cellTo);
96  void GetCurrentCellCoord(CellCoord& currCell);
97 
98  bool Save();
99  bool IsOk() const { return m_isOk; }
100 
101  // @section General functions
102  static void GetCellCoord(CellCoord cell, TCHAR *szCoord);
103 
104 private:
105  void Init(uint32_t index, uint32_t frozenWidth, uint32_t frozenHeight, std::vector<ColumnWidth>& colHeights);
106  void AddFrozenPane(uint32_t width, uint32_t height);
107 
108  template<typename T>
109  void AddCellRoutine(T data, int32_t style);
110 
111  bool SaveCommentsRels();
112 };
113 
114 // ****************************************************************************
119 // ****************************************************************************
120 template<typename T>
121 void CWorksheet::AddCellRoutine(T data, int32_t style)
122 {
123  using namespace xmlw;
124 
125  TCHAR szCoord[15] = { 0 };
127  (*m_xmlStream) << tag(_T("c")) << attr(_T("r")) << szCoord;
128 
129  if (style != 0) { // default style is not necessary to sign explicitly
130  (*m_xmlStream) << attr(_T("s")) << style;
131  }
132 
133  (*m_xmlStream)
134  << tag(_T("v")) << chardata() << data << endtag()
135  << endtag(); // c
136 
138 }
139 
140 // ****************************************************************************
144 // ****************************************************************************
145 inline void CWorksheet::BeginRow(uint32_t height)
146 {
147  using namespace xmlw;
148 
149  if (m_row_opened)
150  (*m_xmlStream) << endtag(); // row
151 
152  (*m_xmlStream) << tag(_T("row")) << attr(_T("r")) << ++m_row_index << attr(_T("x14ac:dyDescent")) << 0.25;
153  if (height != 0) {
154  (*m_xmlStream) << attr(_T("ht")) << height << attr(_T("customHeight")) << 1;
155  }
156 
157  m_current_column = 0;
158  m_row_opened = true;
159 }
160 
161 // ****************************************************************************
165 // ****************************************************************************
166 inline void CWorksheet::AddCells(const std::vector<CellDataStr>& data)
167 {
168  for (size_t i = 0; i < data.size(); i++)
169  AddCell(data[i]);
170 }
171 
172 // ****************************************************************************
176 // ****************************************************************************
177 inline void CWorksheet::AddCells(const std::vector<CellDataTime>& data)
178 {
179  for (size_t i = 0; i < data.size(); i++)
180  AddCell(data[i]);
181 }
182 
183 // ****************************************************************************
186 // ****************************************************************************
187 inline void CWorksheet::EndRow()
188 {
189  using namespace xmlw;
190 
191  if (m_row_opened) {
192  (*m_xmlStream) << endtag(); // row
193  m_row_opened = false;
194  }
195 }
196 
197 } // namespace SimpleXlsx
198 
199 #endif // __WORKSHEET_H__