admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of xlslib -- A multiplatform, C/C++ library
 * for dynamic generation of Excel(TM) files.
 *
 * Copyright 2004 Yeico S. A. de C. V. All Rights Reserved.
 * Copyright 2008-2013 David Hoerl All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#ifndef SHEETREC_H
#define SHEETREC_H
 
#include "../common/xlsys.h"
#include "../common/systype.h"
#include "../xlslib/colinfo.h"
#include "../xlslib/row.h"
 
// #include "common/xls_pshpack2.h"
 
namespace xlslib_core
{
    /*
     ***********************************
     *  worksheet class declaration
     ***********************************
     */
    class cell_t;
    class note_t;
    class range;
    typedef std::vector<xlslib_core::range* XLSLIB_DFLT_ALLOCATOR> RangeObj_Vect_t;
    typedef RangeObj_Vect_t::iterator RangeObj_Vect_Itor_t;
    class range_t;
    typedef std::vector<xlslib_core::range_t* XLSLIB_DFLT_ALLOCATOR> Range_Vect_t;
 
    class expression_node_t;
    class formula_t;
 
    typedef enum
    {
        SHEET_INIT,
        SHEET_BOF,
        SHEET_INDEX,
        SHEET_DFLT_ROW_HEIGHT,
        SHEET_COLINFO,
        SHEET_DFLT_COL_WIDTH,
        SHEET_DIMENSION,
        SHEET_ROWBLOCKS,
        SHEET_DRAWINGS,
        SHEET_NOTES,
        SHEET_MERGED,
        SHEET_WINDOW2,
        SHEET_PANE,
        SHEET_H_LINKS,
        SHEET_VALIDITY_HEADER,
        SHEET_VALIDITY_BODY,
        SHEET_EOF,
        SHEET_FINISH
    } SheetRecordDumpState_t;
 
    typedef struct rowblocksize_t
    {
        size_t rowandcell_size;
        size_t dbcell_size;
        size_t rows_sofar;
        size_t cells_sofar;
 
        unsigned32_t first_col;
        unsigned32_t last_col;
        unsigned32_t first_row;
        unsigned32_t last_row;
 
        // -------------------------------------
 
        rowblocksize_t() :
            rowandcell_size(0),
            dbcell_size(0),
            rows_sofar(0),
            cells_sofar(0),
            first_col((unsigned32_t)(-1)),
            last_col(0),
            first_row((unsigned32_t)(-1)),
            last_row(0)
        {
        }
 
        void reset(void)
        {
            rowandcell_size = 0;
            dbcell_size = 0;
            rows_sofar = 0;
            cells_sofar = 0;
 
            first_col = (unsigned32_t)(-1);
            last_col = 0;
            first_row = (unsigned32_t)(-1);
            last_row = 0;
        }
    } rowblocksize_t;
 
    typedef std::vector<xlslib_core::rowblocksize_t XLSLIB_DFLT_ALLOCATOR> RBSize_Vect_t;
    typedef RBSize_Vect_t::iterator RBSize_Vect_Itor_t;
 
    typedef enum
    {
        RB_INIT,
        RB_FIRST_ROW,
        RB_ROWS,
        RB_FIRSTCELL,
        RB_CELLS,
        RB_DBCELL,
        RB_FINISH
    } DumpRowBlocksState_t;
 
    typedef std::vector<size_t XLSLIB_DFLT_ALLOCATOR> CellOffsets_Vect_t;
    typedef CellOffsets_Vect_t::iterator CellOffsets_Vect_Itor_t;
 
    enum {
        DVAL_TYPE_ANY = 0x00,
        DVAL_TYPE_INTEGER = 0x01,
        DVAL_TYPE_DECIMAL = 0x02,
        DVAL_TYPE_LIST = 0x03,
        DVAL_TYPE_DATE = 0x04,
        DVAL_TYPE_TIME = 0x05,
        DVAL_TYPE_LENGTH = 0x06,
        DVAL_TYPE_FORMULA = 0x07
    };
 
    enum {
        DVAL_ERROR_STOP = 0x00,
        DVAL_ERROR_WARNING = 0x10,
        DVAL_ERROR_INFO = 0x20,
    };
 
    enum {
        DVAL_STRING_LIST_IN_FORMULA = 0x0080,
        DVAL_EMPTY_OK = 0x0100,
        DVAL_STRING_LIST_SUPPRESS_DROPDOWN = 0x0200,
        DVAL_SHOW_PROMPT_IF_SELECTED = 0x040000,
        DVAL_SHOW_ERROR_IF_INVALID   = 0x080000
    };
 
    enum {
        DVAL_OP_BETWEEN          = 0x000000,
        DVAL_OP_NOT_BETWEEN      = 0x100000,
        DVAL_OP_EQUAL            = 0x200000,
        DVAL_OP_NOT_EQUAL        = 0x300000,
        DVAL_OP_GREATER_THAN     = 0x400000,
        DVAL_OP_LESS_THAN        = 0x500000,
        DVAL_OP_GREATER_OR_EQUAL = 0x600000,
        DVAL_OP_LESS_OR_EQUAL    = 0x700000,
    };
 
    struct DataValidation
    {
        unsigned32_t                first_row;
        unsigned32_t                last_row;
        unsigned32_t                first_col;
        unsigned32_t                last_col;
        unsigned32_t                options;
        xlslib_strings::u16string    prompt_title;
        xlslib_strings::u16string    prompt_text;
        xlslib_strings::u16string    error_title;
        xlslib_strings::u16string    error_text;
        const formula_t                *cond1;
        const formula_t                *cond2;
    };
 
    typedef std::vector<xlslib_core::DataValidation * XLSLIB_DFLT_ALLOCATOR> DataValidationList_t;
    typedef DataValidationList_t::iterator DataValidationList_Itor_t;
 
    struct HyperLink
    {
        unsigned16_t row;
        unsigned16_t col;
        xlslib_strings::u16string url;
        xlslib_strings::u16string mark;
    };
    typedef std::vector<xlslib_core::HyperLink * XLSLIB_DFLT_ALLOCATOR> HyperLinkList_t;
    typedef HyperLinkList_t::iterator HyperLinkList_Itor_t;
 
    typedef std::vector<xlslib_core::CUnit* XLSLIB_DFLT_ALLOCATOR> ColInfo_t;
    typedef ColInfo_t::iterator ColInfo_Itor_t;
 
    typedef std::vector<xlslib_core::formula_t * XLSLIB_DFLT_ALLOCATOR> FormulaStackList_t;
    typedef FormulaStackList_t::iterator FormulaStackList_Itor_t;
 
    class worksheet : public CBiffSection
    {
        friend class workbook;
 
    private:
        CGlobalRecords& m_GlobalRecords;
        SheetRecordDumpState_t m_DumpState;
        CUnit* m_pCurrentData;
 
        Range_Vect_t m_MergedRanges;
        Range_Vect_Itor_t m_Current_Range;
 
        Colinfo_Set_t m_Colinfos;
        Colinfo_Set_Itor_t m_Current_Colinfo;
 
        RowHeight_Vect_t m_RowHeights;
        RowHeight_Vect_Itor_t m_Current_RowHeight;
 
        unsigned32_t minRow, minCol, maxRow, maxCol;
        unsigned16_t sheetIndex;
        unsigned32_t currentSPIDidx;
 
        Cell_Set_t m_Cells;
        Cell_Set_Itor_t    m_CurrentCell;              // Init this one in the RowBlocksDump INIT state
        Cell_Set_Itor_t    m_CurrentSizeCell;          // Init this one in the INIT state
        //bool m_CellsSorted;
        
        Cell_Set_t m_Notes;
        Cell_Set_Itor_t    m_CurrentNote;              // Init this one in the RowBlocksDump INIT state
        unsigned16_t m_CurrentNoteIdx;
 
        RangeObj_Vect_t    m_Ranges;
        RBSize_Vect_t m_RBSizes;
        RBSize_Vect_Itor_t m_Current_RBSize;
        bool m_SizesCalculated;
 
        DumpRowBlocksState_t m_DumpRBState;
        unsigned32_t m_RowCounter;
        unsigned32_t m_CellCounter;
        size_t m_DBCellOffset;
        size_t m_FirstRowOffset;
        CellOffsets_Vect_t m_CellOffsets;
 
        Cell_Set_Itor_t    m_Starting_RBCell;
 
        // cache a bit for speedups
        Cell_Set_Itor_t    cellIterHint;
        cell_t* cellHint;
        Cell_Set_Itor_t    noteIterHint;
        cell_t* noteHint;
 
        bool defRowsHidden;
        unsigned16_t defRowHeight;
        unsigned16_t defColWidth;
 
        unsigned16_t rowSplit;
        unsigned16_t colSplit;
 
        FormulaStackList_t m_FormulaStacks;
 
        DataValidationList_t m_DataValidations;
        DataValidationList_Itor_t m_CurrentDval;
 
        HyperLinkList_t    m_HyperLinks;
        HyperLinkList_Itor_t m_CurrentHlink;
 
    private:
        worksheet(CGlobalRecords& gRecords, unsigned16_t idx);
        ~worksheet();
 
        //void                    GetFirstLastRows(unsigned32_t* first_row, unsigned32_t* last_row);
        size_t                    GetNumRowBlocks(rowblocksize_t* rbsize_ref = NULL);
        bool                    GetRowBlockSizes(rowblocksize_t& rbsize);
        CUnit*                    RowBlocksDump(CDataStorage &datastore, const size_t offset);
        size_t                    EstimateNumBiffUnitsNeeded(void);
 
        void                    AddCell(cell_t* pcell);
        void                    AddNote(cell_t* pcell);
        CUnit*                    DumpData(CDataStorage &datastore, size_t offset, size_t writeLen /*, size_t &Last_BOF_offset*/);
 
        CUnit*                    MakeDataValidationHeader(CDataStorage& datastore, unsigned32_t dval_count);
        CUnit*                    MakeDataValidationEntry(CDataStorage& datastore, DataValidation* dval);
        CUnit*                    MakeHyperLink(CDataStorage& datastore, HyperLink* link);
 
    private:
        worksheet(const worksheet& that);
        worksheet& operator=(const worksheet& right);
 
    public:
        void                    MakeActive();   // makes this sheet come up first
        size_t                    NumCells() const { return m_Cells.size(); }
        unsigned16_t            GetIndex() const { return sheetIndex; }
 
        cell_t*                    FindCell(unsigned32_t row, unsigned32_t col) const;
        cell_t*                    FindCellOrMakeBlank(unsigned32_t row, unsigned32_t col);
 
        void                    GetFirstLastRowsAndColumns(unsigned32_t* first_row, unsigned32_t* last_row, unsigned32_t* first_col, unsigned32_t* last_col); /* [i_a] */
        void                     SplitWindow(unsigned16_t height, unsigned16_t width) { rowSplit = height; colSplit = width; }
 
#if 0
        static unsigned32_t        MakeSPID(unsigned32_t sheet_idx, unsigned32_t item) {
                                                                                        unsigned32_t val = (sheet_idx+1) * 1000;
                                                                                        val += startingSPID + item;
                                                                                        return val;
                                                                                    }
#endif
        // Cell operations
        void merge(unsigned32_t first_row, unsigned32_t first_col,
                   unsigned32_t last_row, unsigned32_t last_col);
        void colwidth(unsigned32_t col, unsigned16_t width, xf_t* pxformat = NULL);                // sets column widths to 1/256 x width of "0"
        void rowheight(unsigned32_t row, unsigned16_t heightInTwips, xf_t* pxformat = NULL);    // twips
 
        void defaultRowHeight(unsigned16_t width, bool hidden = false) { defRowHeight = width; defRowsHidden = hidden; } // sets column widths to 1/256 x width of "0"
        void defaultColwidth(unsigned16_t width) { defColWidth = width; } // in points (Excel uses twips, 1/20th of a point, but xlslib didn't)
 
        formula_t* formula_data();
 
        // Ranges
        range* rangegroup(unsigned32_t row1, unsigned32_t col1,
                          unsigned32_t row2, unsigned32_t col2);
        // Cells
        cell_t* blank(unsigned32_t row, unsigned32_t col,
                      xf_t* pxformat = NULL);
 
        cell_t* label(unsigned32_t row, unsigned32_t col,
                      const std::string& strlabel, xf_t* pxformat = NULL);
        cell_t* label(unsigned32_t row, unsigned32_t col,
                      const xlslib_strings::ustring& strlabel, xf_t* pxformat = NULL);
 
        cell_t* number(unsigned32_t row, unsigned32_t col,
                       double numval, format_number_t fmtval, xf_t* pxformat);  // Deprecated
        cell_t* number(unsigned32_t row, unsigned32_t col,
                       double numval, xf_t* pxformat = NULL);
        // 536870911 >= numval >= -536870912
        cell_t* number(unsigned32_t row, unsigned32_t col,
                       signed32_t numval, xf_t* pxformat = NULL);
        cell_t* number(unsigned32_t row, unsigned32_t col,
                       unsigned32_t numval, xf_t* pxformat = NULL);
 
        cell_t* boolean(unsigned32_t row, unsigned32_t col,
                        bool boolval, xf_t* pxformat = NULL);
 
        cell_t* error(unsigned32_t row, unsigned32_t col,
                      errcode_t errorcode, xf_t* pxformat = NULL);
 
        note_t* note(unsigned32_t row, unsigned32_t col,
                     const std::string& author, const std::string& remark, xf_t* pxformat = NULL);
        note_t* note(unsigned32_t row, unsigned32_t col,
                    const xlslib_strings::ustring& author,  const xlslib_strings::ustring& remark, xf_t* pxformat = NULL);
 
        cell_t* formula(unsigned32_t row, unsigned32_t col,
                        bool array_formula,
                        expression_node_t* expression_root, bool auto_destruct_expression_tree = false,
                        xf_t* pxformat = NULL);
        cell_t* formula(unsigned32_t row, unsigned32_t col,
                        expression_node_t* expression_root, bool auto_destruct_expression_tree = false,
                        xf_t* pxformat = NULL);
        cell_t* formula(unsigned32_t row, unsigned32_t col,
                        formula_t *formula, bool array_formula, xf_t* pxformat);
 
        void validate(const range_t *crange, unsigned32_t options,
                const formula_t *cond1 = NULL, const formula_t *cond2 = NULL,
                const std::string& promptTitle = std::string(), const std::string& promptText = std::string(),
                const std::string& errorTitle = std::string(), const std::string& errorText = std::string());
        void validate(const range_t *crange, unsigned32_t options,
                const formula_t *cond1 = NULL, const formula_t *cond2 = NULL,
                const xlslib_strings::ustring& promptTitle = xlslib_strings::ustring(), const xlslib_strings::ustring& promptText = xlslib_strings::ustring(),
                const xlslib_strings::ustring& errorTitle = xlslib_strings::ustring(), const xlslib_strings::ustring& errorText = xlslib_strings::ustring());
 
        // define a cell (label, number, etc) - apply proper url (http://blah.blah), possible text mark too (minus the '#')
        void hyperLink(const cell_t *cell, const std::string& url, const std::string& mark = std::string());
        void hyperLink(const cell_t *cell, const xlslib_strings::ustring& url, const xlslib_strings::ustring& mark = xlslib_strings::ustring());
    };
 
    typedef std::vector<xlslib_core::worksheet*> Sheets_Vector_t;
    typedef Sheets_Vector_t::iterator Sheets_Vector_Itor_t;
}
 
// #include "common/xls_poppack.h"
 
#endif