Subversion Repositories eFlore/Applications.cel

Rev

Rev 1605 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1605 Rev 3473
1
<?php
1
<?php
2
/*
2
/*
3
*  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
3
*  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
4
*
4
*
5
*  The majority of this is _NOT_ my code.  I simply ported it from the
5
*  The majority of this is _NOT_ my code.  I simply ported it from the
6
*  PERL Spreadsheet::WriteExcel module.
6
*  PERL Spreadsheet::WriteExcel module.
7
*
7
*
8
*  The author of the Spreadsheet::WriteExcel module is John McNamara
8
*  The author of the Spreadsheet::WriteExcel module is John McNamara
9
*  <jmcnamara@cpan.org>
9
*  <jmcnamara@cpan.org>
10
*
10
*
11
*  I _DO_ maintain this code, and John McNamara has nothing to do with the
11
*  I _DO_ maintain this code, and John McNamara has nothing to do with the
12
*  porting of this code to PHP.  Any questions directly related to this
12
*  porting of this code to PHP.  Any questions directly related to this
13
*  class library should be directed to me.
13
*  class library should be directed to me.
14
*
14
*
15
*  License Information:
15
*  License Information:
16
*
16
*
17
*    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
17
*    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
18
*    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
18
*    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
19
*
19
*
20
*    This library is free software; you can redistribute it and/or
20
*    This library is free software; you can redistribute it and/or
21
*    modify it under the terms of the GNU Lesser General Public
21
*    modify it under the terms of the GNU Lesser General Public
22
*    License as published by the Free Software Foundation; either
22
*    License as published by the Free Software Foundation; either
23
*    version 2.1 of the License, or (at your option) any later version.
23
*    version 2.1 of the License, or (at your option) any later version.
24
*
24
*
25
*    This library is distributed in the hope that it will be useful,
25
*    This library is distributed in the hope that it will be useful,
26
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
26
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
27
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28
*    Lesser General Public License for more details.
28
*    Lesser General Public License for more details.
29
*
29
*
30
*    You should have received a copy of the GNU Lesser General Public
30
*    You should have received a copy of the GNU Lesser General Public
31
*    License along with this library; if not, write to the Free Software
31
*    License along with this library; if not, write to the Free Software
32
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
*/
33
*/
34
 
34
 
35
require_once 'Spreadsheet/Excel/Writer/Parser.php';
35
require_once 'Spreadsheet/Excel/Writer/Parser.php';
36
require_once 'Spreadsheet/Excel/Writer/BIFFwriter.php';
36
require_once 'Spreadsheet/Excel/Writer/BIFFwriter.php';
37
 
37
 
38
/**
38
/**
39
* Class for generating Excel Spreadsheets
39
* Class for generating Excel Spreadsheets
40
*
40
*
41
* @author   Xavier Noguer <xnoguer@rezebra.com>
41
* @author   Xavier Noguer <xnoguer@rezebra.com>
42
* @category FileFormats
42
* @category FileFormats
43
* @package  Spreadsheet_Excel_Writer
43
* @package  Spreadsheet_Excel_Writer
44
*/
44
*/
45
 
45
 
46
class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwriter
46
class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwriter
47
{
47
{
48
    /**
48
    /**
49
    * Name of the Worksheet
49
    * Name of the Worksheet
50
    * @var string
50
    * @var string
51
    */
51
    */
52
    var $name;
52
    var $name;
53
 
53
 
54
    /**
54
    /**
55
    * Index for the Worksheet
55
    * Index for the Worksheet
56
    * @var integer
56
    * @var integer
57
    */
57
    */
58
    var $index;
58
    var $index;
59
 
59
 
60
    /**
60
    /**
61
    * Reference to the (default) Format object for URLs
61
    * Reference to the (default) Format object for URLs
62
    * @var object Format
62
    * @var object Format
63
    */
63
    */
64
    var $_url_format;
64
    var $_url_format;
65
 
65
 
66
    /**
66
    /**
67
    * Reference to the parser used for parsing formulas
67
    * Reference to the parser used for parsing formulas
68
    * @var object Format
68
    * @var object Format
69
    */
69
    */
70
    var $_parser;
70
    var $_parser;
71
 
71
 
72
    /**
72
    /**
73
    * Filehandle to the temporary file for storing data
73
    * Filehandle to the temporary file for storing data
74
    * @var resource
74
    * @var resource
75
    */
75
    */
76
    var $_filehandle;
76
    var $_filehandle;
77
 
77
 
78
    /**
78
    /**
79
    * Boolean indicating if we are using a temporary file for storing data
79
    * Boolean indicating if we are using a temporary file for storing data
80
    * @var bool
80
    * @var bool
81
    */
81
    */
82
    var $_using_tmpfile;
82
    var $_using_tmpfile;
83
 
83
 
84
    /**
84
    /**
85
    * Maximum number of rows for an Excel spreadsheet (BIFF5)
85
    * Maximum number of rows for an Excel spreadsheet (BIFF5)
86
    * @var integer
86
    * @var integer
87
    */
87
    */
88
    var $_xls_rowmax;
88
    var $_xls_rowmax;
89
 
89
 
90
    /**
90
    /**
91
    * Maximum number of columns for an Excel spreadsheet (BIFF5)
91
    * Maximum number of columns for an Excel spreadsheet (BIFF5)
92
    * @var integer
92
    * @var integer
93
    */
93
    */
94
    var $_xls_colmax;
94
    var $_xls_colmax;
95
 
95
 
96
    /**
96
    /**
97
    * Maximum number of characters for a string (LABEL record in BIFF5)
97
    * Maximum number of characters for a string (LABEL record in BIFF5)
98
    * @var integer
98
    * @var integer
99
    */
99
    */
100
    var $_xls_strmax;
100
    var $_xls_strmax;
101
 
101
 
102
    /**
102
    /**
103
    * First row for the DIMENSIONS record
103
    * First row for the DIMENSIONS record
104
    * @var integer
104
    * @var integer
105
    * @see _storeDimensions()
105
    * @see _storeDimensions()
106
    */
106
    */
107
    var $_dim_rowmin;
107
    var $_dim_rowmin;
108
 
108
 
109
    /**
109
    /**
110
    * Last row for the DIMENSIONS record
110
    * Last row for the DIMENSIONS record
111
    * @var integer
111
    * @var integer
112
    * @see _storeDimensions()
112
    * @see _storeDimensions()
113
    */
113
    */
114
    var $_dim_rowmax;
114
    var $_dim_rowmax;
115
 
115
 
116
    /**
116
    /**
117
    * First column for the DIMENSIONS record
117
    * First column for the DIMENSIONS record
118
    * @var integer
118
    * @var integer
119
    * @see _storeDimensions()
119
    * @see _storeDimensions()
120
    */
120
    */
121
    var $_dim_colmin;
121
    var $_dim_colmin;
122
 
122
 
123
    /**
123
    /**
124
    * Last column for the DIMENSIONS record
124
    * Last column for the DIMENSIONS record
125
    * @var integer
125
    * @var integer
126
    * @see _storeDimensions()
126
    * @see _storeDimensions()
127
    */
127
    */
128
    var $_dim_colmax;
128
    var $_dim_colmax;
129
 
129
 
130
    /**
130
    /**
131
    * Array containing format information for columns
131
    * Array containing format information for columns
132
    * @var array
132
    * @var array
133
    */
133
    */
134
    var $_colinfo;
134
    var $_colinfo;
135
 
135
 
136
    /**
136
    /**
137
    * Array containing the selected area for the worksheet
137
    * Array containing the selected area for the worksheet
138
    * @var array
138
    * @var array
139
    */
139
    */
140
    var $_selection;
140
    var $_selection;
141
 
141
 
142
    /**
142
    /**
143
    * Array containing the panes for the worksheet
143
    * Array containing the panes for the worksheet
144
    * @var array
144
    * @var array
145
    */
145
    */
146
    var $_panes;
146
    var $_panes;
147
 
147
 
148
    /**
148
    /**
149
    * The active pane for the worksheet
149
    * The active pane for the worksheet
150
    * @var integer
150
    * @var integer
151
    */
151
    */
152
    var $_active_pane;
152
    var $_active_pane;
153
 
153
 
154
    /**
154
    /**
155
    * Bit specifying if panes are frozen
155
    * Bit specifying if panes are frozen
156
    * @var integer
156
    * @var integer
157
    */
157
    */
158
    var $_frozen;
158
    var $_frozen;
159
 
159
 
160
    /**
160
    /**
161
    * Bit specifying if the worksheet is selected
161
    * Bit specifying if the worksheet is selected
162
    * @var integer
162
    * @var integer
163
    */
163
    */
164
    var $selected;
164
    var $selected;
165
 
165
 
166
    /**
166
    /**
167
    * The paper size (for printing) (DOCUMENT!!!)
167
    * The paper size (for printing) (DOCUMENT!!!)
168
    * @var integer
168
    * @var integer
169
    */
169
    */
170
    var $_paper_size;
170
    var $_paper_size;
171
 
171
 
172
    /**
172
    /**
173
    * Bit specifying paper orientation (for printing). 0 => landscape, 1 => portrait
173
    * Bit specifying paper orientation (for printing). 0 => landscape, 1 => portrait
174
    * @var integer
174
    * @var integer
175
    */
175
    */
176
    var $_orientation;
176
    var $_orientation;
177
 
177
 
178
    /**
178
    /**
179
    * The page header caption
179
    * The page header caption
180
    * @var string
180
    * @var string
181
    */
181
    */
182
    var $_header;
182
    var $_header;
183
 
183
 
184
    /**
184
    /**
185
    * The page footer caption
185
    * The page footer caption
186
    * @var string
186
    * @var string
187
    */
187
    */
188
    var $_footer;
188
    var $_footer;
189
 
189
 
190
    /**
190
    /**
191
    * The horizontal centering value for the page
191
    * The horizontal centering value for the page
192
    * @var integer
192
    * @var integer
193
    */
193
    */
194
    var $_hcenter;
194
    var $_hcenter;
195
 
195
 
196
    /**
196
    /**
197
    * The vertical centering value for the page
197
    * The vertical centering value for the page
198
    * @var integer
198
    * @var integer
199
    */
199
    */
200
    var $_vcenter;
200
    var $_vcenter;
201
 
201
 
202
    /**
202
    /**
203
    * The margin for the header
203
    * The margin for the header
204
    * @var float
204
    * @var float
205
    */
205
    */
206
    var $_margin_head;
206
    var $_margin_head;
207
 
207
 
208
    /**
208
    /**
209
    * The margin for the footer
209
    * The margin for the footer
210
    * @var float
210
    * @var float
211
    */
211
    */
212
    var $_margin_foot;
212
    var $_margin_foot;
213
 
213
 
214
    /**
214
    /**
215
    * The left margin for the worksheet in inches
215
    * The left margin for the worksheet in inches
216
    * @var float
216
    * @var float
217
    */
217
    */
218
    var $_margin_left;
218
    var $_margin_left;
219
 
219
 
220
    /**
220
    /**
221
    * The right margin for the worksheet in inches
221
    * The right margin for the worksheet in inches
222
    * @var float
222
    * @var float
223
    */
223
    */
224
    var $_margin_right;
224
    var $_margin_right;
225
 
225
 
226
    /**
226
    /**
227
    * The top margin for the worksheet in inches
227
    * The top margin for the worksheet in inches
228
    * @var float
228
    * @var float
229
    */
229
    */
230
    var $_margin_top;
230
    var $_margin_top;
231
 
231
 
232
    /**
232
    /**
233
    * The bottom margin for the worksheet in inches
233
    * The bottom margin for the worksheet in inches
234
    * @var float
234
    * @var float
235
    */
235
    */
236
    var $_margin_bottom;
236
    var $_margin_bottom;
237
 
237
 
238
    /**
238
    /**
239
    * First row to reapeat on each printed page
239
    * First row to reapeat on each printed page
240
    * @var integer
240
    * @var integer
241
    */
241
    */
242
    var $title_rowmin;
242
    var $title_rowmin;
243
 
243
 
244
    /**
244
    /**
245
    * Last row to reapeat on each printed page
245
    * Last row to reapeat on each printed page
246
    * @var integer
246
    * @var integer
247
    */
247
    */
248
    var $title_rowmax;
248
    var $title_rowmax;
249
 
249
 
250
    /**
250
    /**
251
    * First column to reapeat on each printed page
251
    * First column to reapeat on each printed page
252
    * @var integer
252
    * @var integer
253
    */
253
    */
254
    var $title_colmin;
254
    var $title_colmin;
255
 
255
 
256
    /**
256
    /**
257
    * First row of the area to print
257
    * First row of the area to print
258
    * @var integer
258
    * @var integer
259
    */
259
    */
260
    var $print_rowmin;
260
    var $print_rowmin;
261
 
261
 
262
    /**
262
    /**
263
    * Last row to of the area to print
263
    * Last row to of the area to print
264
    * @var integer
264
    * @var integer
265
    */
265
    */
266
    var $print_rowmax;
266
    var $print_rowmax;
267
 
267
 
268
    /**
268
    /**
269
    * First column of the area to print
269
    * First column of the area to print
270
    * @var integer
270
    * @var integer
271
    */
271
    */
272
    var $print_colmin;
272
    var $print_colmin;
273
 
273
 
274
    /**
274
    /**
275
    * Last column of the area to print
275
    * Last column of the area to print
276
    * @var integer
276
    * @var integer
277
    */
277
    */
278
    var $print_colmax;
278
    var $print_colmax;
279
 
279
 
280
    /**
280
    /**
281
    * Whether to display RightToLeft.
281
    * Whether to display RightToLeft.
282
    * @var integer
282
    * @var integer
283
    */
283
    */
284
    var $_Arabic;
284
    var $_Arabic;
285
 
285
 
286
    /**
286
    /**
287
    * Whether to use outline.
287
    * Whether to use outline.
288
    * @var integer
288
    * @var integer
289
    */
289
    */
290
    var $_outline_on;
290
    var $_outline_on;
291
 
291
 
292
    /**
292
    /**
293
    * Auto outline styles.
293
    * Auto outline styles.
294
    * @var bool
294
    * @var bool
295
    */
295
    */
296
    var $_outline_style;
296
    var $_outline_style;
297
 
297
 
298
    /**
298
    /**
299
    * Whether to have outline summary below.
299
    * Whether to have outline summary below.
300
    * @var bool
300
    * @var bool
301
    */
301
    */
302
    var $_outline_below;
302
    var $_outline_below;
303
 
303
 
304
    /**
304
    /**
305
    * Whether to have outline summary at the right.
305
    * Whether to have outline summary at the right.
306
    * @var bool
306
    * @var bool
307
    */
307
    */
308
    var $_outline_right;
308
    var $_outline_right;
309
 
309
 
310
    /**
310
    /**
311
    * Outline row level.
311
    * Outline row level.
312
    * @var integer
312
    * @var integer
313
    */
313
    */
314
    var $_outline_row_level;
314
    var $_outline_row_level;
315
 
315
 
316
    /**
316
    /**
317
    * Whether to fit to page when printing or not.
317
    * Whether to fit to page when printing or not.
318
    * @var bool
318
    * @var bool
319
    */
319
    */
320
    var $_fit_page;
320
    var $_fit_page;
321
 
321
 
322
    /**
322
    /**
323
    * Number of pages to fit wide
323
    * Number of pages to fit wide
324
    * @var integer
324
    * @var integer
325
    */
325
    */
326
    var $_fit_width;
326
    var $_fit_width;
327
 
327
 
328
    /**
328
    /**
329
    * Number of pages to fit high
329
    * Number of pages to fit high
330
    * @var integer
330
    * @var integer
331
    */
331
    */
332
    var $_fit_height;
332
    var $_fit_height;
333
 
333
 
334
    /**
334
    /**
335
    * Reference to the total number of strings in the workbook
335
    * Reference to the total number of strings in the workbook
336
    * @var integer
336
    * @var integer
337
    */
337
    */
338
    var $_str_total;
338
    var $_str_total;
339
 
339
 
340
    /**
340
    /**
341
    * Reference to the number of unique strings in the workbook
341
    * Reference to the number of unique strings in the workbook
342
    * @var integer
342
    * @var integer
343
    */
343
    */
344
    var $_str_unique;
344
    var $_str_unique;
345
 
345
 
346
    /**
346
    /**
347
    * Reference to the array containing all the unique strings in the workbook
347
    * Reference to the array containing all the unique strings in the workbook
348
    * @var array
348
    * @var array
349
    */
349
    */
350
    var $_str_table;
350
    var $_str_table;
351
 
351
 
352
    /**
352
    /**
353
     * Number of merged cell ranges in actual record
353
     * Number of merged cell ranges in actual record
354
     * @var int $_merged_cells_counter
354
     * @var int $_merged_cells_counter
355
     */
355
     */
356
    var $_merged_cells_counter = 0;
356
    var $_merged_cells_counter = 0;
357
 
357
 
358
    /**
358
    /**
359
     * Number of actual mergedcells record
359
     * Number of actual mergedcells record
360
     * @var int $_merged_cells_record
360
     * @var int $_merged_cells_record
361
     */
361
     */
362
    var $_merged_cells_record = 0;
362
    var $_merged_cells_record = 0;
363
 
363
 
364
    /**
364
    /**
365
    * Merged cell ranges
365
    * Merged cell ranges
366
    * @var array
366
    * @var array
367
    */
367
    */
368
    var $_merged_ranges;
368
    var $_merged_ranges;
369
 
369
 
370
    /**
370
    /**
371
    * Charset encoding currently used when calling writeString()
371
    * Charset encoding currently used when calling writeString()
372
    * @var string
372
    * @var string
373
    */
373
    */
374
    var $_input_encoding;
374
    var $_input_encoding;
375
 
375
 
376
    /**
376
    /**
377
    * Constructor
377
    * Constructor
378
    *
378
    *
379
    * @param string  $name         The name of the new worksheet
379
    * @param string  $name         The name of the new worksheet
380
    * @param integer $index        The index of the new worksheet
380
    * @param integer $index        The index of the new worksheet
381
    * @param mixed   &$activesheet The current activesheet of the workbook we belong to
381
    * @param mixed   &$activesheet The current activesheet of the workbook we belong to
382
    * @param mixed   &$firstsheet  The first worksheet in the workbook we belong to
382
    * @param mixed   &$firstsheet  The first worksheet in the workbook we belong to
383
    * @param mixed   &$url_format  The default format for hyperlinks
383
    * @param mixed   &$url_format  The default format for hyperlinks
384
    * @param mixed   &$parser      The formula parser created for the Workbook
384
    * @param mixed   &$parser      The formula parser created for the Workbook
385
    * @param string  $tmp_dir      The path to the directory for temporary files
385
    * @param string  $tmp_dir      The path to the directory for temporary files
386
    * @access private
386
    * @access private
387
    */
387
    */
388
    function Spreadsheet_Excel_Writer_Worksheet($BIFF_version, $name,
388
    function __construct($BIFF_version, $name,
389
                                                $index, &$activesheet,
389
                                                $index, &$activesheet,
390
                                                &$firstsheet, &$str_total,
390
                                                &$firstsheet, &$str_total,
391
                                                &$str_unique, &$str_table,
391
                                                &$str_unique, &$str_table,
392
                                                &$url_format, &$parser,
392
                                                &$url_format, &$parser,
393
                                                $tmp_dir)
393
                                                $tmp_dir)
394
    {
394
    {
395
        // It needs to call its parent's constructor explicitly
395
        // It needs to call its parent's constructor explicitly
396
        $this->Spreadsheet_Excel_Writer_BIFFwriter();
396
        parent::__construct();
397
        $this->_BIFF_version   = $BIFF_version;
397
        $this->_BIFF_version   = $BIFF_version;
398
        $rowmax                = 65536; // 16384 in Excel 5
398
        $rowmax                = 65536; // 16384 in Excel 5
399
        $colmax                = 256;
399
        $colmax                = 256;
400
 
400
 
401
        $this->name            = $name;
401
        $this->name            = $name;
402
        $this->index           = $index;
402
        $this->index           = $index;
403
        $this->activesheet     = &$activesheet;
403
        $this->activesheet     = &$activesheet;
404
        $this->firstsheet      = &$firstsheet;
404
        $this->firstsheet      = &$firstsheet;
405
        $this->_str_total      = &$str_total;
405
        $this->_str_total      = &$str_total;
406
        $this->_str_unique     = &$str_unique;
406
        $this->_str_unique     = &$str_unique;
407
        $this->_str_table      = &$str_table;
407
        $this->_str_table      = &$str_table;
408
        $this->_url_format     = &$url_format;
408
        $this->_url_format     = &$url_format;
409
        $this->_parser         = &$parser;
409
        $this->_parser         = &$parser;
410
 
410
 
411
        //$this->ext_sheets      = array();
411
        //$this->ext_sheets      = array();
412
        $this->_filehandle     = '';
412
        $this->_filehandle     = '';
413
        $this->_using_tmpfile  = true;
413
        $this->_using_tmpfile  = true;
414
        //$this->fileclosed      = 0;
414
        //$this->fileclosed      = 0;
415
        //$this->offset          = 0;
415
        //$this->offset          = 0;
416
        $this->_xls_rowmax     = $rowmax;
416
        $this->_xls_rowmax     = $rowmax;
417
        $this->_xls_colmax     = $colmax;
417
        $this->_xls_colmax     = $colmax;
418
        $this->_xls_strmax     = 255;
418
        $this->_xls_strmax     = 255;
419
        $this->_dim_rowmin     = $rowmax + 1;
419
        $this->_dim_rowmin     = $rowmax + 1;
420
        $this->_dim_rowmax     = 0;
420
        $this->_dim_rowmax     = 0;
421
        $this->_dim_colmin     = $colmax + 1;
421
        $this->_dim_colmin     = $colmax + 1;
422
        $this->_dim_colmax     = 0;
422
        $this->_dim_colmax     = 0;
423
        $this->_colinfo        = array();
423
        $this->_colinfo        = array();
424
        $this->_selection      = array(0,0,0,0);
424
        $this->_selection      = array(0,0,0,0);
425
        $this->_panes          = array();
425
        $this->_panes          = array();
426
        $this->_active_pane    = 3;
426
        $this->_active_pane    = 3;
427
        $this->_frozen         = 0;
427
        $this->_frozen         = 0;
428
        $this->selected        = 0;
428
        $this->selected        = 0;
429
 
429
 
430
        $this->_paper_size      = 0x0;
430
        $this->_paper_size      = 0x0;
431
        $this->_orientation     = 0x1;
431
        $this->_orientation     = 0x1;
432
        $this->_header          = '';
432
        $this->_header          = '';
433
        $this->_footer          = '';
433
        $this->_footer          = '';
434
        $this->_hcenter         = 0;
434
        $this->_hcenter         = 0;
435
        $this->_vcenter         = 0;
435
        $this->_vcenter         = 0;
436
        $this->_margin_head     = 0.50;
436
        $this->_margin_head     = 0.50;
437
        $this->_margin_foot     = 0.50;
437
        $this->_margin_foot     = 0.50;
438
        $this->_margin_left     = 0.75;
438
        $this->_margin_left     = 0.75;
439
        $this->_margin_right    = 0.75;
439
        $this->_margin_right    = 0.75;
440
        $this->_margin_top      = 1.00;
440
        $this->_margin_top      = 1.00;
441
        $this->_margin_bottom   = 1.00;
441
        $this->_margin_bottom   = 1.00;
442
 
442
 
443
        $this->title_rowmin     = null;
443
        $this->title_rowmin     = null;
444
        $this->title_rowmax     = null;
444
        $this->title_rowmax     = null;
445
        $this->title_colmin     = null;
445
        $this->title_colmin     = null;
446
        $this->title_colmax     = null;
446
        $this->title_colmax     = null;
447
        $this->print_rowmin     = null;
447
        $this->print_rowmin     = null;
448
        $this->print_rowmax     = null;
448
        $this->print_rowmax     = null;
449
        $this->print_colmin     = null;
449
        $this->print_colmin     = null;
450
        $this->print_colmax     = null;
450
        $this->print_colmax     = null;
451
 
451
 
452
        $this->_print_gridlines  = 1;
452
        $this->_print_gridlines  = 1;
453
        $this->_screen_gridlines = 1;
453
        $this->_screen_gridlines = 1;
454
        $this->_print_headers    = 0;
454
        $this->_print_headers    = 0;
455
 
455
 
456
        $this->_fit_page        = 0;
456
        $this->_fit_page        = 0;
457
        $this->_fit_width       = 0;
457
        $this->_fit_width       = 0;
458
        $this->_fit_height      = 0;
458
        $this->_fit_height      = 0;
459
 
459
 
460
        $this->_hbreaks         = array();
460
        $this->_hbreaks         = array();
461
        $this->_vbreaks         = array();
461
        $this->_vbreaks         = array();
462
 
462
 
463
        $this->_protect         = 0;
463
        $this->_protect         = 0;
464
        $this->_password        = null;
464
        $this->_password        = null;
465
 
465
 
466
        $this->col_sizes        = array();
466
        $this->col_sizes        = array();
467
        $this->_row_sizes        = array();
467
        $this->_row_sizes        = array();
468
 
468
 
469
        $this->_zoom            = 100;
469
        $this->_zoom            = 100;
470
        $this->_print_scale     = 100;
470
        $this->_print_scale     = 100;
471
 
471
 
472
        $this->_outline_row_level = 0;
472
        $this->_outline_row_level = 0;
473
        $this->_outline_style     = 0;
473
        $this->_outline_style     = 0;
474
        $this->_outline_below     = 1;
474
        $this->_outline_below     = 1;
475
        $this->_outline_right     = 1;
475
        $this->_outline_right     = 1;
476
        $this->_outline_on        = 1;
476
        $this->_outline_on        = 1;
477
        $this->_Arabic            = 0;
477
        $this->_Arabic            = 0;
478
 
478
 
479
        $this->_merged_ranges     = array();
479
        $this->_merged_ranges     = array();
480
 
480
 
481
        $this->_input_encoding    = '';
481
        $this->_input_encoding    = '';
482
 
482
 
483
        $this->_dv                = array();
483
        $this->_dv                = array();
484
        
484
        
485
        $this->_tmp_dir           = $tmp_dir;
485
        $this->_tmp_dir           = $tmp_dir;
486
        $this->_tmp_file          = '';
486
        $this->_tmp_file          = '';
487
 
487
 
488
        $this->_initialize();
488
        $this->_initialize();
489
    }
489
    }
490
 
490
 
491
    /**
491
    /**
492
    * Open a tmp file to store the majority of the Worksheet data. If this fails,
492
    * Open a tmp file to store the majority of the Worksheet data. If this fails,
493
    * for example due to write permissions, store the data in memory. This can be
493
    * for example due to write permissions, store the data in memory. This can be
494
    * slow for large files.
494
    * slow for large files.
495
    *
495
    *
496
    * @access private
496
    * @access private
497
    */
497
    */
498
    function _initialize()
498
    function _initialize()
499
    {
499
    {
500
        if ($this->_using_tmpfile == false) {
500
        if ($this->_using_tmpfile == false) {
501
            return;
501
            return;
502
        }
502
        }
503
 
503
 
504
        if ($this->_tmp_dir === '' && ini_get('open_basedir') === true) {
504
        if ($this->_tmp_dir === '' && ini_get('open_basedir') === true) {
505
            // open_basedir restriction in effect - store data in memory
505
            // open_basedir restriction in effect - store data in memory
506
            // ToDo: Let the error actually have an effect somewhere
506
            // ToDo: Let the error actually have an effect somewhere
507
            $this->_using_tmpfile = false;  
507
            $this->_using_tmpfile = false;  
508
            return new PEAR_Error('Temp file could not be opened since open_basedir restriction in effect - please use setTmpDir() - using memory storage instead');
508
            return new PEAR_Error('Temp file could not be opened since open_basedir restriction in effect - please use setTmpDir() - using memory storage instead');
509
        }
509
        }
510
 
510
 
511
        // Open tmp file for storing Worksheet data
511
        // Open tmp file for storing Worksheet data
512
        if ($this->_tmp_dir === '') {
512
        if ($this->_tmp_dir === '') {
513
            $fh = tmpfile();
513
            $fh = tmpfile();
514
        } else {
514
        } else {
515
            // For people with open base dir restriction
515
            // For people with open base dir restriction
516
            $this->_tmp_file = tempnam($this->_tmp_dir, "Spreadsheet_Excel_Writer");
516
            $this->_tmp_file = tempnam($this->_tmp_dir, "Spreadsheet_Excel_Writer");
517
            $fh = @fopen($this->_tmp_file, "w+b");
517
            $fh = @fopen($this->_tmp_file, "w+b");
518
        }
518
        }
519
 
519
 
520
        if ($fh === false) {
520
        if ($fh === false) {
521
            // If tmpfile() fails store data in memory
521
            // If tmpfile() fails store data in memory
522
            $this->_using_tmpfile = false;
522
            $this->_using_tmpfile = false;
523
        } else {
523
        } else {
524
            // Store filehandle
524
            // Store filehandle
525
            $this->_filehandle = $fh;
525
            $this->_filehandle = $fh;
526
        }
526
        }
527
    }
527
    }
528
 
528
 
529
    /**
529
    /**
530
    * Add data to the beginning of the workbook (note the reverse order)
530
    * Add data to the beginning of the workbook (note the reverse order)
531
    * and to the end of the workbook.
531
    * and to the end of the workbook.
532
    *
532
    *
533
    * @access public
533
    * @access public
534
    * @see Spreadsheet_Excel_Writer_Workbook::storeWorkbook()
534
    * @see Spreadsheet_Excel_Writer_Workbook::storeWorkbook()
535
    * @param array $sheetnames The array of sheetnames from the Workbook this
535
    * @param array $sheetnames The array of sheetnames from the Workbook this
536
    *                          worksheet belongs to
536
    *                          worksheet belongs to
537
    */
537
    */
538
    function close($sheetnames)
538
    function close($sheetnames)
539
    {
539
    {
540
        $num_sheets = count($sheetnames);
540
        $num_sheets = count($sheetnames);
541
 
541
 
542
        /***********************************************
542
        /***********************************************
543
        * Prepend in reverse order!!
543
        * Prepend in reverse order!!
544
        */
544
        */
545
 
545
 
546
        // Prepend the sheet dimensions
546
        // Prepend the sheet dimensions
547
        $this->_storeDimensions();
547
        $this->_storeDimensions();
548
 
548
 
549
        // Prepend the sheet password
549
        // Prepend the sheet password
550
        $this->_storePassword();
550
        $this->_storePassword();
551
 
551
 
552
        // Prepend the sheet protection
552
        // Prepend the sheet protection
553
        $this->_storeProtect();
553
        $this->_storeProtect();
554
 
554
 
555
        // Prepend the page setup
555
        // Prepend the page setup
556
        $this->_storeSetup();
556
        $this->_storeSetup();
557
 
557
 
558
        /* FIXME: margins are actually appended */
558
        /* FIXME: margins are actually appended */
559
        // Prepend the bottom margin
559
        // Prepend the bottom margin
560
        $this->_storeMarginBottom();
560
        $this->_storeMarginBottom();
561
 
561
 
562
        // Prepend the top margin
562
        // Prepend the top margin
563
        $this->_storeMarginTop();
563
        $this->_storeMarginTop();
564
 
564
 
565
        // Prepend the right margin
565
        // Prepend the right margin
566
        $this->_storeMarginRight();
566
        $this->_storeMarginRight();
567
 
567
 
568
        // Prepend the left margin
568
        // Prepend the left margin
569
        $this->_storeMarginLeft();
569
        $this->_storeMarginLeft();
570
 
570
 
571
        // Prepend the page vertical centering
571
        // Prepend the page vertical centering
572
        $this->_storeVcenter();
572
        $this->_storeVcenter();
573
 
573
 
574
        // Prepend the page horizontal centering
574
        // Prepend the page horizontal centering
575
        $this->_storeHcenter();
575
        $this->_storeHcenter();
576
 
576
 
577
        // Prepend the page footer
577
        // Prepend the page footer
578
        $this->_storeFooter();
578
        $this->_storeFooter();
579
 
579
 
580
        // Prepend the page header
580
        // Prepend the page header
581
        $this->_storeHeader();
581
        $this->_storeHeader();
582
 
582
 
583
        // Prepend the vertical page breaks
583
        // Prepend the vertical page breaks
584
        $this->_storeVbreak();
584
        $this->_storeVbreak();
585
 
585
 
586
        // Prepend the horizontal page breaks
586
        // Prepend the horizontal page breaks
587
        $this->_storeHbreak();
587
        $this->_storeHbreak();
588
 
588
 
589
        // Prepend WSBOOL
589
        // Prepend WSBOOL
590
        $this->_storeWsbool();
590
        $this->_storeWsbool();
591
 
591
 
592
        // Prepend GRIDSET
592
        // Prepend GRIDSET
593
        $this->_storeGridset();
593
        $this->_storeGridset();
594
 
594
 
595
         //  Prepend GUTS
595
         //  Prepend GUTS
596
        if ($this->_BIFF_version == 0x0500) {
596
        if ($this->_BIFF_version == 0x0500) {
597
            $this->_storeGuts();
597
            $this->_storeGuts();
598
        }
598
        }
599
 
599
 
600
        // Prepend PRINTGRIDLINES
600
        // Prepend PRINTGRIDLINES
601
        $this->_storePrintGridlines();
601
        $this->_storePrintGridlines();
602
 
602
 
603
        // Prepend PRINTHEADERS
603
        // Prepend PRINTHEADERS
604
        $this->_storePrintHeaders();
604
        $this->_storePrintHeaders();
605
 
605
 
606
        // Prepend EXTERNSHEET references
606
        // Prepend EXTERNSHEET references
607
        if ($this->_BIFF_version == 0x0500) {
607
        if ($this->_BIFF_version == 0x0500) {
608
            for ($i = $num_sheets; $i > 0; $i--) {
608
            for ($i = $num_sheets; $i > 0; $i--) {
609
                $sheetname = $sheetnames[$i-1];
609
                $sheetname = $sheetnames[$i-1];
610
                $this->_storeExternsheet($sheetname);
610
                $this->_storeExternsheet($sheetname);
611
            }
611
            }
612
        }
612
        }
613
 
613
 
614
        // Prepend the EXTERNCOUNT of external references.
614
        // Prepend the EXTERNCOUNT of external references.
615
        if ($this->_BIFF_version == 0x0500) {
615
        if ($this->_BIFF_version == 0x0500) {
616
            $this->_storeExterncount($num_sheets);
616
            $this->_storeExterncount($num_sheets);
617
        }
617
        }
618
 
618
 
619
        // Prepend the COLINFO records if they exist
619
        // Prepend the COLINFO records if they exist
620
        if (!empty($this->_colinfo)) {
620
        if (!empty($this->_colinfo)) {
621
            $colcount = count($this->_colinfo);
621
            $colcount = count($this->_colinfo);
622
            for ($i = 0; $i < $colcount; $i++) {
622
            for ($i = 0; $i < $colcount; $i++) {
623
                $this->_storeColinfo($this->_colinfo[$i]);
623
                $this->_storeColinfo($this->_colinfo[$i]);
624
            }
624
            }
625
            $this->_storeDefcol();
625
            $this->_storeDefcol();
626
        }
626
        }
627
 
627
 
628
        // Prepend the BOF record
628
        // Prepend the BOF record
629
        $this->_storeBof(0x0010);
629
        $this->_storeBof(0x0010);
630
 
630
 
631
        /*
631
        /*
632
        * End of prepend. Read upwards from here.
632
        * End of prepend. Read upwards from here.
633
        ***********************************************/
633
        ***********************************************/
634
 
634
 
635
        // Append
635
        // Append
636
        $this->_storeWindow2();
636
        $this->_storeWindow2();
637
        $this->_storeZoom();
637
        $this->_storeZoom();
638
        if (!empty($this->_panes)) {
638
        if (!empty($this->_panes)) {
639
            $this->_storePanes($this->_panes);
639
            $this->_storePanes($this->_panes);
640
        }
640
        }
641
        $this->_storeSelection($this->_selection);
641
        $this->_storeSelection($this->_selection);
642
        $this->_storeMergedCells();
642
        $this->_storeMergedCells();
643
        /* TODO: add data validity */
643
        /* TODO: add data validity */
644
        /*if ($this->_BIFF_version == 0x0600) {
644
        /*if ($this->_BIFF_version == 0x0600) {
645
            $this->_storeDataValidity();
645
            $this->_storeDataValidity();
646
        }*/
646
        }*/
647
        $this->_storeEof();
647
        $this->_storeEof();
648
 
648
 
649
        if ( $this->_tmp_file != '' ) {
649
        if ( $this->_tmp_file != '' ) {
650
          if ( $this->_filehandle ) {
650
          if ( $this->_filehandle ) {
651
            fclose($this->_filehandle);
651
            fclose($this->_filehandle);
652
            $this->_filehandle = '';
652
            $this->_filehandle = '';
653
          }
653
          }
654
          @unlink($this->_tmp_file);
654
          @unlink($this->_tmp_file);
655
          $this->_tmp_file      = '';
655
          $this->_tmp_file      = '';
656
          $this->_using_tmpfile = true;
656
          $this->_using_tmpfile = true;
657
        }
657
        }
658
    }
658
    }
659
 
659
 
660
    /**
660
    /**
661
    * Retrieve the worksheet name.
661
    * Retrieve the worksheet name.
662
    * This is usefull when creating worksheets without a name.
662
    * This is usefull when creating worksheets without a name.
663
    *
663
    *
664
    * @access public
664
    * @access public
665
    * @return string The worksheet's name
665
    * @return string The worksheet's name
666
    */
666
    */
667
    function getName()
667
    function getName()
668
    {
668
    {
669
        return $this->name;
669
        return $this->name;
670
    }
670
    }
671
 
671
 
672
    /**
672
    /**
673
    * Retrieves data from memory in one chunk, or from disk in $buffer
673
    * Retrieves data from memory in one chunk, or from disk in $buffer
674
    * sized chunks.
674
    * sized chunks.
675
    *
675
    *
676
    * @return string The data
676
    * @return string The data
677
    */
677
    */
678
    function getData()
678
    function getData()
679
    {
679
    {
680
        $buffer = 4096;
680
        $buffer = 4096;
681
 
681
 
682
        // Return data stored in memory
682
        // Return data stored in memory
683
        if (isset($this->_data)) {
683
        if (isset($this->_data)) {
684
            $tmp   = $this->_data;
684
            $tmp   = $this->_data;
685
            unset($this->_data);
685
            unset($this->_data);
686
            $fh    = $this->_filehandle;
686
            $fh    = $this->_filehandle;
687
            if ($this->_using_tmpfile) {
687
            if ($this->_using_tmpfile) {
688
                fseek($fh, 0);
688
                fseek($fh, 0);
689
            }
689
            }
690
            return $tmp;
690
            return $tmp;
691
        }
691
        }
692
        // Return data stored on disk
692
        // Return data stored on disk
693
        if ($this->_using_tmpfile) {
693
        if ($this->_using_tmpfile) {
694
            if ($tmp = fread($this->_filehandle, $buffer)) {
694
            if ($tmp = fread($this->_filehandle, $buffer)) {
695
                return $tmp;
695
                return $tmp;
696
            }
696
            }
697
        }
697
        }
698
 
698
 
699
        // No data to return
699
        // No data to return
700
        return '';
700
        return '';
701
    }
701
    }
702
 
702
 
703
    /**
703
    /**
704
    * Sets a merged cell range
704
    * Sets a merged cell range
705
    *
705
    *
706
    * @access public
706
    * @access public
707
    * @param integer $first_row First row of the area to merge
707
    * @param integer $first_row First row of the area to merge
708
    * @param integer $first_col First column of the area to merge
708
    * @param integer $first_col First column of the area to merge
709
    * @param integer $last_row  Last row of the area to merge
709
    * @param integer $last_row  Last row of the area to merge
710
    * @param integer $last_col  Last column of the area to merge
710
    * @param integer $last_col  Last column of the area to merge
711
    */
711
    */
712
    function setMerge($first_row, $first_col, $last_row, $last_col)
712
    function setMerge($first_row, $first_col, $last_row, $last_col)
713
    {
713
    {
714
        if (($last_row < $first_row) || ($last_col < $first_col)) {
714
        if (($last_row < $first_row) || ($last_col < $first_col)) {
715
            return;
715
            return;
716
        }
716
        }
717
 
717
 
718
        $max_record_ranges = floor(($this->_limit - 6) / 8);
718
        $max_record_ranges = floor(($this->_limit - 6) / 8);
719
        if($this->_merged_cells_counter >= $max_record_ranges)
719
        if($this->_merged_cells_counter >= $max_record_ranges)
720
          {
720
          {
721
            $this->_merged_cells_record++;
721
            $this->_merged_cells_record++;
722
            $this->_merged_cells_counter = 0;
722
            $this->_merged_cells_counter = 0;
723
          }
723
          }
724
 
724
 
725
        // don't check rowmin, rowmax, etc... because we don't know when this
725
        // don't check rowmin, rowmax, etc... because we don't know when this
726
        // is going to be called
726
        // is going to be called
727
        $this->_merged_ranges[$this->_merged_cells_record][] = array($first_row, $first_col, $last_row, $last_col);
727
        $this->_merged_ranges[$this->_merged_cells_record][] = array($first_row, $first_col, $last_row, $last_col);
728
        $this->_merged_cells_counter++;
728
        $this->_merged_cells_counter++;
729
    }
729
    }
730
 
730
 
731
    /**
731
    /**
732
    * Set this worksheet as a selected worksheet,
732
    * Set this worksheet as a selected worksheet,
733
    * i.e. the worksheet has its tab highlighted.
733
    * i.e. the worksheet has its tab highlighted.
734
    *
734
    *
735
    * @access public
735
    * @access public
736
    */
736
    */
737
    function select()
737
    function select()
738
    {
738
    {
739
        $this->selected = 1;
739
        $this->selected = 1;
740
    }
740
    }
741
 
741
 
742
    /**
742
    /**
743
    * Set this worksheet as the active worksheet,
743
    * Set this worksheet as the active worksheet,
744
    * i.e. the worksheet that is displayed when the workbook is opened.
744
    * i.e. the worksheet that is displayed when the workbook is opened.
745
    * Also set it as selected.
745
    * Also set it as selected.
746
    *
746
    *
747
    * @access public
747
    * @access public
748
    */
748
    */
749
    function activate()
749
    function activate()
750
    {
750
    {
751
        $this->selected = 1;
751
        $this->selected = 1;
752
        $this->activesheet = $this->index;
752
        $this->activesheet = $this->index;
753
    }
753
    }
754
 
754
 
755
    /**
755
    /**
756
    * Set this worksheet as the first visible sheet.
756
    * Set this worksheet as the first visible sheet.
757
    * This is necessary when there are a large number of worksheets and the
757
    * This is necessary when there are a large number of worksheets and the
758
    * activated worksheet is not visible on the screen.
758
    * activated worksheet is not visible on the screen.
759
    *
759
    *
760
    * @access public
760
    * @access public
761
    */
761
    */
762
    function setFirstSheet()
762
    function setFirstSheet()
763
    {
763
    {
764
        $this->firstsheet = $this->index;
764
        $this->firstsheet = $this->index;
765
    }
765
    }
766
 
766
 
767
    /**
767
    /**
768
    * Set the worksheet protection flag
768
    * Set the worksheet protection flag
769
    * to prevent accidental modification and to
769
    * to prevent accidental modification and to
770
    * hide formulas if the locked and hidden format properties have been set.
770
    * hide formulas if the locked and hidden format properties have been set.
771
    *
771
    *
772
    * @access public
772
    * @access public
773
    * @param string $password The password to use for protecting the sheet.
773
    * @param string $password The password to use for protecting the sheet.
774
    */
774
    */
775
    function protect($password)
775
    function protect($password)
776
    {
776
    {
777
        $this->_protect   = 1;
777
        $this->_protect   = 1;
778
        $this->_password  = $this->_encodePassword($password);
778
        $this->_password  = $this->_encodePassword($password);
779
    }
779
    }
780
 
780
 
781
    /**
781
    /**
782
    * Set the width of a single column or a range of columns.
782
    * Set the width of a single column or a range of columns.
783
    *
783
    *
784
    * @access public
784
    * @access public
785
    * @param integer $firstcol first column on the range
785
    * @param integer $firstcol first column on the range
786
    * @param integer $lastcol  last column on the range
786
    * @param integer $lastcol  last column on the range
787
    * @param integer $width    width to set
787
    * @param integer $width    width to set
788
    * @param mixed   $format   The optional XF format to apply to the columns
788
    * @param mixed   $format   The optional XF format to apply to the columns
789
    * @param integer $hidden   The optional hidden atribute
789
    * @param integer $hidden   The optional hidden atribute
790
    * @param integer $level    The optional outline level
790
    * @param integer $level    The optional outline level
791
    */
791
    */
792
    function setColumn($firstcol, $lastcol, $width, $format = null, $hidden = 0, $level = 0) 
792
    function setColumn($firstcol, $lastcol, $width, $format = null, $hidden = 0, $level = 0) 
793
    { // added by Dan Lynn <dan@spiderweblabs.com) on 2006-12-06 
793
    { // added by Dan Lynn <dan@spiderweblabs.com) on 2006-12-06 
794
        // look for any ranges this might overlap and remove, size or split where necessary 
794
        // look for any ranges this might overlap and remove, size or split where necessary 
795
        foreach ($this->_colinfo as $key => $colinfo) 
795
        foreach ($this->_colinfo as $key => $colinfo) 
796
        {
796
        {
797
            $existing_start = $colinfo[0]; $existing_end = $colinfo[1]; 
797
            $existing_start = $colinfo[0]; $existing_end = $colinfo[1]; 
798
            // if the new range starts within another range 
798
            // if the new range starts within another range 
799
            if ($firstcol > $existing_start && $firstcol < $existing_end) 
799
            if ($firstcol > $existing_start && $firstcol < $existing_end) 
800
            { // trim the existing range to the beginning of the new range 
800
            { // trim the existing range to the beginning of the new range 
801
                $this->_colinfo[$key][1] = $firstcol - 1; 
801
                $this->_colinfo[$key][1] = $firstcol - 1; 
802
                // if the new range lies WITHIN the existing range 
802
                // if the new range lies WITHIN the existing range 
803
                if ($lastcol < $existing_end) 
803
                if ($lastcol < $existing_end) 
804
                { // split the existing range by adding a range after our new range 
804
                { // split the existing range by adding a range after our new range 
805
                    $this->_colinfo[] = array($lastcol+1, $existing_end, $colinfo[2], &$colinfo[3], $colinfo[4], $colinfo[5]); 
805
                    $this->_colinfo[] = array($lastcol+1, $existing_end, $colinfo[2], &$colinfo[3], $colinfo[4], $colinfo[5]); 
806
                } 
806
                } 
807
            } // if the new range ends inside an existing range 
807
            } // if the new range ends inside an existing range 
808
            elseif ($lastcol > $existing_start && $lastcol < $existing_end) 
808
            elseif ($lastcol > $existing_start && $lastcol < $existing_end) 
809
            { // trim the existing range to the end of the new range 
809
            { // trim the existing range to the end of the new range 
810
                $this->_colinfo[$key][0] = $lastcol + 1; 
810
                $this->_colinfo[$key][0] = $lastcol + 1; 
811
            } // if the new range completely overlaps the existing range 
811
            } // if the new range completely overlaps the existing range 
812
            elseif ($firstcol <= $existing_start && $lastcol >= $existing_end) 
812
            elseif ($firstcol <= $existing_start && $lastcol >= $existing_end) 
813
            { 
813
            { 
814
                unset($this->_colinfo[$key]); 
814
                unset($this->_colinfo[$key]); 
815
            } 
815
            } 
816
        } // added by Dan Lynn <dan@spiderweblabs.com) on 2006-12-06 
816
        } // added by Dan Lynn <dan@spiderweblabs.com) on 2006-12-06 
817
        // regenerate keys 
817
        // regenerate keys 
818
        $this->_colinfo = array_values($this->_colinfo); 
818
        $this->_colinfo = array_values($this->_colinfo); 
819
        $this->_colinfo[] = array($firstcol, $lastcol, $width, &$format, $hidden, $level); 
819
        $this->_colinfo[] = array($firstcol, $lastcol, $width, &$format, $hidden, $level); 
820
        // Set width to zero if column is hidden 
820
        // Set width to zero if column is hidden 
821
        $width = ($hidden) ? 0 : $width; 
821
        $width = ($hidden) ? 0 : $width; 
822
        for ($col = $firstcol; $col <= $lastcol; $col++) 
822
        for ($col = $firstcol; $col <= $lastcol; $col++) 
823
        { 
823
        { 
824
            $this->col_sizes[$col] = $width; 
824
            $this->col_sizes[$col] = $width; 
825
        } 
825
        } 
826
    }
826
    }
827
 
827
 
828
    /**
828
    /**
829
    * Set which cell or cells are selected in a worksheet
829
    * Set which cell or cells are selected in a worksheet
830
    *
830
    *
831
    * @access public
831
    * @access public
832
    * @param integer $first_row    first row in the selected quadrant
832
    * @param integer $first_row    first row in the selected quadrant
833
    * @param integer $first_column first column in the selected quadrant
833
    * @param integer $first_column first column in the selected quadrant
834
    * @param integer $last_row     last row in the selected quadrant
834
    * @param integer $last_row     last row in the selected quadrant
835
    * @param integer $last_column  last column in the selected quadrant
835
    * @param integer $last_column  last column in the selected quadrant
836
    */
836
    */
837
    function setSelection($first_row,$first_column,$last_row,$last_column)
837
    function setSelection($first_row,$first_column,$last_row,$last_column)
838
    {
838
    {
839
        $this->_selection = array($first_row,$first_column,$last_row,$last_column);
839
        $this->_selection = array($first_row,$first_column,$last_row,$last_column);
840
    }
840
    }
841
 
841
 
842
    /**
842
    /**
843
    * Set panes and mark them as frozen.
843
    * Set panes and mark them as frozen.
844
    *
844
    *
845
    * @access public
845
    * @access public
846
    * @param array $panes This is the only parameter received and is composed of the following:
846
    * @param array $panes This is the only parameter received and is composed of the following:
847
    *                     0 => Vertical split position,
847
    *                     0 => Vertical split position,
848
    *                     1 => Horizontal split position
848
    *                     1 => Horizontal split position
849
    *                     2 => Top row visible
849
    *                     2 => Top row visible
850
    *                     3 => Leftmost column visible
850
    *                     3 => Leftmost column visible
851
    *                     4 => Active pane
851
    *                     4 => Active pane
852
    */
852
    */
853
    function freezePanes($panes)
853
    function freezePanes($panes)
854
    {
854
    {
855
        $this->_frozen = 1;
855
        $this->_frozen = 1;
856
        $this->_panes  = $panes;
856
        $this->_panes  = $panes;
857
    }
857
    }
858
 
858
 
859
    /**
859
    /**
860
    * Set panes and mark them as unfrozen.
860
    * Set panes and mark them as unfrozen.
861
    *
861
    *
862
    * @access public
862
    * @access public
863
    * @param array $panes This is the only parameter received and is composed of the following:
863
    * @param array $panes This is the only parameter received and is composed of the following:
864
    *                     0 => Vertical split position,
864
    *                     0 => Vertical split position,
865
    *                     1 => Horizontal split position
865
    *                     1 => Horizontal split position
866
    *                     2 => Top row visible
866
    *                     2 => Top row visible
867
    *                     3 => Leftmost column visible
867
    *                     3 => Leftmost column visible
868
    *                     4 => Active pane
868
    *                     4 => Active pane
869
    */
869
    */
870
    function thawPanes($panes)
870
    function thawPanes($panes)
871
    {
871
    {
872
        $this->_frozen = 0;
872
        $this->_frozen = 0;
873
        $this->_panes  = $panes;
873
        $this->_panes  = $panes;
874
    }
874
    }
875
 
875
 
876
    /**
876
    /**
877
    * Set the page orientation as portrait.
877
    * Set the page orientation as portrait.
878
    *
878
    *
879
    * @access public
879
    * @access public
880
    */
880
    */
881
    function setPortrait()
881
    function setPortrait()
882
    {
882
    {
883
        $this->_orientation = 1;
883
        $this->_orientation = 1;
884
    }
884
    }
885
 
885
 
886
    /**
886
    /**
887
    * Set the page orientation as landscape.
887
    * Set the page orientation as landscape.
888
    *
888
    *
889
    * @access public
889
    * @access public
890
    */
890
    */
891
    function setLandscape()
891
    function setLandscape()
892
    {
892
    {
893
        $this->_orientation = 0;
893
        $this->_orientation = 0;
894
    }
894
    }
895
 
895
 
896
    /**
896
    /**
897
    * Set the paper type. Ex. 1 = US Letter, 9 = A4
897
    * Set the paper type. Ex. 1 = US Letter, 9 = A4
898
    *
898
    *
899
    * @access public
899
    * @access public
900
    * @param integer $size The type of paper size to use
900
    * @param integer $size The type of paper size to use
901
    */
901
    */
902
    function setPaper($size = 0)
902
    function setPaper($size = 0)
903
    {
903
    {
904
        $this->_paper_size = $size;
904
        $this->_paper_size = $size;
905
    }
905
    }
906
 
906
 
907
 
907
 
908
    /**
908
    /**
909
    * Set the page header caption and optional margin.
909
    * Set the page header caption and optional margin.
910
    *
910
    *
911
    * @access public
911
    * @access public
912
    * @param string $string The header text
912
    * @param string $string The header text
913
    * @param float  $margin optional head margin in inches.
913
    * @param float  $margin optional head margin in inches.
914
    */
914
    */
915
    function setHeader($string,$margin = 0.50)
915
    function setHeader($string,$margin = 0.50)
916
    {
916
    {
917
        if (strlen($string) >= 255) {
917
        if (strlen($string) >= 255) {
918
            //carp 'Header string must be less than 255 characters';
918
            //carp 'Header string must be less than 255 characters';
919
            return;
919
            return;
920
        }
920
        }
921
        $this->_header      = $string;
921
        $this->_header      = $string;
922
        $this->_margin_head = $margin;
922
        $this->_margin_head = $margin;
923
    }
923
    }
924
 
924
 
925
    /**
925
    /**
926
    * Set the page footer caption and optional margin.
926
    * Set the page footer caption and optional margin.
927
    *
927
    *
928
    * @access public
928
    * @access public
929
    * @param string $string The footer text
929
    * @param string $string The footer text
930
    * @param float  $margin optional foot margin in inches.
930
    * @param float  $margin optional foot margin in inches.
931
    */
931
    */
932
    function setFooter($string,$margin = 0.50)
932
    function setFooter($string,$margin = 0.50)
933
    {
933
    {
934
        if (strlen($string) >= 255) {
934
        if (strlen($string) >= 255) {
935
            //carp 'Footer string must be less than 255 characters';
935
            //carp 'Footer string must be less than 255 characters';
936
            return;
936
            return;
937
        }
937
        }
938
        $this->_footer      = $string;
938
        $this->_footer      = $string;
939
        $this->_margin_foot = $margin;
939
        $this->_margin_foot = $margin;
940
    }
940
    }
941
 
941
 
942
    /**
942
    /**
943
    * Center the page horinzontally.
943
    * Center the page horinzontally.
944
    *
944
    *
945
    * @access public
945
    * @access public
946
    * @param integer $center the optional value for centering. Defaults to 1 (center).
946
    * @param integer $center the optional value for centering. Defaults to 1 (center).
947
    */
947
    */
948
    function centerHorizontally($center = 1)
948
    function centerHorizontally($center = 1)
949
    {
949
    {
950
        $this->_hcenter = $center;
950
        $this->_hcenter = $center;
951
    }
951
    }
952
 
952
 
953
    /**
953
    /**
954
    * Center the page vertically.
954
    * Center the page vertically.
955
    *
955
    *
956
    * @access public
956
    * @access public
957
    * @param integer $center the optional value for centering. Defaults to 1 (center).
957
    * @param integer $center the optional value for centering. Defaults to 1 (center).
958
    */
958
    */
959
    function centerVertically($center = 1)
959
    function centerVertically($center = 1)
960
    {
960
    {
961
        $this->_vcenter = $center;
961
        $this->_vcenter = $center;
962
    }
962
    }
963
 
963
 
964
    /**
964
    /**
965
    * Set all the page margins to the same value in inches.
965
    * Set all the page margins to the same value in inches.
966
    *
966
    *
967
    * @access public
967
    * @access public
968
    * @param float $margin The margin to set in inches
968
    * @param float $margin The margin to set in inches
969
    */
969
    */
970
    function setMargins($margin)
970
    function setMargins($margin)
971
    {
971
    {
972
        $this->setMarginLeft($margin);
972
        $this->setMarginLeft($margin);
973
        $this->setMarginRight($margin);
973
        $this->setMarginRight($margin);
974
        $this->setMarginTop($margin);
974
        $this->setMarginTop($margin);
975
        $this->setMarginBottom($margin);
975
        $this->setMarginBottom($margin);
976
    }
976
    }
977
 
977
 
978
    /**
978
    /**
979
    * Set the left and right margins to the same value in inches.
979
    * Set the left and right margins to the same value in inches.
980
    *
980
    *
981
    * @access public
981
    * @access public
982
    * @param float $margin The margin to set in inches
982
    * @param float $margin The margin to set in inches
983
    */
983
    */
984
    function setMargins_LR($margin)
984
    function setMargins_LR($margin)
985
    {
985
    {
986
        $this->setMarginLeft($margin);
986
        $this->setMarginLeft($margin);
987
        $this->setMarginRight($margin);
987
        $this->setMarginRight($margin);
988
    }
988
    }
989
 
989
 
990
    /**
990
    /**
991
    * Set the top and bottom margins to the same value in inches.
991
    * Set the top and bottom margins to the same value in inches.
992
    *
992
    *
993
    * @access public
993
    * @access public
994
    * @param float $margin The margin to set in inches
994
    * @param float $margin The margin to set in inches
995
    */
995
    */
996
    function setMargins_TB($margin)
996
    function setMargins_TB($margin)
997
    {
997
    {
998
        $this->setMarginTop($margin);
998
        $this->setMarginTop($margin);
999
        $this->setMarginBottom($margin);
999
        $this->setMarginBottom($margin);
1000
    }
1000
    }
1001
 
1001
 
1002
    /**
1002
    /**
1003
    * Set the left margin in inches.
1003
    * Set the left margin in inches.
1004
    *
1004
    *
1005
    * @access public
1005
    * @access public
1006
    * @param float $margin The margin to set in inches
1006
    * @param float $margin The margin to set in inches
1007
    */
1007
    */
1008
    function setMarginLeft($margin = 0.75)
1008
    function setMarginLeft($margin = 0.75)
1009
    {
1009
    {
1010
        $this->_margin_left = $margin;
1010
        $this->_margin_left = $margin;
1011
    }
1011
    }
1012
 
1012
 
1013
    /**
1013
    /**
1014
    * Set the right margin in inches.
1014
    * Set the right margin in inches.
1015
    *
1015
    *
1016
    * @access public
1016
    * @access public
1017
    * @param float $margin The margin to set in inches
1017
    * @param float $margin The margin to set in inches
1018
    */
1018
    */
1019
    function setMarginRight($margin = 0.75)
1019
    function setMarginRight($margin = 0.75)
1020
    {
1020
    {
1021
        $this->_margin_right = $margin;
1021
        $this->_margin_right = $margin;
1022
    }
1022
    }
1023
 
1023
 
1024
    /**
1024
    /**
1025
    * Set the top margin in inches.
1025
    * Set the top margin in inches.
1026
    *
1026
    *
1027
    * @access public
1027
    * @access public
1028
    * @param float $margin The margin to set in inches
1028
    * @param float $margin The margin to set in inches
1029
    */
1029
    */
1030
    function setMarginTop($margin = 1.00)
1030
    function setMarginTop($margin = 1.00)
1031
    {
1031
    {
1032
        $this->_margin_top = $margin;
1032
        $this->_margin_top = $margin;
1033
    }
1033
    }
1034
 
1034
 
1035
    /**
1035
    /**
1036
    * Set the bottom margin in inches.
1036
    * Set the bottom margin in inches.
1037
    *
1037
    *
1038
    * @access public
1038
    * @access public
1039
    * @param float $margin The margin to set in inches
1039
    * @param float $margin The margin to set in inches
1040
    */
1040
    */
1041
    function setMarginBottom($margin = 1.00)
1041
    function setMarginBottom($margin = 1.00)
1042
    {
1042
    {
1043
        $this->_margin_bottom = $margin;
1043
        $this->_margin_bottom = $margin;
1044
    }
1044
    }
1045
 
1045
 
1046
    /**
1046
    /**
1047
    * Set the rows to repeat at the top of each printed page.
1047
    * Set the rows to repeat at the top of each printed page.
1048
    *
1048
    *
1049
    * @access public
1049
    * @access public
1050
    * @param integer $first_row First row to repeat
1050
    * @param integer $first_row First row to repeat
1051
    * @param integer $last_row  Last row to repeat. Optional.
1051
    * @param integer $last_row  Last row to repeat. Optional.
1052
    */
1052
    */
1053
    function repeatRows($first_row, $last_row = null)
1053
    function repeatRows($first_row, $last_row = null)
1054
    {
1054
    {
1055
        $this->title_rowmin  = $first_row;
1055
        $this->title_rowmin  = $first_row;
1056
        if (isset($last_row)) { //Second row is optional
1056
        if (isset($last_row)) { //Second row is optional
1057
            $this->title_rowmax  = $last_row;
1057
            $this->title_rowmax  = $last_row;
1058
        } else {
1058
        } else {
1059
            $this->title_rowmax  = $first_row;
1059
            $this->title_rowmax  = $first_row;
1060
        }
1060
        }
1061
    }
1061
    }
1062
 
1062
 
1063
    /**
1063
    /**
1064
    * Set the columns to repeat at the left hand side of each printed page.
1064
    * Set the columns to repeat at the left hand side of each printed page.
1065
    *
1065
    *
1066
    * @access public
1066
    * @access public
1067
    * @param integer $first_col First column to repeat
1067
    * @param integer $first_col First column to repeat
1068
    * @param integer $last_col  Last column to repeat. Optional.
1068
    * @param integer $last_col  Last column to repeat. Optional.
1069
    */
1069
    */
1070
    function repeatColumns($first_col, $last_col = null)
1070
    function repeatColumns($first_col, $last_col = null)
1071
    {
1071
    {
1072
        $this->title_colmin  = $first_col;
1072
        $this->title_colmin  = $first_col;
1073
        if (isset($last_col)) { // Second col is optional
1073
        if (isset($last_col)) { // Second col is optional
1074
            $this->title_colmax  = $last_col;
1074
            $this->title_colmax  = $last_col;
1075
        } else {
1075
        } else {
1076
            $this->title_colmax  = $first_col;
1076
            $this->title_colmax  = $first_col;
1077
        }
1077
        }
1078
    }
1078
    }
1079
 
1079
 
1080
    /**
1080
    /**
1081
    * Set the area of each worksheet that will be printed.
1081
    * Set the area of each worksheet that will be printed.
1082
    *
1082
    *
1083
    * @access public
1083
    * @access public
1084
    * @param integer $first_row First row of the area to print
1084
    * @param integer $first_row First row of the area to print
1085
    * @param integer $first_col First column of the area to print
1085
    * @param integer $first_col First column of the area to print
1086
    * @param integer $last_row  Last row of the area to print
1086
    * @param integer $last_row  Last row of the area to print
1087
    * @param integer $last_col  Last column of the area to print
1087
    * @param integer $last_col  Last column of the area to print
1088
    */
1088
    */
1089
    function printArea($first_row, $first_col, $last_row, $last_col)
1089
    function printArea($first_row, $first_col, $last_row, $last_col)
1090
    {
1090
    {
1091
        $this->print_rowmin  = $first_row;
1091
        $this->print_rowmin  = $first_row;
1092
        $this->print_colmin  = $first_col;
1092
        $this->print_colmin  = $first_col;
1093
        $this->print_rowmax  = $last_row;
1093
        $this->print_rowmax  = $last_row;
1094
        $this->print_colmax  = $last_col;
1094
        $this->print_colmax  = $last_col;
1095
    }
1095
    }
1096
 
1096
 
1097
 
1097
 
1098
    /**
1098
    /**
1099
    * Set the option to hide gridlines on the printed page.
1099
    * Set the option to hide gridlines on the printed page.
1100
    *
1100
    *
1101
    * @access public
1101
    * @access public
1102
    */
1102
    */
1103
    function hideGridlines()
1103
    function hideGridlines()
1104
    {
1104
    {
1105
        $this->_print_gridlines = 0;
1105
        $this->_print_gridlines = 0;
1106
    }
1106
    }
1107
 
1107
 
1108
    /**
1108
    /**
1109
    * Set the option to hide gridlines on the worksheet (as seen on the screen).
1109
    * Set the option to hide gridlines on the worksheet (as seen on the screen).
1110
    *
1110
    *
1111
    * @access public
1111
    * @access public
1112
    */
1112
    */
1113
    function hideScreenGridlines()
1113
    function hideScreenGridlines()
1114
    {
1114
    {
1115
        $this->_screen_gridlines = 0;
1115
        $this->_screen_gridlines = 0;
1116
    }
1116
    }
1117
 
1117
 
1118
    /**
1118
    /**
1119
    * Set the option to print the row and column headers on the printed page.
1119
    * Set the option to print the row and column headers on the printed page.
1120
    *
1120
    *
1121
    * @access public
1121
    * @access public
1122
    * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
1122
    * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
1123
    */
1123
    */
1124
    function printRowColHeaders($print = 1)
1124
    function printRowColHeaders($print = 1)
1125
    {
1125
    {
1126
        $this->_print_headers = $print;
1126
        $this->_print_headers = $print;
1127
    }
1127
    }
1128
 
1128
 
1129
    /**
1129
    /**
1130
    * Set the vertical and horizontal number of pages that will define the maximum area printed.
1130
    * Set the vertical and horizontal number of pages that will define the maximum area printed.
1131
    * It doesn't seem to work with OpenOffice.
1131
    * It doesn't seem to work with OpenOffice.
1132
    *
1132
    *
1133
    * @access public
1133
    * @access public
1134
    * @param  integer $width  Maximun width of printed area in pages
1134
    * @param  integer $width  Maximun width of printed area in pages
1135
    * @param  integer $height Maximun heigth of printed area in pages
1135
    * @param  integer $height Maximun heigth of printed area in pages
1136
    * @see setPrintScale()
1136
    * @see setPrintScale()
1137
    */
1137
    */
1138
    function fitToPages($width, $height)
1138
    function fitToPages($width, $height)
1139
    {
1139
    {
1140
        $this->_fit_page      = 1;
1140
        $this->_fit_page      = 1;
1141
        $this->_fit_width     = $width;
1141
        $this->_fit_width     = $width;
1142
        $this->_fit_height    = $height;
1142
        $this->_fit_height    = $height;
1143
    }
1143
    }
1144
 
1144
 
1145
    /**
1145
    /**
1146
    * Store the horizontal page breaks on a worksheet (for printing).
1146
    * Store the horizontal page breaks on a worksheet (for printing).
1147
    * The breaks represent the row after which the break is inserted.
1147
    * The breaks represent the row after which the break is inserted.
1148
    *
1148
    *
1149
    * @access public
1149
    * @access public
1150
    * @param array $breaks Array containing the horizontal page breaks
1150
    * @param array $breaks Array containing the horizontal page breaks
1151
    */
1151
    */
1152
    function setHPagebreaks($breaks)
1152
    function setHPagebreaks($breaks)
1153
    {
1153
    {
1154
        foreach ($breaks as $break) {
1154
        foreach ($breaks as $break) {
1155
            array_push($this->_hbreaks, $break);
1155
            array_push($this->_hbreaks, $break);
1156
        }
1156
        }
1157
    }
1157
    }
1158
 
1158
 
1159
    /**
1159
    /**
1160
    * Store the vertical page breaks on a worksheet (for printing).
1160
    * Store the vertical page breaks on a worksheet (for printing).
1161
    * The breaks represent the column after which the break is inserted.
1161
    * The breaks represent the column after which the break is inserted.
1162
    *
1162
    *
1163
    * @access public
1163
    * @access public
1164
    * @param array $breaks Array containing the vertical page breaks
1164
    * @param array $breaks Array containing the vertical page breaks
1165
    */
1165
    */
1166
    function setVPagebreaks($breaks)
1166
    function setVPagebreaks($breaks)
1167
    {
1167
    {
1168
        foreach ($breaks as $break) {
1168
        foreach ($breaks as $break) {
1169
            array_push($this->_vbreaks, $break);
1169
            array_push($this->_vbreaks, $break);
1170
        }
1170
        }
1171
    }
1171
    }
1172
 
1172
 
1173
 
1173
 
1174
    /**
1174
    /**
1175
    * Set the worksheet zoom factor.
1175
    * Set the worksheet zoom factor.
1176
    *
1176
    *
1177
    * @access public
1177
    * @access public
1178
    * @param integer $scale The zoom factor
1178
    * @param integer $scale The zoom factor
1179
    */
1179
    */
1180
    function setZoom($scale = 100)
1180
    function setZoom($scale = 100)
1181
    {
1181
    {
1182
        // Confine the scale to Excel's range
1182
        // Confine the scale to Excel's range
1183
        if ($scale < 10 || $scale > 400) {
1183
        if ($scale < 10 || $scale > 400) {
1184
            $this->raiseError("Zoom factor $scale outside range: 10 <= zoom <= 400");
1184
            $this->raiseError("Zoom factor $scale outside range: 10 <= zoom <= 400");
1185
            $scale = 100;
1185
            $scale = 100;
1186
        }
1186
        }
1187
 
1187
 
1188
        $this->_zoom = floor($scale);
1188
        $this->_zoom = floor($scale);
1189
    }
1189
    }
1190
 
1190
 
1191
    /**
1191
    /**
1192
    * Set the scale factor for the printed page.
1192
    * Set the scale factor for the printed page.
1193
    * It turns off the "fit to page" option
1193
    * It turns off the "fit to page" option
1194
    *
1194
    *
1195
    * @access public
1195
    * @access public
1196
    * @param integer $scale The optional scale factor. Defaults to 100
1196
    * @param integer $scale The optional scale factor. Defaults to 100
1197
    */
1197
    */
1198
    function setPrintScale($scale = 100)
1198
    function setPrintScale($scale = 100)
1199
    {
1199
    {
1200
        // Confine the scale to Excel's range
1200
        // Confine the scale to Excel's range
1201
        if ($scale < 10 || $scale > 400) {
1201
        if ($scale < 10 || $scale > 400) {
1202
            $this->raiseError("Print scale $scale outside range: 10 <= zoom <= 400");
1202
            $this->raiseError("Print scale $scale outside range: 10 <= zoom <= 400");
1203
            $scale = 100;
1203
            $scale = 100;
1204
        }
1204
        }
1205
 
1205
 
1206
        // Turn off "fit to page" option
1206
        // Turn off "fit to page" option
1207
        $this->_fit_page = 0;
1207
        $this->_fit_page = 0;
1208
 
1208
 
1209
        $this->_print_scale = floor($scale);
1209
        $this->_print_scale = floor($scale);
1210
    }
1210
    }
1211
 
1211
 
1212
    /**
1212
    /**
1213
    * Map to the appropriate write method acording to the token recieved.
1213
    * Map to the appropriate write method acording to the token recieved.
1214
    *
1214
    *
1215
    * @access public
1215
    * @access public
1216
    * @param integer $row    The row of the cell we are writing to
1216
    * @param integer $row    The row of the cell we are writing to
1217
    * @param integer $col    The column of the cell we are writing to
1217
    * @param integer $col    The column of the cell we are writing to
1218
    * @param mixed   $token  What we are writing
1218
    * @param mixed   $token  What we are writing
1219
    * @param mixed   $format The optional format to apply to the cell
1219
    * @param mixed   $format The optional format to apply to the cell
1220
    */
1220
    */
1221
    function write($row, $col, $token, $format = null)
1221
    function write($row, $col, $token, $format = null)
1222
    {
1222
    {
1223
        // Check for a cell reference in A1 notation and substitute row and column
1223
        // Check for a cell reference in A1 notation and substitute row and column
1224
        /*if ($_[0] =~ /^\D/) {
1224
        /*if ($_[0] =~ /^\D/) {
1225
            @_ = $this->_substituteCellref(@_);
1225
            @_ = $this->_substituteCellref(@_);
1226
    }*/
1226
    }*/
1227
 
1227
 
1228
        if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
1228
        if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
1229
            // Match number
1229
            // Match number
1230
            return $this->writeNumber($row, $col, $token, $format);
1230
            return $this->writeNumber($row, $col, $token, $format);
1231
        } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
1231
        } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
1232
            // Match http or ftp URL
1232
            // Match http or ftp URL
1233
            return $this->writeUrl($row, $col, $token, '', $format);
1233
            return $this->writeUrl($row, $col, $token, '', $format);
1234
        } elseif (preg_match("/^mailto:/", $token)) {
1234
        } elseif (preg_match("/^mailto:/", $token)) {
1235
            // Match mailto:
1235
            // Match mailto:
1236
            return $this->writeUrl($row, $col, $token, '', $format);
1236
            return $this->writeUrl($row, $col, $token, '', $format);
1237
        } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
1237
        } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
1238
            // Match internal or external sheet link
1238
            // Match internal or external sheet link
1239
            return $this->writeUrl($row, $col, $token, '', $format);
1239
            return $this->writeUrl($row, $col, $token, '', $format);
1240
        } elseif (preg_match("/^=/", $token)) {
1240
        } elseif (preg_match("/^=/", $token)) {
1241
            // Match formula
1241
            // Match formula
1242
            return $this->writeFormula($row, $col, $token, $format);
1242
            return $this->writeFormula($row, $col, $token, $format);
1243
        } elseif ($token == '') {
1243
        } elseif ($token == '') {
1244
            // Match blank
1244
            // Match blank
1245
            return $this->writeBlank($row, $col, $format);
1245
            return $this->writeBlank($row, $col, $format);
1246
        } else {
1246
        } else {
1247
            // Default: match string
1247
            // Default: match string
1248
            return $this->writeString($row, $col, $token, $format);
1248
            return $this->writeString($row, $col, $token, $format);
1249
        }
1249
        }
1250
    }
1250
    }
1251
 
1251
 
1252
    /**
1252
    /**
1253
    * Write an array of values as a row
1253
    * Write an array of values as a row
1254
    *
1254
    *
1255
    * @access public
1255
    * @access public
1256
    * @param integer $row    The row we are writing to
1256
    * @param integer $row    The row we are writing to
1257
    * @param integer $col    The first col (leftmost col) we are writing to
1257
    * @param integer $col    The first col (leftmost col) we are writing to
1258
    * @param array   $val    The array of values to write
1258
    * @param array   $val    The array of values to write
1259
    * @param mixed   $format The optional format to apply to the cell
1259
    * @param mixed   $format The optional format to apply to the cell
1260
    * @return mixed PEAR_Error on failure
1260
    * @return mixed PEAR_Error on failure
1261
    */
1261
    */
1262
 
1262
 
1263
    function writeRow($row, $col, $val, $format = null)
1263
    function writeRow($row, $col, $val, $format = null)
1264
    {
1264
    {
1265
        $retval = '';
1265
        $retval = '';
1266
        if (is_array($val)) {
1266
        if (is_array($val)) {
1267
            foreach ($val as $v) {
1267
            foreach ($val as $v) {
1268
                if (is_array($v)) {
1268
                if (is_array($v)) {
1269
                    $this->writeCol($row, $col, $v, $format);
1269
                    $this->writeCol($row, $col, $v, $format);
1270
                } else {
1270
                } else {
1271
                    $this->write($row, $col, $v, $format);
1271
                    $this->write($row, $col, $v, $format);
1272
                }
1272
                }
1273
                $col++;
1273
                $col++;
1274
            }
1274
            }
1275
        } else {
1275
        } else {
1276
            $retval = new PEAR_Error('$val needs to be an array');
1276
            $retval = new PEAR_Error('$val needs to be an array');
1277
        }
1277
        }
1278
        return($retval);
1278
        return($retval);
1279
    }
1279
    }
1280
 
1280
 
1281
    /**
1281
    /**
1282
    * Write an array of values as a column
1282
    * Write an array of values as a column
1283
    *
1283
    *
1284
    * @access public
1284
    * @access public
1285
    * @param integer $row    The first row (uppermost row) we are writing to
1285
    * @param integer $row    The first row (uppermost row) we are writing to
1286
    * @param integer $col    The col we are writing to
1286
    * @param integer $col    The col we are writing to
1287
    * @param array   $val    The array of values to write
1287
    * @param array   $val    The array of values to write
1288
    * @param mixed   $format The optional format to apply to the cell
1288
    * @param mixed   $format The optional format to apply to the cell
1289
    * @return mixed PEAR_Error on failure
1289
    * @return mixed PEAR_Error on failure
1290
    */
1290
    */
1291
 
1291
 
1292
    function writeCol($row, $col, $val, $format = null)
1292
    function writeCol($row, $col, $val, $format = null)
1293
    {
1293
    {
1294
        $retval = '';
1294
        $retval = '';
1295
        if (is_array($val)) {
1295
        if (is_array($val)) {
1296
            foreach ($val as $v) {
1296
            foreach ($val as $v) {
1297
                $this->write($row, $col, $v, $format);
1297
                $this->write($row, $col, $v, $format);
1298
                $row++;
1298
                $row++;
1299
            }
1299
            }
1300
        } else {
1300
        } else {
1301
            $retval = new PEAR_Error('$val needs to be an array');
1301
            $retval = new PEAR_Error('$val needs to be an array');
1302
        }
1302
        }
1303
        return($retval);
1303
        return($retval);
1304
    }
1304
    }
1305
 
1305
 
1306
    /**
1306
    /**
1307
    * Returns an index to the XF record in the workbook
1307
    * Returns an index to the XF record in the workbook
1308
    *
1308
    *
1309
    * @access private
1309
    * @access private
1310
    * @param mixed &$format The optional XF format
1310
    * @param mixed &$format The optional XF format
1311
    * @return integer The XF record index
1311
    * @return integer The XF record index
1312
    */
1312
    */
1313
    function _XF(&$format)
1313
    function _XF(&$format)
1314
    {
1314
    {
1315
        if ($format) {
1315
        if ($format) {
1316
            return($format->getXfIndex());
1316
            return($format->getXfIndex());
1317
        } else {
1317
        } else {
1318
            return(0x0F);
1318
            return(0x0F);
1319
        }
1319
        }
1320
    }
1320
    }
1321
 
1321
 
1322
 
1322
 
1323
    /******************************************************************************
1323
    /******************************************************************************
1324
    *******************************************************************************
1324
    *******************************************************************************
1325
    *
1325
    *
1326
    * Internal methods
1326
    * Internal methods
1327
    */
1327
    */
1328
 
1328
 
1329
 
1329
 
1330
    /**
1330
    /**
1331
    * Store Worksheet data in memory using the parent's class append() or to a
1331
    * Store Worksheet data in memory using the parent's class append() or to a
1332
    * temporary file, the default.
1332
    * temporary file, the default.
1333
    *
1333
    *
1334
    * @access private
1334
    * @access private
1335
    * @param string $data The binary data to append
1335
    * @param string $data The binary data to append
1336
    */
1336
    */
1337
    function _append(&$data)
1337
    function _append(&$data)
1338
    {
1338
    {
1339
        if ($this->_using_tmpfile) {
1339
        if ($this->_using_tmpfile) {
1340
            // Add CONTINUE records if necessary
1340
            // Add CONTINUE records if necessary
1341
            if (strlen($data) > $this->_limit) {
1341
            if (strlen($data) > $this->_limit) {
1342
                $data = $this->_addContinue($data);
1342
                $data = $this->_addContinue($data);
1343
            }
1343
            }
1344
            fwrite($this->_filehandle, $data);
1344
            fwrite($this->_filehandle, $data);
1345
            $this->_datasize += strlen($data);
1345
            $this->_datasize += strlen($data);
1346
        } else {
1346
        } else {
1347
            parent::_append($data);
1347
            parent::_append($data);
1348
        }
1348
        }
1349
    }
1349
    }
1350
 
1350
 
1351
    /**
1351
    /**
1352
    * Substitute an Excel cell reference in A1 notation for  zero based row and
1352
    * Substitute an Excel cell reference in A1 notation for  zero based row and
1353
    * column values in an argument list.
1353
    * column values in an argument list.
1354
    *
1354
    *
1355
    * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
1355
    * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
1356
    *
1356
    *
1357
    * @access private
1357
    * @access private
1358
    * @param string $cell The cell reference. Or range of cells.
1358
    * @param string $cell The cell reference. Or range of cells.
1359
    * @return array
1359
    * @return array
1360
    */
1360
    */
1361
    function _substituteCellref($cell)
1361
    function _substituteCellref($cell)
1362
    {
1362
    {
1363
        $cell = strtoupper($cell);
1363
        $cell = strtoupper($cell);
1364
 
1364
 
1365
        // Convert a column range: 'A:A' or 'B:G'
1365
        // Convert a column range: 'A:A' or 'B:G'
1366
        if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/", $cell, $match)) {
1366
        if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/", $cell, $match)) {
1367
            list($no_use, $col1) =  $this->_cellToRowcol($match[1] .'1'); // Add a dummy row
1367
            list($no_use, $col1) =  $this->_cellToRowcol($match[1] .'1'); // Add a dummy row
1368
            list($no_use, $col2) =  $this->_cellToRowcol($match[2] .'1'); // Add a dummy row
1368
            list($no_use, $col2) =  $this->_cellToRowcol($match[2] .'1'); // Add a dummy row
1369
            return(array($col1, $col2));
1369
            return(array($col1, $col2));
1370
        }
1370
        }
1371
 
1371
 
1372
        // Convert a cell range: 'A1:B7'
1372
        // Convert a cell range: 'A1:B7'
1373
        if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
1373
        if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
1374
            list($row1, $col1) =  $this->_cellToRowcol($match[1]);
1374
            list($row1, $col1) =  $this->_cellToRowcol($match[1]);
1375
            list($row2, $col2) =  $this->_cellToRowcol($match[2]);
1375
            list($row2, $col2) =  $this->_cellToRowcol($match[2]);
1376
            return(array($row1, $col1, $row2, $col2));
1376
            return(array($row1, $col1, $row2, $col2));
1377
        }
1377
        }
1378
 
1378
 
1379
        // Convert a cell reference: 'A1' or 'AD2000'
1379
        // Convert a cell reference: 'A1' or 'AD2000'
1380
        if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
1380
        if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
1381
            list($row1, $col1) =  $this->_cellToRowcol($match[1]);
1381
            list($row1, $col1) =  $this->_cellToRowcol($match[1]);
1382
            return(array($row1, $col1));
1382
            return(array($row1, $col1));
1383
        }
1383
        }
1384
 
1384
 
1385
        // TODO use real error codes
1385
        // TODO use real error codes
1386
        $this->raiseError("Unknown cell reference $cell", 0, PEAR_ERROR_DIE);
1386
        $this->raiseError("Unknown cell reference $cell", 0, PEAR_ERROR_DIE);
1387
    }
1387
    }
1388
 
1388
 
1389
    /**
1389
    /**
1390
    * Convert an Excel cell reference in A1 notation to a zero based row and column
1390
    * Convert an Excel cell reference in A1 notation to a zero based row and column
1391
    * reference; converts C1 to (0, 2).
1391
    * reference; converts C1 to (0, 2).
1392
    *
1392
    *
1393
    * @access private
1393
    * @access private
1394
    * @param string $cell The cell reference.
1394
    * @param string $cell The cell reference.
1395
    * @return array containing (row, column)
1395
    * @return array containing (row, column)
1396
    */
1396
    */
1397
    function _cellToRowcol($cell)
1397
    function _cellToRowcol($cell)
1398
    {
1398
    {
1399
        preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
1399
        preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
1400
        $col     = $match[1];
1400
        $col     = $match[1];
1401
        $row     = $match[2];
1401
        $row     = $match[2];
1402
 
1402
 
1403
        // Convert base26 column string to number
1403
        // Convert base26 column string to number
1404
        $chars = explode('', $col);
1404
        $chars = explode('', $col);
1405
        $expn  = 0;
1405
        $expn  = 0;
1406
        $col   = 0;
1406
        $col   = 0;
1407
 
1407
 
1408
        while ($chars) {
1408
        while ($chars) {
1409
            $char = array_pop($chars);        // LS char first
1409
            $char = array_pop($chars);        // LS char first
1410
            $col += (ord($char) -ord('A') +1) * pow(26,$expn);
1410
            $col += (ord($char) -ord('A') +1) * pow(26,$expn);
1411
            $expn++;
1411
            $expn++;
1412
        }
1412
        }
1413
 
1413
 
1414
        // Convert 1-index to zero-index
1414
        // Convert 1-index to zero-index
1415
        $row--;
1415
        $row--;
1416
        $col--;
1416
        $col--;
1417
 
1417
 
1418
        return(array($row, $col));
1418
        return(array($row, $col));
1419
    }
1419
    }
1420
 
1420
 
1421
    /**
1421
    /**
1422
    * Based on the algorithm provided by Daniel Rentz of OpenOffice.
1422
    * Based on the algorithm provided by Daniel Rentz of OpenOffice.
1423
    *
1423
    *
1424
    * @access private
1424
    * @access private
1425
    * @param string $plaintext The password to be encoded in plaintext.
1425
    * @param string $plaintext The password to be encoded in plaintext.
1426
    * @return string The encoded password
1426
    * @return string The encoded password
1427
    */
1427
    */
1428
    function _encodePassword($plaintext)
1428
    function _encodePassword($plaintext)
1429
    {
1429
    {
1430
        $password = 0x0000;
1430
        $password = 0x0000;
1431
        $i        = 1;       // char position
1431
        $i        = 1;       // char position
1432
 
1432
 
1433
        // split the plain text password in its component characters
1433
        // split the plain text password in its component characters
1434
        $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
1434
        $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
1435
        foreach ($chars as $char) {
1435
        foreach ($chars as $char) {
1436
            $value        = ord($char) << $i;   // shifted ASCII value
1436
            $value        = ord($char) << $i;   // shifted ASCII value
1437
            $rotated_bits = $value >> 15;       // rotated bits beyond bit 15
1437
            $rotated_bits = $value >> 15;       // rotated bits beyond bit 15
1438
            $value       &= 0x7fff;             // first 15 bits
1438
            $value       &= 0x7fff;             // first 15 bits
1439
            $password    ^= ($value | $rotated_bits);
1439
            $password    ^= ($value | $rotated_bits);
1440
            $i++;
1440
            $i++;
1441
        }
1441
        }
1442
 
1442
 
1443
        $password ^= strlen($plaintext);
1443
        $password ^= strlen($plaintext);
1444
        $password ^= 0xCE4B;
1444
        $password ^= 0xCE4B;
1445
 
1445
 
1446
        return($password);
1446
        return($password);
1447
    }
1447
    }
1448
 
1448
 
1449
    /**
1449
    /**
1450
    * This method sets the properties for outlining and grouping. The defaults
1450
    * This method sets the properties for outlining and grouping. The defaults
1451
    * correspond to Excel's defaults.
1451
    * correspond to Excel's defaults.
1452
    *
1452
    *
1453
    * @param bool $visible
1453
    * @param bool $visible
1454
    * @param bool $symbols_below
1454
    * @param bool $symbols_below
1455
    * @param bool $symbols_right
1455
    * @param bool $symbols_right
1456
    * @param bool $auto_style
1456
    * @param bool $auto_style
1457
    */
1457
    */
1458
    function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
1458
    function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
1459
    {
1459
    {
1460
        $this->_outline_on    = $visible;
1460
        $this->_outline_on    = $visible;
1461
        $this->_outline_below = $symbols_below;
1461
        $this->_outline_below = $symbols_below;
1462
        $this->_outline_right = $symbols_right;
1462
        $this->_outline_right = $symbols_right;
1463
        $this->_outline_style = $auto_style;
1463
        $this->_outline_style = $auto_style;
1464
 
1464
 
1465
        // Ensure this is a boolean vale for Window2
1465
        // Ensure this is a boolean vale for Window2
1466
        if ($this->_outline_on) {
1466
        if ($this->_outline_on) {
1467
            $this->_outline_on = 1;
1467
            $this->_outline_on = 1;
1468
        }
1468
        }
1469
     }
1469
     }
1470
 
1470
 
1471
    /**
1471
    /**
1472
    * This method sets the worksheet direction to right-to-left (RTL)
1472
    * This method sets the worksheet direction to right-to-left (RTL)
1473
    *
1473
    *
1474
    * @param bool $rtl
1474
    * @param bool $rtl
1475
    */
1475
    */
1476
    function setRTL($rtl = true)
1476
    function setRTL($rtl = true)
1477
    {
1477
    {
1478
        $this->_Arabic = ($rtl ? 1 : 0);
1478
        $this->_Arabic = ($rtl ? 1 : 0);
1479
     }
1479
     }
1480
 
1480
 
1481
    /******************************************************************************
1481
    /******************************************************************************
1482
    *******************************************************************************
1482
    *******************************************************************************
1483
    *
1483
    *
1484
    * BIFF RECORDS
1484
    * BIFF RECORDS
1485
    */
1485
    */
1486
 
1486
 
1487
 
1487
 
1488
    /**
1488
    /**
1489
    * Write a double to the specified row and column (zero indexed).
1489
    * Write a double to the specified row and column (zero indexed).
1490
    * An integer can be written as a double. Excel will display an
1490
    * An integer can be written as a double. Excel will display an
1491
    * integer. $format is optional.
1491
    * integer. $format is optional.
1492
    *
1492
    *
1493
    * Returns  0 : normal termination
1493
    * Returns  0 : normal termination
1494
    *         -2 : row or column out of range
1494
    *         -2 : row or column out of range
1495
    *
1495
    *
1496
    * @access public
1496
    * @access public
1497
    * @param integer $row    Zero indexed row
1497
    * @param integer $row    Zero indexed row
1498
    * @param integer $col    Zero indexed column
1498
    * @param integer $col    Zero indexed column
1499
    * @param float   $num    The number to write
1499
    * @param float   $num    The number to write
1500
    * @param mixed   $format The optional XF format
1500
    * @param mixed   $format The optional XF format
1501
    * @return integer
1501
    * @return integer
1502
    */
1502
    */
1503
    function writeNumber($row, $col, $num, $format = null)
1503
    function writeNumber($row, $col, $num, $format = null)
1504
    {
1504
    {
1505
        $record    = 0x0203;                 // Record identifier
1505
        $record    = 0x0203;                 // Record identifier
1506
        $length    = 0x000E;                 // Number of bytes to follow
1506
        $length    = 0x000E;                 // Number of bytes to follow
1507
 
1507
 
1508
        $xf        = $this->_XF($format);    // The cell format
1508
        $xf        = $this->_XF($format);    // The cell format
1509
 
1509
 
1510
        // Check that row and col are valid and store max and min values
1510
        // Check that row and col are valid and store max and min values
1511
        if ($row >= $this->_xls_rowmax) {
1511
        if ($row >= $this->_xls_rowmax) {
1512
            return(-2);
1512
            return(-2);
1513
        }
1513
        }
1514
        if ($col >= $this->_xls_colmax) {
1514
        if ($col >= $this->_xls_colmax) {
1515
            return(-2);
1515
            return(-2);
1516
        }
1516
        }
1517
        if ($row <  $this->_dim_rowmin)  {
1517
        if ($row <  $this->_dim_rowmin)  {
1518
            $this->_dim_rowmin = $row;
1518
            $this->_dim_rowmin = $row;
1519
        }
1519
        }
1520
        if ($row >  $this->_dim_rowmax)  {
1520
        if ($row >  $this->_dim_rowmax)  {
1521
            $this->_dim_rowmax = $row;
1521
            $this->_dim_rowmax = $row;
1522
        }
1522
        }
1523
        if ($col <  $this->_dim_colmin)  {
1523
        if ($col <  $this->_dim_colmin)  {
1524
            $this->_dim_colmin = $col;
1524
            $this->_dim_colmin = $col;
1525
        }
1525
        }
1526
        if ($col >  $this->_dim_colmax)  {
1526
        if ($col >  $this->_dim_colmax)  {
1527
            $this->_dim_colmax = $col;
1527
            $this->_dim_colmax = $col;
1528
        }
1528
        }
1529
 
1529
 
1530
        $header    = pack("vv",  $record, $length);
1530
        $header    = pack("vv",  $record, $length);
1531
        $data      = pack("vvv", $row, $col, $xf);
1531
        $data      = pack("vvv", $row, $col, $xf);
1532
        $xl_double = pack("d",   $num);
1532
        $xl_double = pack("d",   $num);
1533
        if ($this->_byte_order) { // if it's Big Endian
1533
        if ($this->_byte_order) { // if it's Big Endian
1534
            $xl_double = strrev($xl_double);
1534
            $xl_double = strrev($xl_double);
1535
        }
1535
        }
1536
 
1536
 
1537
		$data = $header . $data . $xl_double;
1537
		$data = $header . $data . $xl_double;
1538
        $this->_append($data);
1538
        $this->_append($data);
1539
        return(0);
1539
        return(0);
1540
    }
1540
    }
1541
 
1541
 
1542
    /**
1542
    /**
1543
    * Write a string to the specified row and column (zero indexed).
1543
    * Write a string to the specified row and column (zero indexed).
1544
    * NOTE: there is an Excel 5 defined limit of 255 characters.
1544
    * NOTE: there is an Excel 5 defined limit of 255 characters.
1545
    * $format is optional.
1545
    * $format is optional.
1546
    * Returns  0 : normal termination
1546
    * Returns  0 : normal termination
1547
    *         -2 : row or column out of range
1547
    *         -2 : row or column out of range
1548
    *         -3 : long string truncated to 255 chars
1548
    *         -3 : long string truncated to 255 chars
1549
    *
1549
    *
1550
    * @access public
1550
    * @access public
1551
    * @param integer $row    Zero indexed row
1551
    * @param integer $row    Zero indexed row
1552
    * @param integer $col    Zero indexed column
1552
    * @param integer $col    Zero indexed column
1553
    * @param string  $str    The string to write
1553
    * @param string  $str    The string to write
1554
    * @param mixed   $format The XF format for the cell
1554
    * @param mixed   $format The XF format for the cell
1555
    * @return integer
1555
    * @return integer
1556
    */
1556
    */
1557
    function writeString($row, $col, $str, $format = null)
1557
    function writeString($row, $col, $str, $format = null)
1558
    {
1558
    {
1559
        if ($this->_BIFF_version == 0x0600) {
1559
        if ($this->_BIFF_version == 0x0600) {
1560
            return $this->writeStringBIFF8($row, $col, $str, $format);
1560
            return $this->writeStringBIFF8($row, $col, $str, $format);
1561
        }
1561
        }
1562
        $strlen    = strlen($str);
1562
        $strlen    = strlen($str);
1563
        $record    = 0x0204;                   // Record identifier
1563
        $record    = 0x0204;                   // Record identifier
1564
        $length    = 0x0008 + $strlen;         // Bytes to follow
1564
        $length    = 0x0008 + $strlen;         // Bytes to follow
1565
        $xf        = $this->_XF($format);      // The cell format
1565
        $xf        = $this->_XF($format);      // The cell format
1566
 
1566
 
1567
        $str_error = 0;
1567
        $str_error = 0;
1568
 
1568
 
1569
        // Check that row and col are valid and store max and min values
1569
        // Check that row and col are valid and store max and min values
1570
        if ($row >= $this->_xls_rowmax) {
1570
        if ($row >= $this->_xls_rowmax) {
1571
            return(-2);
1571
            return(-2);
1572
        }
1572
        }
1573
        if ($col >= $this->_xls_colmax) {
1573
        if ($col >= $this->_xls_colmax) {
1574
            return(-2);
1574
            return(-2);
1575
        }
1575
        }
1576
        if ($row <  $this->_dim_rowmin) {
1576
        if ($row <  $this->_dim_rowmin) {
1577
            $this->_dim_rowmin = $row;
1577
            $this->_dim_rowmin = $row;
1578
        }
1578
        }
1579
        if ($row >  $this->_dim_rowmax) {
1579
        if ($row >  $this->_dim_rowmax) {
1580
            $this->_dim_rowmax = $row;
1580
            $this->_dim_rowmax = $row;
1581
        }
1581
        }
1582
        if ($col <  $this->_dim_colmin) {
1582
        if ($col <  $this->_dim_colmin) {
1583
            $this->_dim_colmin = $col;
1583
            $this->_dim_colmin = $col;
1584
        }
1584
        }
1585
        if ($col >  $this->_dim_colmax) {
1585
        if ($col >  $this->_dim_colmax) {
1586
            $this->_dim_colmax = $col;
1586
            $this->_dim_colmax = $col;
1587
        }
1587
        }
1588
 
1588
 
1589
        if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars
1589
        if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars
1590
            $str       = substr($str, 0, $this->_xls_strmax);
1590
            $str       = substr($str, 0, $this->_xls_strmax);
1591
            $length    = 0x0008 + $this->_xls_strmax;
1591
            $length    = 0x0008 + $this->_xls_strmax;
1592
            $strlen    = $this->_xls_strmax;
1592
            $strlen    = $this->_xls_strmax;
1593
            $str_error = -3;
1593
            $str_error = -3;
1594
        }
1594
        }
1595
 
1595
 
1596
        $header    = pack("vv",   $record, $length);
1596
        $header    = pack("vv",   $record, $length);
1597
        $data      = pack("vvvv", $row, $col, $xf, $strlen);
1597
        $data      = pack("vvvv", $row, $col, $xf, $strlen);
1598
 
1598
 
1599
		$data = $header . $data . $str;
1599
		$data = $header . $data . $str;
1600
        $this->_append($data);
1600
        $this->_append($data);
1601
        return($str_error);
1601
        return($str_error);
1602
    }
1602
    }
1603
 
1603
 
1604
    /**
1604
    /**
1605
    * Sets Input Encoding for writing strings
1605
    * Sets Input Encoding for writing strings
1606
    *
1606
    *
1607
    * @access public
1607
    * @access public
1608
    * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7'
1608
    * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7'
1609
    */
1609
    */
1610
    function setInputEncoding($encoding)
1610
    function setInputEncoding($encoding)
1611
    {
1611
    {
1612
         if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
1612
         if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
1613
             $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv");
1613
             $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv");
1614
         }
1614
         }
1615
         $this->_input_encoding = $encoding;
1615
         $this->_input_encoding = $encoding;
1616
    }
1616
    }
1617
 
1617
 
1618
    /**
1618
    /**
1619
    * Write a string to the specified row and column (zero indexed).
1619
    * Write a string to the specified row and column (zero indexed).
1620
    * This is the BIFF8 version (no 255 chars limit).
1620
    * This is the BIFF8 version (no 255 chars limit).
1621
    * $format is optional.
1621
    * $format is optional.
1622
    * Returns  0 : normal termination
1622
    * Returns  0 : normal termination
1623
    *         -2 : row or column out of range
1623
    *         -2 : row or column out of range
1624
    *         -3 : long string truncated to 255 chars
1624
    *         -3 : long string truncated to 255 chars
1625
    *
1625
    *
1626
    * @access public
1626
    * @access public
1627
    * @param integer $row    Zero indexed row
1627
    * @param integer $row    Zero indexed row
1628
    * @param integer $col    Zero indexed column
1628
    * @param integer $col    Zero indexed column
1629
    * @param string  $str    The string to write
1629
    * @param string  $str    The string to write
1630
    * @param mixed   $format The XF format for the cell
1630
    * @param mixed   $format The XF format for the cell
1631
    * @return integer
1631
    * @return integer
1632
    */
1632
    */
1633
    function writeStringBIFF8($row, $col, $str, $format = null)
1633
    function writeStringBIFF8($row, $col, $str, $format = null)
1634
    {
1634
    {
1635
        if ($this->_input_encoding == 'UTF-16LE')
1635
        if ($this->_input_encoding == 'UTF-16LE')
1636
        {
1636
        {
1637
            $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
1637
            $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
1638
            $encoding  = 0x1;
1638
            $encoding  = 0x1;
1639
        }
1639
        }
1640
        elseif ($this->_input_encoding != '')
1640
        elseif ($this->_input_encoding != '')
1641
        {
1641
        {
1642
            $str = iconv($this->_input_encoding, 'UTF-16LE', $str);
1642
            $str = iconv($this->_input_encoding, 'UTF-16LE', $str);
1643
            $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
1643
            $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
1644
            $encoding  = 0x1;
1644
            $encoding  = 0x1;
1645
        }
1645
        }
1646
        else
1646
        else
1647
        {
1647
        {
1648
            $strlen    = strlen($str);
1648
            $strlen    = strlen($str);
1649
            $encoding  = 0x0;
1649
            $encoding  = 0x0;
1650
        }
1650
        }
1651
        $record    = 0x00FD;                   // Record identifier
1651
        $record    = 0x00FD;                   // Record identifier
1652
        $length    = 0x000A;                   // Bytes to follow
1652
        $length    = 0x000A;                   // Bytes to follow
1653
        $xf        = $this->_XF($format);      // The cell format
1653
        $xf        = $this->_XF($format);      // The cell format
1654
 
1654
 
1655
        $str_error = 0;
1655
        $str_error = 0;
1656
 
1656
 
1657
        // Check that row and col are valid and store max and min values
1657
        // Check that row and col are valid and store max and min values
1658
        if ($this->_checkRowCol($row, $col) == false) {
1658
        if ($this->_checkRowCol($row, $col) == false) {
1659
            return -2;
1659
            return -2;
1660
        }
1660
        }
1661
 
1661
 
1662
        $str = pack('vC', $strlen, $encoding).$str;
1662
        $str = pack('vC', $strlen, $encoding).$str;
1663
 
1663
 
1664
        /* check if string is already present */
1664
        /* check if string is already present */
1665
        if (!isset($this->_str_table[$str])) {
1665
        if (!isset($this->_str_table[$str])) {
1666
            $this->_str_table[$str] = $this->_str_unique++;
1666
            $this->_str_table[$str] = $this->_str_unique++;
1667
        }
1667
        }
1668
        $this->_str_total++;
1668
        $this->_str_total++;
1669
 
1669
 
1670
        $header    = pack('vv',   $record, $length);
1670
        $header    = pack('vv',   $record, $length);
1671
        $data      = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]);
1671
        $data      = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]);
1672
		$data = $header . $data;
1672
		$data = $header . $data;
1673
 
1673
 
1674
		$this->_append($data);
1674
		$this->_append($data);
1675
        return $str_error;
1675
        return $str_error;
1676
    }
1676
    }
1677
 
1677
 
1678
    /**
1678
    /**
1679
    * Check row and col before writing to a cell, and update the sheet's
1679
    * Check row and col before writing to a cell, and update the sheet's
1680
    * dimensions accordingly
1680
    * dimensions accordingly
1681
    *
1681
    *
1682
    * @access private
1682
    * @access private
1683
    * @param integer $row    Zero indexed row
1683
    * @param integer $row    Zero indexed row
1684
    * @param integer $col    Zero indexed column
1684
    * @param integer $col    Zero indexed column
1685
    * @return boolean true for success, false if row and/or col are grester
1685
    * @return boolean true for success, false if row and/or col are grester
1686
    *                 then maximums allowed.
1686
    *                 then maximums allowed.
1687
    */
1687
    */
1688
    function _checkRowCol($row, $col)
1688
    function _checkRowCol($row, $col)
1689
    {
1689
    {
1690
        if ($row >= $this->_xls_rowmax) {
1690
        if ($row >= $this->_xls_rowmax) {
1691
            return false;
1691
            return false;
1692
        }
1692
        }
1693
        if ($col >= $this->_xls_colmax) {
1693
        if ($col >= $this->_xls_colmax) {
1694
            return false;
1694
            return false;
1695
        }
1695
        }
1696
        if ($row <  $this->_dim_rowmin) {
1696
        if ($row <  $this->_dim_rowmin) {
1697
            $this->_dim_rowmin = $row;
1697
            $this->_dim_rowmin = $row;
1698
        }
1698
        }
1699
        if ($row >  $this->_dim_rowmax) {
1699
        if ($row >  $this->_dim_rowmax) {
1700
            $this->_dim_rowmax = $row;
1700
            $this->_dim_rowmax = $row;
1701
        }
1701
        }
1702
        if ($col <  $this->_dim_colmin) {
1702
        if ($col <  $this->_dim_colmin) {
1703
            $this->_dim_colmin = $col;
1703
            $this->_dim_colmin = $col;
1704
        }
1704
        }
1705
        if ($col >  $this->_dim_colmax) {
1705
        if ($col >  $this->_dim_colmax) {
1706
            $this->_dim_colmax = $col;
1706
            $this->_dim_colmax = $col;
1707
        }
1707
        }
1708
        return true;
1708
        return true;
1709
    }
1709
    }
1710
 
1710
 
1711
    /**
1711
    /**
1712
    * Writes a note associated with the cell given by the row and column.
1712
    * Writes a note associated with the cell given by the row and column.
1713
    * NOTE records don't have a length limit.
1713
    * NOTE records don't have a length limit.
1714
    *
1714
    *
1715
    * @access public
1715
    * @access public
1716
    * @param integer $row    Zero indexed row
1716
    * @param integer $row    Zero indexed row
1717
    * @param integer $col    Zero indexed column
1717
    * @param integer $col    Zero indexed column
1718
    * @param string  $note   The note to write
1718
    * @param string  $note   The note to write
1719
    */
1719
    */
1720
    function writeNote($row, $col, $note)
1720
    function writeNote($row, $col, $note)
1721
    {
1721
    {
1722
        $note_length    = strlen($note);
1722
        $note_length    = strlen($note);
1723
        $record         = 0x001C;                // Record identifier
1723
        $record         = 0x001C;                // Record identifier
1724
        $max_length     = 2048;                  // Maximun length for a NOTE record
1724
        $max_length     = 2048;                  // Maximun length for a NOTE record
1725
        //$length      = 0x0006 + $note_length;    // Bytes to follow
1725
        //$length      = 0x0006 + $note_length;    // Bytes to follow
1726
 
1726
 
1727
        // Check that row and col are valid and store max and min values
1727
        // Check that row and col are valid and store max and min values
1728
        if ($row >= $this->_xls_rowmax) {
1728
        if ($row >= $this->_xls_rowmax) {
1729
            return(-2);
1729
            return(-2);
1730
        }
1730
        }
1731
        if ($col >= $this->_xls_colmax) {
1731
        if ($col >= $this->_xls_colmax) {
1732
            return(-2);
1732
            return(-2);
1733
        }
1733
        }
1734
        if ($row <  $this->_dim_rowmin) {
1734
        if ($row <  $this->_dim_rowmin) {
1735
            $this->_dim_rowmin = $row;
1735
            $this->_dim_rowmin = $row;
1736
        }
1736
        }
1737
        if ($row >  $this->_dim_rowmax) {
1737
        if ($row >  $this->_dim_rowmax) {
1738
            $this->_dim_rowmax = $row;
1738
            $this->_dim_rowmax = $row;
1739
        }
1739
        }
1740
        if ($col <  $this->_dim_colmin) {
1740
        if ($col <  $this->_dim_colmin) {
1741
            $this->_dim_colmin = $col;
1741
            $this->_dim_colmin = $col;
1742
        }
1742
        }
1743
        if ($col >  $this->_dim_colmax) {
1743
        if ($col >  $this->_dim_colmax) {
1744
            $this->_dim_colmax = $col;
1744
            $this->_dim_colmax = $col;
1745
        }
1745
        }
1746
 
1746
 
1747
        // Length for this record is no more than 2048 + 6
1747
        // Length for this record is no more than 2048 + 6
1748
        $length    = 0x0006 + min($note_length, 2048);
1748
        $length    = 0x0006 + min($note_length, 2048);
1749
        $header    = pack("vv",   $record, $length);
1749
        $header    = pack("vv",   $record, $length);
1750
        $data      = pack("vvv", $row, $col, $note_length) . substr($note, 0, 2048);
1750
        $data      = pack("vvv", $row, $col, $note_length) . substr($note, 0, 2048);
1751
		$data = $header . $data;
1751
		$data = $header . $data;
1752
 
1752
 
1753
        $this->_append($data);
1753
        $this->_append($data);
1754
 
1754
 
1755
        for ($i = $max_length; $i < $note_length; $i += $max_length) {
1755
        for ($i = $max_length; $i < $note_length; $i += $max_length) {
1756
            $chunk  = substr($note, $i, $max_length);
1756
            $chunk  = substr($note, $i, $max_length);
1757
            $length = 0x0006 + strlen($chunk);
1757
            $length = 0x0006 + strlen($chunk);
1758
            $header = pack("vv",   $record, $length);
1758
            $header = pack("vv",   $record, $length);
1759
            $data   = pack("vvv", -1, 0, strlen($chunk)) . $chunk;
1759
            $data   = pack("vvv", -1, 0, strlen($chunk)) . $chunk;
1760
			$data = $header . $data;
1760
			$data = $header . $data;
1761
 
1761
 
1762
            $this->_append($data);
1762
            $this->_append($data);
1763
        }
1763
        }
1764
        return(0);
1764
        return(0);
1765
    }
1765
    }
1766
 
1766
 
1767
    /**
1767
    /**
1768
    * Write a blank cell to the specified row and column (zero indexed).
1768
    * Write a blank cell to the specified row and column (zero indexed).
1769
    * A blank cell is used to specify formatting without adding a string
1769
    * A blank cell is used to specify formatting without adding a string
1770
    * or a number.
1770
    * or a number.
1771
    *
1771
    *
1772
    * A blank cell without a format serves no purpose. Therefore, we don't write
1772
    * A blank cell without a format serves no purpose. Therefore, we don't write
1773
    * a BLANK record unless a format is specified.
1773
    * a BLANK record unless a format is specified.
1774
    *
1774
    *
1775
    * Returns  0 : normal termination (including no format)
1775
    * Returns  0 : normal termination (including no format)
1776
    *         -1 : insufficient number of arguments
1776
    *         -1 : insufficient number of arguments
1777
    *         -2 : row or column out of range
1777
    *         -2 : row or column out of range
1778
    *
1778
    *
1779
    * @access public
1779
    * @access public
1780
    * @param integer $row    Zero indexed row
1780
    * @param integer $row    Zero indexed row
1781
    * @param integer $col    Zero indexed column
1781
    * @param integer $col    Zero indexed column
1782
    * @param mixed   $format The XF format
1782
    * @param mixed   $format The XF format
1783
    */
1783
    */
1784
    function writeBlank($row, $col, $format)
1784
    function writeBlank($row, $col, $format)
1785
    {
1785
    {
1786
        // Don't write a blank cell unless it has a format
1786
        // Don't write a blank cell unless it has a format
1787
        if (!$format) {
1787
        if (!$format) {
1788
            return(0);
1788
            return(0);
1789
        }
1789
        }
1790
 
1790
 
1791
        $record    = 0x0201;                 // Record identifier
1791
        $record    = 0x0201;                 // Record identifier
1792
        $length    = 0x0006;                 // Number of bytes to follow
1792
        $length    = 0x0006;                 // Number of bytes to follow
1793
        $xf        = $this->_XF($format);    // The cell format
1793
        $xf        = $this->_XF($format);    // The cell format
1794
 
1794
 
1795
        // Check that row and col are valid and store max and min values
1795
        // Check that row and col are valid and store max and min values
1796
        if ($row >= $this->_xls_rowmax) {
1796
        if ($row >= $this->_xls_rowmax) {
1797
            return(-2);
1797
            return(-2);
1798
        }
1798
        }
1799
        if ($col >= $this->_xls_colmax) {
1799
        if ($col >= $this->_xls_colmax) {
1800
            return(-2);
1800
            return(-2);
1801
        }
1801
        }
1802
        if ($row <  $this->_dim_rowmin) {
1802
        if ($row <  $this->_dim_rowmin) {
1803
            $this->_dim_rowmin = $row;
1803
            $this->_dim_rowmin = $row;
1804
        }
1804
        }
1805
        if ($row >  $this->_dim_rowmax) {
1805
        if ($row >  $this->_dim_rowmax) {
1806
            $this->_dim_rowmax = $row;
1806
            $this->_dim_rowmax = $row;
1807
        }
1807
        }
1808
        if ($col <  $this->_dim_colmin) {
1808
        if ($col <  $this->_dim_colmin) {
1809
            $this->_dim_colmin = $col;
1809
            $this->_dim_colmin = $col;
1810
        }
1810
        }
1811
        if ($col >  $this->_dim_colmax) {
1811
        if ($col >  $this->_dim_colmax) {
1812
            $this->_dim_colmax = $col;
1812
            $this->_dim_colmax = $col;
1813
        }
1813
        }
1814
 
1814
 
1815
        $header    = pack("vv",  $record, $length);
1815
        $header    = pack("vv",  $record, $length);
1816
        $data      = pack("vvv", $row, $col, $xf);
1816
        $data      = pack("vvv", $row, $col, $xf);
1817
		$data = $header . $data;
1817
		$data = $header . $data;
1818
 
1818
 
1819
        $this->_append($data);
1819
        $this->_append($data);
1820
        return 0;
1820
        return 0;
1821
    }
1821
    }
1822
 
1822
 
1823
    /**
1823
    /**
1824
    * Write a formula to the specified row and column (zero indexed).
1824
    * Write a formula to the specified row and column (zero indexed).
1825
    * The textual representation of the formula is passed to the parser in
1825
    * The textual representation of the formula is passed to the parser in
1826
    * Parser.php which returns a packed binary string.
1826
    * Parser.php which returns a packed binary string.
1827
    *
1827
    *
1828
    * Returns  0 : normal termination
1828
    * Returns  0 : normal termination
1829
    *         -1 : formula errors (bad formula)
1829
    *         -1 : formula errors (bad formula)
1830
    *         -2 : row or column out of range
1830
    *         -2 : row or column out of range
1831
    *
1831
    *
1832
    * @access public
1832
    * @access public
1833
    * @param integer $row     Zero indexed row
1833
    * @param integer $row     Zero indexed row
1834
    * @param integer $col     Zero indexed column
1834
    * @param integer $col     Zero indexed column
1835
    * @param string  $formula The formula text string
1835
    * @param string  $formula The formula text string
1836
    * @param mixed   $format  The optional XF format
1836
    * @param mixed   $format  The optional XF format
1837
    * @return integer
1837
    * @return integer
1838
    */
1838
    */
1839
    function writeFormula($row, $col, $formula, $format = null)
1839
    function writeFormula($row, $col, $formula, $format = null)
1840
    {
1840
    {
1841
        $record    = 0x0006;     // Record identifier
1841
        $record    = 0x0006;     // Record identifier
1842
 
1842
 
1843
        // Excel normally stores the last calculated value of the formula in $num.
1843
        // Excel normally stores the last calculated value of the formula in $num.
1844
        // Clearly we are not in a position to calculate this a priori. Instead
1844
        // Clearly we are not in a position to calculate this a priori. Instead
1845
        // we set $num to zero and set the option flags in $grbit to ensure
1845
        // we set $num to zero and set the option flags in $grbit to ensure
1846
        // automatic calculation of the formula when the file is opened.
1846
        // automatic calculation of the formula when the file is opened.
1847
        //
1847
        //
1848
        $xf        = $this->_XF($format); // The cell format
1848
        $xf        = $this->_XF($format); // The cell format
1849
        $num       = 0x00;                // Current value of formula
1849
        $num       = 0x00;                // Current value of formula
1850
        $grbit     = 0x03;                // Option flags
1850
        $grbit     = 0x03;                // Option flags
1851
        $unknown   = 0x0000;              // Must be zero
1851
        $unknown   = 0x0000;              // Must be zero
1852
 
1852
 
1853
 
1853
 
1854
        // Check that row and col are valid and store max and min values
1854
        // Check that row and col are valid and store max and min values
1855
        if ($this->_checkRowCol($row, $col) == false) {
1855
        if ($this->_checkRowCol($row, $col) == false) {
1856
            return -2;
1856
            return -2;
1857
        }
1857
        }
1858
 
1858
 
1859
        // Strip the '=' or '@' sign at the beginning of the formula string
1859
        // Strip the '=' or '@' sign at the beginning of the formula string
1860
        if (preg_match("/^=/", $formula)) {
1860
        if (preg_match("/^=/", $formula)) {
1861
            $formula = preg_replace("/(^=)/", "", $formula);
1861
            $formula = preg_replace("/(^=)/", "", $formula);
1862
        } elseif (preg_match("/^@/", $formula)) {
1862
        } elseif (preg_match("/^@/", $formula)) {
1863
            $formula = preg_replace("/(^@)/", "", $formula);
1863
            $formula = preg_replace("/(^@)/", "", $formula);
1864
        } else {
1864
        } else {
1865
            // Error handling
1865
            // Error handling
1866
            $this->writeString($row, $col, 'Unrecognised character for formula');
1866
            $this->writeString($row, $col, 'Unrecognised character for formula');
1867
            return -1;
1867
            return -1;
1868
        }
1868
        }
1869
 
1869
 
1870
        // Parse the formula using the parser in Parser.php
1870
        // Parse the formula using the parser in Parser.php
1871
        $error = $this->_parser->parse($formula);
1871
        $error = $this->_parser->parse($formula);
1872
        if ($this->isError($error)) {
1872
        if ($this->isError($error)) {
1873
            $this->writeString($row, $col, $error->getMessage());
1873
            $this->writeString($row, $col, $error->getMessage());
1874
            return -1;
1874
            return -1;
1875
        }
1875
        }
1876
 
1876
 
1877
        $formula = $this->_parser->toReversePolish();
1877
        $formula = $this->_parser->toReversePolish();
1878
        if ($this->isError($formula)) {
1878
        if ($this->isError($formula)) {
1879
            $this->writeString($row, $col, $formula->getMessage());
1879
            $this->writeString($row, $col, $formula->getMessage());
1880
            return -1;
1880
            return -1;
1881
        }
1881
        }
1882
 
1882
 
1883
        $formlen    = strlen($formula);    // Length of the binary string
1883
        $formlen    = strlen($formula);    // Length of the binary string
1884
        $length     = 0x16 + $formlen;     // Length of the record data
1884
        $length     = 0x16 + $formlen;     // Length of the record data
1885
 
1885
 
1886
        $header    = pack("vv",      $record, $length);
1886
        $header    = pack("vv",      $record, $length);
1887
        $data      = pack("vvvdvVv", $row, $col, $xf, $num,
1887
        $data      = pack("vvvdvVv", $row, $col, $xf, $num,
1888
                                     $grbit, $unknown, $formlen);
1888
                                     $grbit, $unknown, $formlen);
1889
		$data = $header . $data . $formula;
1889
		$data = $header . $data . $formula;
1890
 
1890
 
1891
        $this->_append($data);
1891
        $this->_append($data);
1892
        return 0;
1892
        return 0;
1893
    }
1893
    }
1894
 
1894
 
1895
    /**
1895
    /**
1896
    * Write a hyperlink.
1896
    * Write a hyperlink.
1897
    * This is comprised of two elements: the visible label and
1897
    * This is comprised of two elements: the visible label and
1898
    * the invisible link. The visible label is the same as the link unless an
1898
    * the invisible link. The visible label is the same as the link unless an
1899
    * alternative string is specified. The label is written using the
1899
    * alternative string is specified. The label is written using the
1900
    * writeString() method. Therefore the 255 characters string limit applies.
1900
    * writeString() method. Therefore the 255 characters string limit applies.
1901
    * $string and $format are optional.
1901
    * $string and $format are optional.
1902
    *
1902
    *
1903
    * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
1903
    * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
1904
    * directory url.
1904
    * directory url.
1905
    *
1905
    *
1906
    * Returns  0 : normal termination
1906
    * Returns  0 : normal termination
1907
    *         -2 : row or column out of range
1907
    *         -2 : row or column out of range
1908
    *         -3 : long string truncated to 255 chars
1908
    *         -3 : long string truncated to 255 chars
1909
    *
1909
    *
1910
    * @access public
1910
    * @access public
1911
    * @param integer $row    Row
1911
    * @param integer $row    Row
1912
    * @param integer $col    Column
1912
    * @param integer $col    Column
1913
    * @param string  $url    URL string
1913
    * @param string  $url    URL string
1914
    * @param string  $string Alternative label
1914
    * @param string  $string Alternative label
1915
    * @param mixed   $format The cell format
1915
    * @param mixed   $format The cell format
1916
    * @return integer
1916
    * @return integer
1917
    */
1917
    */
1918
    function writeUrl($row, $col, $url, $string = '', $format = null)
1918
    function writeUrl($row, $col, $url, $string = '', $format = null)
1919
    {
1919
    {
1920
        // Add start row and col to arg list
1920
        // Add start row and col to arg list
1921
        return($this->_writeUrlRange($row, $col, $row, $col, $url, $string, $format));
1921
        return($this->_writeUrlRange($row, $col, $row, $col, $url, $string, $format));
1922
    }
1922
    }
1923
 
1923
 
1924
    /**
1924
    /**
1925
    * This is the more general form of writeUrl(). It allows a hyperlink to be
1925
    * This is the more general form of writeUrl(). It allows a hyperlink to be
1926
    * written to a range of cells. This function also decides the type of hyperlink
1926
    * written to a range of cells. This function also decides the type of hyperlink
1927
    * to be written. These are either, Web (http, ftp, mailto), Internal
1927
    * to be written. These are either, Web (http, ftp, mailto), Internal
1928
    * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
1928
    * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
1929
    *
1929
    *
1930
    * @access private
1930
    * @access private
1931
    * @see writeUrl()
1931
    * @see writeUrl()
1932
    * @param integer $row1   Start row
1932
    * @param integer $row1   Start row
1933
    * @param integer $col1   Start column
1933
    * @param integer $col1   Start column
1934
    * @param integer $row2   End row
1934
    * @param integer $row2   End row
1935
    * @param integer $col2   End column
1935
    * @param integer $col2   End column
1936
    * @param string  $url    URL string
1936
    * @param string  $url    URL string
1937
    * @param string  $string Alternative label
1937
    * @param string  $string Alternative label
1938
    * @param mixed   $format The cell format
1938
    * @param mixed   $format The cell format
1939
    * @return integer
1939
    * @return integer
1940
    */
1940
    */
1941
 
1941
 
1942
    function _writeUrlRange($row1, $col1, $row2, $col2, $url, $string = '', $format = null)
1942
    function _writeUrlRange($row1, $col1, $row2, $col2, $url, $string = '', $format = null)
1943
    {
1943
    {
1944
 
1944
 
1945
        // Check for internal/external sheet links or default to web link
1945
        // Check for internal/external sheet links or default to web link
1946
        if (preg_match('[^internal:]', $url)) {
1946
        if (preg_match('[^internal:]', $url)) {
1947
            return($this->_writeUrlInternal($row1, $col1, $row2, $col2, $url, $string, $format));
1947
            return($this->_writeUrlInternal($row1, $col1, $row2, $col2, $url, $string, $format));
1948
        }
1948
        }
1949
        if (preg_match('[^external:]', $url)) {
1949
        if (preg_match('[^external:]', $url)) {
1950
            return($this->_writeUrlExternal($row1, $col1, $row2, $col2, $url, $string, $format));
1950
            return($this->_writeUrlExternal($row1, $col1, $row2, $col2, $url, $string, $format));
1951
        }
1951
        }
1952
        return($this->_writeUrlWeb($row1, $col1, $row2, $col2, $url, $string, $format));
1952
        return($this->_writeUrlWeb($row1, $col1, $row2, $col2, $url, $string, $format));
1953
    }
1953
    }
1954
 
1954
 
1955
 
1955
 
1956
    /**
1956
    /**
1957
    * Used to write http, ftp and mailto hyperlinks.
1957
    * Used to write http, ftp and mailto hyperlinks.
1958
    * The link type ($options) is 0x03 is the same as absolute dir ref without
1958
    * The link type ($options) is 0x03 is the same as absolute dir ref without
1959
    * sheet. However it is differentiated by the $unknown2 data stream.
1959
    * sheet. However it is differentiated by the $unknown2 data stream.
1960
    *
1960
    *
1961
    * @access private
1961
    * @access private
1962
    * @see writeUrl()
1962
    * @see writeUrl()
1963
    * @param integer $row1   Start row
1963
    * @param integer $row1   Start row
1964
    * @param integer $col1   Start column
1964
    * @param integer $col1   Start column
1965
    * @param integer $row2   End row
1965
    * @param integer $row2   End row
1966
    * @param integer $col2   End column
1966
    * @param integer $col2   End column
1967
    * @param string  $url    URL string
1967
    * @param string  $url    URL string
1968
    * @param string  $str    Alternative label
1968
    * @param string  $str    Alternative label
1969
    * @param mixed   $format The cell format
1969
    * @param mixed   $format The cell format
1970
    * @return integer
1970
    * @return integer
1971
    */
1971
    */
1972
    function _writeUrlWeb($row1, $col1, $row2, $col2, $url, $str, $format = null)
1972
    function _writeUrlWeb($row1, $col1, $row2, $col2, $url, $str, $format = null)
1973
    {
1973
    {
1974
        $record      = 0x01B8;                       // Record identifier
1974
        $record      = 0x01B8;                       // Record identifier
1975
        $length      = 0x00000;                      // Bytes to follow
1975
        $length      = 0x00000;                      // Bytes to follow
1976
 
1976
 
1977
        if (!$format) {
1977
        if (!$format) {
1978
            $format = $this->_url_format;
1978
            $format = $this->_url_format;
1979
        }
1979
        }
1980
 
1980
 
1981
        // Write the visible label using the writeString() method.
1981
        // Write the visible label using the writeString() method.
1982
        if ($str == '') {
1982
        if ($str == '') {
1983
            $str = $url;
1983
            $str = $url;
1984
        }
1984
        }
1985
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
1985
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
1986
        if (($str_error == -2) || ($str_error == -3)) {
1986
        if (($str_error == -2) || ($str_error == -3)) {
1987
            return $str_error;
1987
            return $str_error;
1988
        }
1988
        }
1989
 
1989
 
1990
        // Pack the undocumented parts of the hyperlink stream
1990
        // Pack the undocumented parts of the hyperlink stream
1991
        $unknown1    = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
1991
        $unknown1    = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
1992
        $unknown2    = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
1992
        $unknown2    = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
1993
 
1993
 
1994
        // Pack the option flags
1994
        // Pack the option flags
1995
        $options     = pack("V", 0x03);
1995
        $options     = pack("V", 0x03);
1996
 
1996
 
1997
        // Convert URL to a null terminated wchar string
1997
        // Convert URL to a null terminated wchar string
1998
        $url         = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
1998
        $url         = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
1999
        $url         = $url . "\0\0\0";
1999
        $url         = $url . "\0\0\0";
2000
 
2000
 
2001
        // Pack the length of the URL
2001
        // Pack the length of the URL
2002
        $url_len     = pack("V", strlen($url));
2002
        $url_len     = pack("V", strlen($url));
2003
 
2003
 
2004
        // Calculate the data length
2004
        // Calculate the data length
2005
        $length      = 0x34 + strlen($url);
2005
        $length      = 0x34 + strlen($url);
2006
 
2006
 
2007
        // Pack the header data
2007
        // Pack the header data
2008
        $header      = pack("vv",   $record, $length);
2008
        $header      = pack("vv",   $record, $length);
2009
        $data        = pack("vvvv", $row1, $row2, $col1, $col2);
2009
        $data        = pack("vvvv", $row1, $row2, $col1, $col2);
2010
 
2010
 
2011
		$data = $header . $data . $unknown1 . $options . $unknown2 . $url_len . $url;
2011
		$data = $header . $data . $unknown1 . $options . $unknown2 . $url_len . $url;
2012
 
2012
 
2013
        // Write the packed data
2013
        // Write the packed data
2014
        $this->_append($data);
2014
        $this->_append($data);
2015
        return($str_error);
2015
        return($str_error);
2016
    }
2016
    }
2017
 
2017
 
2018
    /**
2018
    /**
2019
    * Used to write internal reference hyperlinks such as "Sheet1!A1".
2019
    * Used to write internal reference hyperlinks such as "Sheet1!A1".
2020
    *
2020
    *
2021
    * @access private
2021
    * @access private
2022
    * @see writeUrl()
2022
    * @see writeUrl()
2023
    * @param integer $row1   Start row
2023
    * @param integer $row1   Start row
2024
    * @param integer $col1   Start column
2024
    * @param integer $col1   Start column
2025
    * @param integer $row2   End row
2025
    * @param integer $row2   End row
2026
    * @param integer $col2   End column
2026
    * @param integer $col2   End column
2027
    * @param string  $url    URL string
2027
    * @param string  $url    URL string
2028
    * @param string  $str    Alternative label
2028
    * @param string  $str    Alternative label
2029
    * @param mixed   $format The cell format
2029
    * @param mixed   $format The cell format
2030
    * @return integer
2030
    * @return integer
2031
    */
2031
    */
2032
    function _writeUrlInternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
2032
    function _writeUrlInternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
2033
    {
2033
    {
2034
        $record      = 0x01B8;                       // Record identifier
2034
        $record      = 0x01B8;                       // Record identifier
2035
        $length      = 0x00000;                      // Bytes to follow
2035
        $length      = 0x00000;                      // Bytes to follow
2036
 
2036
 
2037
        if (!$format) {
2037
        if (!$format) {
2038
            $format = $this->_url_format;
2038
            $format = $this->_url_format;
2039
        }
2039
        }
2040
 
2040
 
2041
        // Strip URL type
2041
        // Strip URL type
2042
        $url = preg_replace('/^internal:/', '', $url);
2042
        $url = preg_replace('/^internal:/', '', $url);
2043
 
2043
 
2044
        // Write the visible label
2044
        // Write the visible label
2045
        if ($str == '') {
2045
        if ($str == '') {
2046
            $str = $url;
2046
            $str = $url;
2047
        }
2047
        }
2048
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
2048
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
2049
        if (($str_error == -2) || ($str_error == -3)) {
2049
        if (($str_error == -2) || ($str_error == -3)) {
2050
            return $str_error;
2050
            return $str_error;
2051
        }
2051
        }
2052
 
2052
 
2053
        // Pack the undocumented parts of the hyperlink stream
2053
        // Pack the undocumented parts of the hyperlink stream
2054
        $unknown1    = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
2054
        $unknown1    = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
2055
 
2055
 
2056
        // Pack the option flags
2056
        // Pack the option flags
2057
        $options     = pack("V", 0x08);
2057
        $options     = pack("V", 0x08);
2058
 
2058
 
2059
        // Convert the URL type and to a null terminated wchar string
2059
        // Convert the URL type and to a null terminated wchar string
2060
        $url         = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
2060
        $url         = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
2061
        $url         = $url . "\0\0\0";
2061
        $url         = $url . "\0\0\0";
2062
 
2062
 
2063
        // Pack the length of the URL as chars (not wchars)
2063
        // Pack the length of the URL as chars (not wchars)
2064
        $url_len     = pack("V", floor(strlen($url)/2));
2064
        $url_len     = pack("V", floor(strlen($url)/2));
2065
 
2065
 
2066
        // Calculate the data length
2066
        // Calculate the data length
2067
        $length      = 0x24 + strlen($url);
2067
        $length      = 0x24 + strlen($url);
2068
 
2068
 
2069
        // Pack the header data
2069
        // Pack the header data
2070
        $header      = pack("vv",   $record, $length);
2070
        $header      = pack("vv",   $record, $length);
2071
        $data        = pack("vvvv", $row1, $row2, $col1, $col2);
2071
        $data        = pack("vvvv", $row1, $row2, $col1, $col2);
2072
 
2072
 
2073
		$data = $header . $data . $unknown1 . $options . $url_len . $url;
2073
		$data = $header . $data . $unknown1 . $options . $url_len . $url;
2074
 
2074
 
2075
        // Write the packed data
2075
        // Write the packed data
2076
        $this->_append($data);
2076
        $this->_append($data);
2077
        return($str_error);
2077
        return($str_error);
2078
    }
2078
    }
2079
 
2079
 
2080
    /**
2080
    /**
2081
    * Write links to external directory names such as 'c:\foo.xls',
2081
    * Write links to external directory names such as 'c:\foo.xls',
2082
    * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
2082
    * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
2083
    *
2083
    *
2084
    * Note: Excel writes some relative links with the $dir_long string. We ignore
2084
    * Note: Excel writes some relative links with the $dir_long string. We ignore
2085
    * these cases for the sake of simpler code.
2085
    * these cases for the sake of simpler code.
2086
    *
2086
    *
2087
    * @access private
2087
    * @access private
2088
    * @see writeUrl()
2088
    * @see writeUrl()
2089
    * @param integer $row1   Start row
2089
    * @param integer $row1   Start row
2090
    * @param integer $col1   Start column
2090
    * @param integer $col1   Start column
2091
    * @param integer $row2   End row
2091
    * @param integer $row2   End row
2092
    * @param integer $col2   End column
2092
    * @param integer $col2   End column
2093
    * @param string  $url    URL string
2093
    * @param string  $url    URL string
2094
    * @param string  $str    Alternative label
2094
    * @param string  $str    Alternative label
2095
    * @param mixed   $format The cell format
2095
    * @param mixed   $format The cell format
2096
    * @return integer
2096
    * @return integer
2097
    */
2097
    */
2098
    function _writeUrlExternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
2098
    function _writeUrlExternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
2099
    {
2099
    {
2100
        // Network drives are different. We will handle them separately
2100
        // Network drives are different. We will handle them separately
2101
        // MS/Novell network drives and shares start with \\
2101
        // MS/Novell network drives and shares start with \\
2102
        if (preg_match('[^external:\\\\]', $url)) {
2102
        if (preg_match('[^external:\\\\]', $url)) {
2103
            return; //($this->_writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
2103
            return; //($this->_writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
2104
        }
2104
        }
2105
    
2105
    
2106
        $record      = 0x01B8;                       // Record identifier
2106
        $record      = 0x01B8;                       // Record identifier
2107
        $length      = 0x00000;                      // Bytes to follow
2107
        $length      = 0x00000;                      // Bytes to follow
2108
    
2108
    
2109
        if (!$format) {
2109
        if (!$format) {
2110
            $format = $this->_url_format;
2110
            $format = $this->_url_format;
2111
        }
2111
        }
2112
    
2112
    
2113
        // Strip URL type and change Unix dir separator to Dos style (if needed)
2113
        // Strip URL type and change Unix dir separator to Dos style (if needed)
2114
        //
2114
        //
2115
        $url = preg_replace('/^external:/', '', $url);
2115
        $url = preg_replace('/^external:/', '', $url);
2116
        $url = preg_replace('/\//', "\\", $url);
2116
        $url = preg_replace('/\//', "\\", $url);
2117
    
2117
    
2118
        // Write the visible label
2118
        // Write the visible label
2119
        if ($str == '') {
2119
        if ($str == '') {
2120
            $str = preg_replace('/\#/', ' - ', $url);
2120
            $str = preg_replace('/\#/', ' - ', $url);
2121
        }
2121
        }
2122
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
2122
        $str_error = is_numeric($str) ? $this->writeNumber($row1, $col1, $str, $format) : $this->writeString($row1, $col1, $str, $format);
2123
        if (($str_error == -2) or ($str_error == -3)) {
2123
        if (($str_error == -2) or ($str_error == -3)) {
2124
            return $str_error;
2124
            return $str_error;
2125
        }
2125
        }
2126
    
2126
    
2127
        // Determine if the link is relative or absolute:
2127
        // Determine if the link is relative or absolute:
2128
        //   relative if link contains no dir separator, "somefile.xls"
2128
        //   relative if link contains no dir separator, "somefile.xls"
2129
        //   relative if link starts with up-dir, "..\..\somefile.xls"
2129
        //   relative if link starts with up-dir, "..\..\somefile.xls"
2130
        //   otherwise, absolute
2130
        //   otherwise, absolute
2131
        
2131
        
2132
        $absolute    = 0x02; // Bit mask
2132
        $absolute    = 0x02; // Bit mask
2133
        if (!preg_match("/\\\/", $url)) {
2133
        if (!preg_match("/\\\/", $url)) {
2134
            $absolute    = 0x00;
2134
            $absolute    = 0x00;
2135
        }
2135
        }
2136
        if (preg_match("/^\.\.\\\/", $url)) {
2136
        if (preg_match("/^\.\.\\\/", $url)) {
2137
            $absolute    = 0x00;
2137
            $absolute    = 0x00;
2138
        }
2138
        }
2139
        $link_type               = 0x01 | $absolute;
2139
        $link_type               = 0x01 | $absolute;
2140
    
2140
    
2141
        // Determine if the link contains a sheet reference and change some of the
2141
        // Determine if the link contains a sheet reference and change some of the
2142
        // parameters accordingly.
2142
        // parameters accordingly.
2143
        // Split the dir name and sheet name (if it exists)
2143
        // Split the dir name and sheet name (if it exists)
2144
        /*if (preg_match("/\#/", $url)) {
2144
        /*if (preg_match("/\#/", $url)) {
2145
            list($dir_long, $sheet) = split("\#", $url);
2145
            list($dir_long, $sheet) = split("\#", $url);
2146
        } else {
2146
        } else {
2147
            $dir_long = $url;
2147
            $dir_long = $url;
2148
        }
2148
        }
2149
    
2149
    
2150
        if (isset($sheet)) {
2150
        if (isset($sheet)) {
2151
            $link_type |= 0x08;
2151
            $link_type |= 0x08;
2152
            $sheet_len  = pack("V", strlen($sheet) + 0x01);
2152
            $sheet_len  = pack("V", strlen($sheet) + 0x01);
2153
            $sheet      = join("\0", split('', $sheet));
2153
            $sheet      = join("\0", split('', $sheet));
2154
            $sheet     .= "\0\0\0";
2154
            $sheet     .= "\0\0\0";
2155
        } else {
2155
        } else {
2156
            $sheet_len   = '';
2156
            $sheet_len   = '';
2157
            $sheet       = '';
2157
            $sheet       = '';
2158
        }*/
2158
        }*/
2159
        $dir_long = $url;
2159
        $dir_long = $url;
2160
        if (preg_match("/\#/", $url)) {
2160
        if (preg_match("/\#/", $url)) {
2161
            $link_type |= 0x08;
2161
            $link_type |= 0x08;
2162
        }
2162
        }
2163
 
2163
 
2164
 
2164
 
2165
    
2165
    
2166
        // Pack the link type
2166
        // Pack the link type
2167
        $link_type   = pack("V", $link_type);
2167
        $link_type   = pack("V", $link_type);
2168
    
2168
    
2169
        // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
2169
        // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
2170
        $up_count    = preg_match_all("/\.\.\\\/", $dir_long, $useless);
2170
        $up_count    = preg_match_all("/\.\.\\\/", $dir_long, $useless);
2171
        $up_count    = pack("v", $up_count);
2171
        $up_count    = pack("v", $up_count);
2172
    
2172
    
2173
        // Store the short dos dir name (null terminated)
2173
        // Store the short dos dir name (null terminated)
2174
        $dir_short   = preg_replace("/\.\.\\\/", '', $dir_long) . "\0";
2174
        $dir_short   = preg_replace("/\.\.\\\/", '', $dir_long) . "\0";
2175
    
2175
    
2176
        // Store the long dir name as a wchar string (non-null terminated)
2176
        // Store the long dir name as a wchar string (non-null terminated)
2177
        //$dir_long       = join("\0", split('', $dir_long));
2177
        //$dir_long       = join("\0", split('', $dir_long));
2178
        $dir_long       = $dir_long . "\0";
2178
        $dir_long       = $dir_long . "\0";
2179
    
2179
    
2180
        // Pack the lengths of the dir strings
2180
        // Pack the lengths of the dir strings
2181
        $dir_short_len = pack("V", strlen($dir_short)      );
2181
        $dir_short_len = pack("V", strlen($dir_short)      );
2182
        $dir_long_len  = pack("V", strlen($dir_long)       );
2182
        $dir_long_len  = pack("V", strlen($dir_long)       );
2183
        $stream_len    = pack("V", 0);//strlen($dir_long) + 0x06);
2183
        $stream_len    = pack("V", 0);//strlen($dir_long) + 0x06);
2184
    
2184
    
2185
        // Pack the undocumented parts of the hyperlink stream
2185
        // Pack the undocumented parts of the hyperlink stream
2186
        $unknown1 = pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000'       );
2186
        $unknown1 = pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000'       );
2187
        $unknown2 = pack("H*",'0303000000000000C000000000000046'               );
2187
        $unknown2 = pack("H*",'0303000000000000C000000000000046'               );
2188
        $unknown3 = pack("H*",'FFFFADDE000000000000000000000000000000000000000');
2188
        $unknown3 = pack("H*",'FFFFADDE000000000000000000000000000000000000000');
2189
        $unknown4 = pack("v",  0x03                                            );
2189
        $unknown4 = pack("v",  0x03                                            );
2190
    
2190
    
2191
        // Pack the main data stream
2191
        // Pack the main data stream
2192
        $data        = pack("vvvv", $row1, $row2, $col1, $col2) .
2192
        $data        = pack("vvvv", $row1, $row2, $col1, $col2) .
2193
                          $unknown1     .
2193
                          $unknown1     .
2194
                          $link_type    .
2194
                          $link_type    .
2195
                          $unknown2     .
2195
                          $unknown2     .
2196
                          $up_count     .
2196
                          $up_count     .
2197
                          $dir_short_len.
2197
                          $dir_short_len.
2198
                          $dir_short    .
2198
                          $dir_short    .
2199
                          $unknown3     .
2199
                          $unknown3     .
2200
                          $stream_len   ;/*.
2200
                          $stream_len   ;/*.
2201
                          $dir_long_len .
2201
                          $dir_long_len .
2202
                          $unknown4     .
2202
                          $unknown4     .
2203
                          $dir_long     .
2203
                          $dir_long     .
2204
                          $sheet_len    .
2204
                          $sheet_len    .
2205
                          $sheet        ;*/
2205
                          $sheet        ;*/
2206
    
2206
    
2207
        // Pack the header data
2207
        // Pack the header data
2208
        $length   = strlen($data);
2208
        $length   = strlen($data);
2209
        $header   = pack("vv", $record, $length);
2209
        $header   = pack("vv", $record, $length);
2210
		$data = $header. $data;
2210
		$data = $header. $data;
2211
 
2211
 
2212
        // Write the packed data
2212
        // Write the packed data
2213
        $this->_append($data);
2213
        $this->_append($data);
2214
        return($str_error);
2214
        return($str_error);
2215
    }
2215
    }
2216
 
2216
 
2217
 
2217
 
2218
    /**
2218
    /**
2219
    * This method is used to set the height and format for a row.
2219
    * This method is used to set the height and format for a row.
2220
    *
2220
    *
2221
    * @access public
2221
    * @access public
2222
    * @param integer $row    The row to set
2222
    * @param integer $row    The row to set
2223
    * @param integer $height Height we are giving to the row.
2223
    * @param integer $height Height we are giving to the row.
2224
    *                        Use null to set XF without setting height
2224
    *                        Use null to set XF without setting height
2225
    * @param mixed   $format XF format we are giving to the row
2225
    * @param mixed   $format XF format we are giving to the row
2226
    * @param bool    $hidden The optional hidden attribute
2226
    * @param bool    $hidden The optional hidden attribute
2227
    * @param integer $level  The optional outline level for row, in range [0,7]
2227
    * @param integer $level  The optional outline level for row, in range [0,7]
2228
    */
2228
    */
2229
    function setRow($row, $height, $format = null, $hidden = false, $level = 0)
2229
    function setRow($row, $height, $format = null, $hidden = false, $level = 0)
2230
    {
2230
    {
2231
        $record      = 0x0208;               // Record identifier
2231
        $record      = 0x0208;               // Record identifier
2232
        $length      = 0x0010;               // Number of bytes to follow
2232
        $length      = 0x0010;               // Number of bytes to follow
2233
 
2233
 
2234
        $colMic      = 0x0000;               // First defined column
2234
        $colMic      = 0x0000;               // First defined column
2235
        $colMac      = 0x0000;               // Last defined column
2235
        $colMac      = 0x0000;               // Last defined column
2236
        $irwMac      = 0x0000;               // Used by Excel to optimise loading
2236
        $irwMac      = 0x0000;               // Used by Excel to optimise loading
2237
        $reserved    = 0x0000;               // Reserved
2237
        $reserved    = 0x0000;               // Reserved
2238
        $grbit       = 0x0000;               // Option flags
2238
        $grbit       = 0x0000;               // Option flags
2239
        $ixfe        = $this->_XF($format);  // XF index
2239
        $ixfe        = $this->_XF($format);  // XF index
2240
 
2240
 
2241
        // set _row_sizes so _sizeRow() can use it
2241
        // set _row_sizes so _sizeRow() can use it
2242
        $this->_row_sizes[$row] = $height;
2242
        $this->_row_sizes[$row] = $height;
2243
 
2243
 
2244
        // Use setRow($row, null, $XF) to set XF format without setting height
2244
        // Use setRow($row, null, $XF) to set XF format without setting height
2245
        if ($height != null) {
2245
        if ($height != null) {
2246
            $miyRw = $height * 20;  // row height
2246
            $miyRw = $height * 20;  // row height
2247
        } else {
2247
        } else {
2248
            $miyRw = 0xff;          // default row height is 256
2248
            $miyRw = 0xff;          // default row height is 256
2249
        }
2249
        }
2250
 
2250
 
2251
        $level = max(0, min($level, 7));  // level should be between 0 and 7
2251
        $level = max(0, min($level, 7));  // level should be between 0 and 7
2252
        $this->_outline_row_level = max($level, $this->_outline_row_level);
2252
        $this->_outline_row_level = max($level, $this->_outline_row_level);
2253
 
2253
 
2254
 
2254
 
2255
        // Set the options flags. fUnsynced is used to show that the font and row
2255
        // Set the options flags. fUnsynced is used to show that the font and row
2256
        // heights are not compatible. This is usually the case for WriteExcel.
2256
        // heights are not compatible. This is usually the case for WriteExcel.
2257
        // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
2257
        // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
2258
        // is collapsed. Instead it is used to indicate that the previous row is
2258
        // is collapsed. Instead it is used to indicate that the previous row is
2259
        // collapsed. The zero height flag, 0x20, is used to collapse a row.
2259
        // collapsed. The zero height flag, 0x20, is used to collapse a row.
2260
 
2260
 
2261
        $grbit |= $level;
2261
        $grbit |= $level;
2262
        if ($hidden) {
2262
        if ($hidden) {
2263
            $grbit |= 0x0020;
2263
            $grbit |= 0x0020;
2264
        }
2264
        }
2265
        $grbit |= 0x0040; // fUnsynced
2265
        $grbit |= 0x0040; // fUnsynced
2266
        if ($format) {
2266
        if ($format) {
2267
            $grbit |= 0x0080;
2267
            $grbit |= 0x0080;
2268
        }
2268
        }
2269
        $grbit |= 0x0100;
2269
        $grbit |= 0x0100;
2270
 
2270
 
2271
        $header   = pack("vv",       $record, $length);
2271
        $header   = pack("vv",       $record, $length);
2272
        $data     = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw,
2272
        $data     = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw,
2273
                                     $irwMac,$reserved, $grbit, $ixfe);
2273
                                     $irwMac,$reserved, $grbit, $ixfe);
2274
		$data = $header . $data;
2274
		$data = $header . $data;
2275
        $this->_append($data);
2275
        $this->_append($data);
2276
    }
2276
    }
2277
 
2277
 
2278
    /**
2278
    /**
2279
    * Writes Excel DIMENSIONS to define the area in which there is data.
2279
    * Writes Excel DIMENSIONS to define the area in which there is data.
2280
    *
2280
    *
2281
    * @access private
2281
    * @access private
2282
    */
2282
    */
2283
    function _storeDimensions()
2283
    function _storeDimensions()
2284
    {
2284
    {
2285
        $record    = 0x0200;                 // Record identifier
2285
        $record    = 0x0200;                 // Record identifier
2286
        $row_min   = $this->_dim_rowmin;     // First row
2286
        $row_min   = $this->_dim_rowmin;     // First row
2287
        $row_max   = $this->_dim_rowmax + 1; // Last row plus 1
2287
        $row_max   = $this->_dim_rowmax + 1; // Last row plus 1
2288
        $col_min   = $this->_dim_colmin;     // First column
2288
        $col_min   = $this->_dim_colmin;     // First column
2289
        $col_max   = $this->_dim_colmax + 1; // Last column plus 1
2289
        $col_max   = $this->_dim_colmax + 1; // Last column plus 1
2290
        $reserved  = 0x0000;                 // Reserved by Excel
2290
        $reserved  = 0x0000;                 // Reserved by Excel
2291
 
2291
 
2292
        if ($this->_BIFF_version == 0x0500) {
2292
        if ($this->_BIFF_version == 0x0500) {
2293
            $length    = 0x000A;               // Number of bytes to follow
2293
            $length    = 0x000A;               // Number of bytes to follow
2294
            $data      = pack("vvvvv", $row_min, $row_max,
2294
            $data      = pack("vvvvv", $row_min, $row_max,
2295
                                       $col_min, $col_max, $reserved);
2295
                                       $col_min, $col_max, $reserved);
2296
        } elseif ($this->_BIFF_version == 0x0600) {
2296
        } elseif ($this->_BIFF_version == 0x0600) {
2297
            $length    = 0x000E;
2297
            $length    = 0x000E;
2298
            $data      = pack("VVvvv", $row_min, $row_max,
2298
            $data      = pack("VVvvv", $row_min, $row_max,
2299
                                       $col_min, $col_max, $reserved);
2299
                                       $col_min, $col_max, $reserved);
2300
        }
2300
        }
2301
        $header = pack("vv", $record, $length);
2301
        $header = pack("vv", $record, $length);
2302
        $this->_prepend($header.$data);
2302
        $this->_prepend($header.$data);
2303
    }
2303
    }
2304
 
2304
 
2305
    /**
2305
    /**
2306
    * Write BIFF record Window2.
2306
    * Write BIFF record Window2.
2307
    *
2307
    *
2308
    * @access private
2308
    * @access private
2309
    */
2309
    */
2310
    function _storeWindow2()
2310
    function _storeWindow2()
2311
    {
2311
    {
2312
        $record         = 0x023E;     // Record identifier
2312
        $record         = 0x023E;     // Record identifier
2313
        if ($this->_BIFF_version == 0x0500) {
2313
        if ($this->_BIFF_version == 0x0500) {
2314
            $length         = 0x000A;     // Number of bytes to follow
2314
            $length         = 0x000A;     // Number of bytes to follow
2315
        } elseif ($this->_BIFF_version == 0x0600) {
2315
        } elseif ($this->_BIFF_version == 0x0600) {
2316
            $length         = 0x0012;
2316
            $length         = 0x0012;
2317
        }
2317
        }
2318
 
2318
 
2319
        $grbit          = 0x00B6;     // Option flags
2319
        $grbit          = 0x00B6;     // Option flags
2320
        $rwTop          = 0x0000;     // Top row visible in window
2320
        $rwTop          = 0x0000;     // Top row visible in window
2321
        $colLeft        = 0x0000;     // Leftmost column visible in window
2321
        $colLeft        = 0x0000;     // Leftmost column visible in window
2322
 
2322
 
2323
 
2323
 
2324
        // The options flags that comprise $grbit
2324
        // The options flags that comprise $grbit
2325
        $fDspFmla       = 0;                     // 0 - bit
2325
        $fDspFmla       = 0;                     // 0 - bit
2326
        $fDspGrid       = $this->_screen_gridlines; // 1
2326
        $fDspGrid       = $this->_screen_gridlines; // 1
2327
        $fDspRwCol      = 1;                     // 2
2327
        $fDspRwCol      = 1;                     // 2
2328
        $fFrozen        = $this->_frozen;        // 3
2328
        $fFrozen        = $this->_frozen;        // 3
2329
        $fDspZeros      = 1;                     // 4
2329
        $fDspZeros      = 1;                     // 4
2330
        $fDefaultHdr    = 1;                     // 5
2330
        $fDefaultHdr    = 1;                     // 5
2331
        $fArabic        = $this->_Arabic;        // 6
2331
        $fArabic        = $this->_Arabic;        // 6
2332
        $fDspGuts       = $this->_outline_on;    // 7
2332
        $fDspGuts       = $this->_outline_on;    // 7
2333
        $fFrozenNoSplit = 0;                     // 0 - bit
2333
        $fFrozenNoSplit = 0;                     // 0 - bit
2334
        $fSelected      = $this->selected;       // 1
2334
        $fSelected      = $this->selected;       // 1
2335
        $fPaged         = 1;                     // 2
2335
        $fPaged         = 1;                     // 2
2336
 
2336
 
2337
        $grbit             = $fDspFmla;
2337
        $grbit             = $fDspFmla;
2338
        $grbit            |= $fDspGrid       << 1;
2338
        $grbit            |= $fDspGrid       << 1;
2339
        $grbit            |= $fDspRwCol      << 2;
2339
        $grbit            |= $fDspRwCol      << 2;
2340
        $grbit            |= $fFrozen        << 3;
2340
        $grbit            |= $fFrozen        << 3;
2341
        $grbit            |= $fDspZeros      << 4;
2341
        $grbit            |= $fDspZeros      << 4;
2342
        $grbit            |= $fDefaultHdr    << 5;
2342
        $grbit            |= $fDefaultHdr    << 5;
2343
        $grbit            |= $fArabic        << 6;
2343
        $grbit            |= $fArabic        << 6;
2344
        $grbit            |= $fDspGuts       << 7;
2344
        $grbit            |= $fDspGuts       << 7;
2345
        $grbit            |= $fFrozenNoSplit << 8;
2345
        $grbit            |= $fFrozenNoSplit << 8;
2346
        $grbit            |= $fSelected      << 9;
2346
        $grbit            |= $fSelected      << 9;
2347
        $grbit            |= $fPaged         << 10;
2347
        $grbit            |= $fPaged         << 10;
2348
 
2348
 
2349
        $header  = pack("vv",   $record, $length);
2349
        $header  = pack("vv",   $record, $length);
2350
        $data    = pack("vvv", $grbit, $rwTop, $colLeft);
2350
        $data    = pack("vvv", $grbit, $rwTop, $colLeft);
2351
        // FIXME !!!
2351
        // FIXME !!!
2352
        if ($this->_BIFF_version == 0x0500) {
2352
        if ($this->_BIFF_version == 0x0500) {
2353
            $rgbHdr         = 0x00000000; // Row/column heading and gridline color
2353
            $rgbHdr         = 0x00000000; // Row/column heading and gridline color
2354
            $data .= pack("V", $rgbHdr);
2354
            $data .= pack("V", $rgbHdr);
2355
        } elseif ($this->_BIFF_version == 0x0600) {
2355
        } elseif ($this->_BIFF_version == 0x0600) {
2356
            $rgbHdr       = 0x0040; // Row/column heading and gridline color index
2356
            $rgbHdr       = 0x0040; // Row/column heading and gridline color index
2357
            $zoom_factor_page_break = 0x0000;
2357
            $zoom_factor_page_break = 0x0000;
2358
            $zoom_factor_normal     = 0x0000;
2358
            $zoom_factor_normal     = 0x0000;
2359
            $data .= pack("vvvvV", $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
2359
            $data .= pack("vvvvV", $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
2360
        }
2360
        }
2361
		$data = $header.$data;
2361
		$data = $header.$data;
2362
 
2362
 
2363
        $this->_append($data);
2363
        $this->_append($data);
2364
    }
2364
    }
2365
 
2365
 
2366
    /**
2366
    /**
2367
    * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
2367
    * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
2368
    *
2368
    *
2369
    * @access private
2369
    * @access private
2370
    */
2370
    */
2371
    function _storeDefcol()
2371
    function _storeDefcol()
2372
    {
2372
    {
2373
        $record   = 0x0055;      // Record identifier
2373
        $record   = 0x0055;      // Record identifier
2374
        $length   = 0x0002;      // Number of bytes to follow
2374
        $length   = 0x0002;      // Number of bytes to follow
2375
        $colwidth = 0x0008;      // Default column width
2375
        $colwidth = 0x0008;      // Default column width
2376
 
2376
 
2377
        $header   = pack("vv", $record, $length);
2377
        $header   = pack("vv", $record, $length);
2378
        $data     = pack("v",  $colwidth);
2378
        $data     = pack("v",  $colwidth);
2379
        $this->_prepend($header . $data);
2379
        $this->_prepend($header . $data);
2380
    }
2380
    }
2381
 
2381
 
2382
    /**
2382
    /**
2383
    * Write BIFF record COLINFO to define column widths
2383
    * Write BIFF record COLINFO to define column widths
2384
    *
2384
    *
2385
    * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
2385
    * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
2386
    * length record.
2386
    * length record.
2387
    *
2387
    *
2388
    * @access private
2388
    * @access private
2389
    * @param array $col_array This is the only parameter received and is composed of the following:
2389
    * @param array $col_array This is the only parameter received and is composed of the following:
2390
    *                0 => First formatted column,
2390
    *                0 => First formatted column,
2391
    *                1 => Last formatted column,
2391
    *                1 => Last formatted column,
2392
    *                2 => Col width (8.43 is Excel default),
2392
    *                2 => Col width (8.43 is Excel default),
2393
    *                3 => The optional XF format of the column,
2393
    *                3 => The optional XF format of the column,
2394
    *                4 => Option flags.
2394
    *                4 => Option flags.
2395
    *                5 => Optional outline level
2395
    *                5 => Optional outline level
2396
    */
2396
    */
2397
    function _storeColinfo($col_array)
2397
    function _storeColinfo($col_array)
2398
    {
2398
    {
2399
        if (isset($col_array[0])) {
2399
        if (isset($col_array[0])) {
2400
            $colFirst = $col_array[0];
2400
            $colFirst = $col_array[0];
2401
        }
2401
        }
2402
        if (isset($col_array[1])) {
2402
        if (isset($col_array[1])) {
2403
            $colLast = $col_array[1];
2403
            $colLast = $col_array[1];
2404
        }
2404
        }
2405
        if (isset($col_array[2])) {
2405
        if (isset($col_array[2])) {
2406
            $coldx = $col_array[2];
2406
            $coldx = $col_array[2];
2407
        } else {
2407
        } else {
2408
            $coldx = 8.43;
2408
            $coldx = 8.43;
2409
        }
2409
        }
2410
        if (isset($col_array[3])) {
2410
        if (isset($col_array[3])) {
2411
            $format = $col_array[3];
2411
            $format = $col_array[3];
2412
        } else {
2412
        } else {
2413
            $format = 0;
2413
            $format = 0;
2414
        }
2414
        }
2415
        if (isset($col_array[4])) {
2415
        if (isset($col_array[4])) {
2416
            $grbit = $col_array[4];
2416
            $grbit = $col_array[4];
2417
        } else {
2417
        } else {
2418
            $grbit = 0;
2418
            $grbit = 0;
2419
        }
2419
        }
2420
        if (isset($col_array[5])) {
2420
        if (isset($col_array[5])) {
2421
            $level = $col_array[5];
2421
            $level = $col_array[5];
2422
        } else {
2422
        } else {
2423
            $level = 0;
2423
            $level = 0;
2424
        }
2424
        }
2425
        $record   = 0x007D;          // Record identifier
2425
        $record   = 0x007D;          // Record identifier
2426
        $length   = 0x000B;          // Number of bytes to follow
2426
        $length   = 0x000B;          // Number of bytes to follow
2427
 
2427
 
2428
        $coldx   += 0.72;            // Fudge. Excel subtracts 0.72 !?
2428
        $coldx   += 0.72;            // Fudge. Excel subtracts 0.72 !?
2429
        $coldx   *= 256;             // Convert to units of 1/256 of a char
2429
        $coldx   *= 256;             // Convert to units of 1/256 of a char
2430
 
2430
 
2431
        $ixfe     = $this->_XF($format);
2431
        $ixfe     = $this->_XF($format);
2432
        $reserved = 0x00;            // Reserved
2432
        $reserved = 0x00;            // Reserved
2433
 
2433
 
2434
        $level = max(0, min($level, 7));
2434
        $level = max(0, min($level, 7));
2435
        $grbit |= $level << 8;
2435
        $grbit |= $level << 8;
2436
 
2436
 
2437
        $header   = pack("vv",     $record, $length);
2437
        $header   = pack("vv",     $record, $length);
2438
        $data     = pack("vvvvvC", $colFirst, $colLast, $coldx,
2438
        $data     = pack("vvvvvC", $colFirst, $colLast, $coldx,
2439
                                   $ixfe, $grbit, $reserved);
2439
                                   $ixfe, $grbit, $reserved);
2440
        $this->_prepend($header.$data);
2440
        $this->_prepend($header.$data);
2441
    }
2441
    }
2442
 
2442
 
2443
    /**
2443
    /**
2444
    * Write BIFF record SELECTION.
2444
    * Write BIFF record SELECTION.
2445
    *
2445
    *
2446
    * @access private
2446
    * @access private
2447
    * @param array $array array containing ($rwFirst,$colFirst,$rwLast,$colLast)
2447
    * @param array $array array containing ($rwFirst,$colFirst,$rwLast,$colLast)
2448
    * @see setSelection()
2448
    * @see setSelection()
2449
    */
2449
    */
2450
    function _storeSelection($array)
2450
    function _storeSelection($array)
2451
    {
2451
    {
2452
        list($rwFirst,$colFirst,$rwLast,$colLast) = $array;
2452
        list($rwFirst,$colFirst,$rwLast,$colLast) = $array;
2453
        $record   = 0x001D;                  // Record identifier
2453
        $record   = 0x001D;                  // Record identifier
2454
        $length   = 0x000F;                  // Number of bytes to follow
2454
        $length   = 0x000F;                  // Number of bytes to follow
2455
 
2455
 
2456
        $pnn      = $this->_active_pane;     // Pane position
2456
        $pnn      = $this->_active_pane;     // Pane position
2457
        $rwAct    = $rwFirst;                // Active row
2457
        $rwAct    = $rwFirst;                // Active row
2458
        $colAct   = $colFirst;               // Active column
2458
        $colAct   = $colFirst;               // Active column
2459
        $irefAct  = 0;                       // Active cell ref
2459
        $irefAct  = 0;                       // Active cell ref
2460
        $cref     = 1;                       // Number of refs
2460
        $cref     = 1;                       // Number of refs
2461
 
2461
 
2462
        if (!isset($rwLast)) {
2462
        if (!isset($rwLast)) {
2463
            $rwLast   = $rwFirst;       // Last  row in reference
2463
            $rwLast   = $rwFirst;       // Last  row in reference
2464
        }
2464
        }
2465
        if (!isset($colLast)) {
2465
        if (!isset($colLast)) {
2466
            $colLast  = $colFirst;      // Last  col in reference
2466
            $colLast  = $colFirst;      // Last  col in reference
2467
        }
2467
        }
2468
 
2468
 
2469
        // Swap last row/col for first row/col as necessary
2469
        // Swap last row/col for first row/col as necessary
2470
        if ($rwFirst > $rwLast) {
2470
        if ($rwFirst > $rwLast) {
2471
            list($rwFirst, $rwLast) = array($rwLast, $rwFirst);
2471
            list($rwFirst, $rwLast) = array($rwLast, $rwFirst);
2472
        }
2472
        }
2473
 
2473
 
2474
        if ($colFirst > $colLast) {
2474
        if ($colFirst > $colLast) {
2475
            list($colFirst, $colLast) = array($colLast, $colFirst);
2475
            list($colFirst, $colLast) = array($colLast, $colFirst);
2476
        }
2476
        }
2477
 
2477
 
2478
        $header   = pack("vv",         $record, $length);
2478
        $header   = pack("vv",         $record, $length);
2479
        $data     = pack("CvvvvvvCC",  $pnn, $rwAct, $colAct,
2479
        $data     = pack("CvvvvvvCC",  $pnn, $rwAct, $colAct,
2480
                                       $irefAct, $cref,
2480
                                       $irefAct, $cref,
2481
                                       $rwFirst, $rwLast,
2481
                                       $rwFirst, $rwLast,
2482
                                       $colFirst, $colLast);
2482
                                       $colFirst, $colLast);
2483
		$data = $header . $data;
2483
		$data = $header . $data;
2484
 
2484
 
2485
        $this->_append($data);
2485
        $this->_append($data);
2486
    }
2486
    }
2487
 
2487
 
2488
    /**
2488
    /**
2489
    * Store the MERGEDCELLS record for all ranges of merged cells
2489
    * Store the MERGEDCELLS record for all ranges of merged cells
2490
    *
2490
    *
2491
    * @access private
2491
    * @access private
2492
    */
2492
    */
2493
    function _storeMergedCells()
2493
    function _storeMergedCells()
2494
    {
2494
    {
2495
        // if there are no merged cell ranges set, return
2495
        // if there are no merged cell ranges set, return
2496
        if (count($this->_merged_ranges) == 0) {
2496
        if (count($this->_merged_ranges) == 0) {
2497
            return;
2497
            return;
2498
        }
2498
        }
2499
        $record   = 0x00E5;
2499
        $record   = 0x00E5;
2500
        foreach($this->_merged_ranges as $ranges)
2500
        foreach($this->_merged_ranges as $ranges)
2501
          {
2501
          {
2502
            $length   = 2 + count($ranges) * 8; 
2502
            $length   = 2 + count($ranges) * 8; 
2503
            $header   = pack('vv', $record, $length);
2503
            $header   = pack('vv', $record, $length);
2504
            $data     = pack('v',  count($ranges));
2504
            $data     = pack('v',  count($ranges));
2505
            foreach($ranges as $range) 
2505
            foreach($ranges as $range) 
2506
              $data .= pack('vvvv', $range[0], $range[2], $range[1], $range[3]);
2506
              $data .= pack('vvvv', $range[0], $range[2], $range[1], $range[3]);
2507
            $string = $header . $data;
2507
            $string = $header . $data;
2508
 
2508
 
2509
            $this->_append($string);
2509
            $this->_append($string);
2510
          }
2510
          }
2511
    }
2511
    }
2512
 
2512
 
2513
    /**
2513
    /**
2514
    * Write BIFF record EXTERNCOUNT to indicate the number of external sheet
2514
    * Write BIFF record EXTERNCOUNT to indicate the number of external sheet
2515
    * references in a worksheet.
2515
    * references in a worksheet.
2516
    *
2516
    *
2517
    * Excel only stores references to external sheets that are used in formulas.
2517
    * Excel only stores references to external sheets that are used in formulas.
2518
    * For simplicity we store references to all the sheets in the workbook
2518
    * For simplicity we store references to all the sheets in the workbook
2519
    * regardless of whether they are used or not. This reduces the overall
2519
    * regardless of whether they are used or not. This reduces the overall
2520
    * complexity and eliminates the need for a two way dialogue between the formula
2520
    * complexity and eliminates the need for a two way dialogue between the formula
2521
    * parser the worksheet objects.
2521
    * parser the worksheet objects.
2522
    *
2522
    *
2523
    * @access private
2523
    * @access private
2524
    * @param integer $count The number of external sheet references in this worksheet
2524
    * @param integer $count The number of external sheet references in this worksheet
2525
    */
2525
    */
2526
    function _storeExterncount($count)
2526
    function _storeExterncount($count)
2527
    {
2527
    {
2528
        $record = 0x0016;          // Record identifier
2528
        $record = 0x0016;          // Record identifier
2529
        $length = 0x0002;          // Number of bytes to follow
2529
        $length = 0x0002;          // Number of bytes to follow
2530
 
2530
 
2531
        $header = pack("vv", $record, $length);
2531
        $header = pack("vv", $record, $length);
2532
        $data   = pack("v",  $count);
2532
        $data   = pack("v",  $count);
2533
        $this->_prepend($header . $data);
2533
        $this->_prepend($header . $data);
2534
    }
2534
    }
2535
 
2535
 
2536
    /**
2536
    /**
2537
    * Writes the Excel BIFF EXTERNSHEET record. These references are used by
2537
    * Writes the Excel BIFF EXTERNSHEET record. These references are used by
2538
    * formulas. A formula references a sheet name via an index. Since we store a
2538
    * formulas. A formula references a sheet name via an index. Since we store a
2539
    * reference to all of the external worksheets the EXTERNSHEET index is the same
2539
    * reference to all of the external worksheets the EXTERNSHEET index is the same
2540
    * as the worksheet index.
2540
    * as the worksheet index.
2541
    *
2541
    *
2542
    * @access private
2542
    * @access private
2543
    * @param string $sheetname The name of a external worksheet
2543
    * @param string $sheetname The name of a external worksheet
2544
    */
2544
    */
2545
    function _storeExternsheet($sheetname)
2545
    function _storeExternsheet($sheetname)
2546
    {
2546
    {
2547
        $record    = 0x0017;         // Record identifier
2547
        $record    = 0x0017;         // Record identifier
2548
 
2548
 
2549
        // References to the current sheet are encoded differently to references to
2549
        // References to the current sheet are encoded differently to references to
2550
        // external sheets.
2550
        // external sheets.
2551
        //
2551
        //
2552
        if ($this->name == $sheetname) {
2552
        if ($this->name == $sheetname) {
2553
            $sheetname = '';
2553
            $sheetname = '';
2554
            $length    = 0x02;  // The following 2 bytes
2554
            $length    = 0x02;  // The following 2 bytes
2555
            $cch       = 1;     // The following byte
2555
            $cch       = 1;     // The following byte
2556
            $rgch      = 0x02;  // Self reference
2556
            $rgch      = 0x02;  // Self reference
2557
        } else {
2557
        } else {
2558
            $length    = 0x02 + strlen($sheetname);
2558
            $length    = 0x02 + strlen($sheetname);
2559
            $cch       = strlen($sheetname);
2559
            $cch       = strlen($sheetname);
2560
            $rgch      = 0x03;  // Reference to a sheet in the current workbook
2560
            $rgch      = 0x03;  // Reference to a sheet in the current workbook
2561
        }
2561
        }
2562
 
2562
 
2563
        $header = pack("vv",  $record, $length);
2563
        $header = pack("vv",  $record, $length);
2564
        $data   = pack("CC", $cch, $rgch);
2564
        $data   = pack("CC", $cch, $rgch);
2565
        $this->_prepend($header . $data . $sheetname);
2565
        $this->_prepend($header . $data . $sheetname);
2566
    }
2566
    }
2567
 
2567
 
2568
    /**
2568
    /**
2569
    * Writes the Excel BIFF PANE record.
2569
    * Writes the Excel BIFF PANE record.
2570
    * The panes can either be frozen or thawed (unfrozen).
2570
    * The panes can either be frozen or thawed (unfrozen).
2571
    * Frozen panes are specified in terms of an integer number of rows and columns.
2571
    * Frozen panes are specified in terms of an integer number of rows and columns.
2572
    * Thawed panes are specified in terms of Excel's units for rows and columns.
2572
    * Thawed panes are specified in terms of Excel's units for rows and columns.
2573
    *
2573
    *
2574
    * @access private
2574
    * @access private
2575
    * @param array $panes This is the only parameter received and is composed of the following:
2575
    * @param array $panes This is the only parameter received and is composed of the following:
2576
    *                     0 => Vertical split position,
2576
    *                     0 => Vertical split position,
2577
    *                     1 => Horizontal split position
2577
    *                     1 => Horizontal split position
2578
    *                     2 => Top row visible
2578
    *                     2 => Top row visible
2579
    *                     3 => Leftmost column visible
2579
    *                     3 => Leftmost column visible
2580
    *                     4 => Active pane
2580
    *                     4 => Active pane
2581
    */
2581
    */
2582
    function _storePanes($panes)
2582
    function _storePanes($panes)
2583
    {
2583
    {
2584
        $y       = $panes[0];
2584
        $y       = $panes[0];
2585
        $x       = $panes[1];
2585
        $x       = $panes[1];
2586
        $rwTop   = $panes[2];
2586
        $rwTop   = $panes[2];
2587
        $colLeft = $panes[3];
2587
        $colLeft = $panes[3];
2588
        if (count($panes) > 4) { // if Active pane was received
2588
        if (count($panes) > 4) { // if Active pane was received
2589
            $pnnAct = $panes[4];
2589
            $pnnAct = $panes[4];
2590
        } else {
2590
        } else {
2591
            $pnnAct = null;
2591
            $pnnAct = null;
2592
        }
2592
        }
2593
        $record  = 0x0041;       // Record identifier
2593
        $record  = 0x0041;       // Record identifier
2594
        $length  = 0x000A;       // Number of bytes to follow
2594
        $length  = 0x000A;       // Number of bytes to follow
2595
 
2595
 
2596
        // Code specific to frozen or thawed panes.
2596
        // Code specific to frozen or thawed panes.
2597
        if ($this->_frozen) {
2597
        if ($this->_frozen) {
2598
            // Set default values for $rwTop and $colLeft
2598
            // Set default values for $rwTop and $colLeft
2599
            if (!isset($rwTop)) {
2599
            if (!isset($rwTop)) {
2600
                $rwTop   = $y;
2600
                $rwTop   = $y;
2601
            }
2601
            }
2602
            if (!isset($colLeft)) {
2602
            if (!isset($colLeft)) {
2603
                $colLeft = $x;
2603
                $colLeft = $x;
2604
            }
2604
            }
2605
        } else {
2605
        } else {
2606
            // Set default values for $rwTop and $colLeft
2606
            // Set default values for $rwTop and $colLeft
2607
            if (!isset($rwTop)) {
2607
            if (!isset($rwTop)) {
2608
                $rwTop   = 0;
2608
                $rwTop   = 0;
2609
            }
2609
            }
2610
            if (!isset($colLeft)) {
2610
            if (!isset($colLeft)) {
2611
                $colLeft = 0;
2611
                $colLeft = 0;
2612
            }
2612
            }
2613
 
2613
 
2614
            // Convert Excel's row and column units to the internal units.
2614
            // Convert Excel's row and column units to the internal units.
2615
            // The default row height is 12.75
2615
            // The default row height is 12.75
2616
            // The default column width is 8.43
2616
            // The default column width is 8.43
2617
            // The following slope and intersection values were interpolated.
2617
            // The following slope and intersection values were interpolated.
2618
            //
2618
            //
2619
            $y = 20*$y      + 255;
2619
            $y = 20*$y      + 255;
2620
            $x = 113.879*$x + 390;
2620
            $x = 113.879*$x + 390;
2621
        }
2621
        }
2622
 
2622
 
2623
 
2623
 
2624
        // Determine which pane should be active. There is also the undocumented
2624
        // Determine which pane should be active. There is also the undocumented
2625
        // option to override this should it be necessary: may be removed later.
2625
        // option to override this should it be necessary: may be removed later.
2626
        //
2626
        //
2627
        if (!isset($pnnAct)) {
2627
        if (!isset($pnnAct)) {
2628
            if ($x != 0 && $y != 0) {
2628
            if ($x != 0 && $y != 0) {
2629
                $pnnAct = 0; // Bottom right
2629
                $pnnAct = 0; // Bottom right
2630
            }
2630
            }
2631
            if ($x != 0 && $y == 0) {
2631
            if ($x != 0 && $y == 0) {
2632
                $pnnAct = 1; // Top right
2632
                $pnnAct = 1; // Top right
2633
            }
2633
            }
2634
            if ($x == 0 && $y != 0) {
2634
            if ($x == 0 && $y != 0) {
2635
                $pnnAct = 2; // Bottom left
2635
                $pnnAct = 2; // Bottom left
2636
            }
2636
            }
2637
            if ($x == 0 && $y == 0) {
2637
            if ($x == 0 && $y == 0) {
2638
                $pnnAct = 3; // Top left
2638
                $pnnAct = 3; // Top left
2639
            }
2639
            }
2640
        }
2640
        }
2641
 
2641
 
2642
        $this->_active_pane = $pnnAct; // Used in _storeSelection
2642
        $this->_active_pane = $pnnAct; // Used in _storeSelection
2643
 
2643
 
2644
        $header     = pack("vv",    $record, $length);
2644
        $header     = pack("vv",    $record, $length);
2645
        $data       = pack("vvvvv", $x, $y, $rwTop, $colLeft, $pnnAct);
2645
        $data       = pack("vvvvv", $x, $y, $rwTop, $colLeft, $pnnAct);
2646
		$data = $header . $data;
2646
		$data = $header . $data;
2647
        $this->_append($data);
2647
        $this->_append($data);
2648
    }
2648
    }
2649
 
2649
 
2650
    /**
2650
    /**
2651
    * Store the page setup SETUP BIFF record.
2651
    * Store the page setup SETUP BIFF record.
2652
    *
2652
    *
2653
    * @access private
2653
    * @access private
2654
    */
2654
    */
2655
    function _storeSetup()
2655
    function _storeSetup()
2656
    {
2656
    {
2657
        $record       = 0x00A1;                  // Record identifier
2657
        $record       = 0x00A1;                  // Record identifier
2658
        $length       = 0x0022;                  // Number of bytes to follow
2658
        $length       = 0x0022;                  // Number of bytes to follow
2659
 
2659
 
2660
        $iPaperSize   = $this->_paper_size;    // Paper size
2660
        $iPaperSize   = $this->_paper_size;    // Paper size
2661
        $iScale       = $this->_print_scale;   // Print scaling factor
2661
        $iScale       = $this->_print_scale;   // Print scaling factor
2662
        $iPageStart   = 0x01;                 // Starting page number
2662
        $iPageStart   = 0x01;                 // Starting page number
2663
        $iFitWidth    = $this->_fit_width;    // Fit to number of pages wide
2663
        $iFitWidth    = $this->_fit_width;    // Fit to number of pages wide
2664
        $iFitHeight   = $this->_fit_height;   // Fit to number of pages high
2664
        $iFitHeight   = $this->_fit_height;   // Fit to number of pages high
2665
        $grbit        = 0x00;                 // Option flags
2665
        $grbit        = 0x00;                 // Option flags
2666
        $iRes         = 0x0258;               // Print resolution
2666
        $iRes         = 0x0258;               // Print resolution
2667
        $iVRes        = 0x0258;               // Vertical print resolution
2667
        $iVRes        = 0x0258;               // Vertical print resolution
2668
        $numHdr       = $this->_margin_head;  // Header Margin
2668
        $numHdr       = $this->_margin_head;  // Header Margin
2669
        $numFtr       = $this->_margin_foot;   // Footer Margin
2669
        $numFtr       = $this->_margin_foot;   // Footer Margin
2670
        $iCopies      = 0x01;                 // Number of copies
2670
        $iCopies      = 0x01;                 // Number of copies
2671
 
2671
 
2672
        $fLeftToRight = 0x0;                     // Print over then down
2672
        $fLeftToRight = 0x0;                     // Print over then down
2673
        $fLandscape   = $this->_orientation;     // Page orientation
2673
        $fLandscape   = $this->_orientation;     // Page orientation
2674
        $fNoPls       = 0x0;                     // Setup not read from printer
2674
        $fNoPls       = 0x0;                     // Setup not read from printer
2675
        $fNoColor     = 0x0;                     // Print black and white
2675
        $fNoColor     = 0x0;                     // Print black and white
2676
        $fDraft       = 0x0;                     // Print draft quality
2676
        $fDraft       = 0x0;                     // Print draft quality
2677
        $fNotes       = 0x0;                     // Print notes
2677
        $fNotes       = 0x0;                     // Print notes
2678
        $fNoOrient    = 0x0;                     // Orientation not set
2678
        $fNoOrient    = 0x0;                     // Orientation not set
2679
        $fUsePage     = 0x0;                     // Use custom starting page
2679
        $fUsePage     = 0x0;                     // Use custom starting page
2680
 
2680
 
2681
        $grbit           = $fLeftToRight;
2681
        $grbit           = $fLeftToRight;
2682
        $grbit          |= $fLandscape    << 1;
2682
        $grbit          |= $fLandscape    << 1;
2683
        $grbit          |= $fNoPls        << 2;
2683
        $grbit          |= $fNoPls        << 2;
2684
        $grbit          |= $fNoColor      << 3;
2684
        $grbit          |= $fNoColor      << 3;
2685
        $grbit          |= $fDraft        << 4;
2685
        $grbit          |= $fDraft        << 4;
2686
        $grbit          |= $fNotes        << 5;
2686
        $grbit          |= $fNotes        << 5;
2687
        $grbit          |= $fNoOrient     << 6;
2687
        $grbit          |= $fNoOrient     << 6;
2688
        $grbit          |= $fUsePage      << 7;
2688
        $grbit          |= $fUsePage      << 7;
2689
 
2689
 
2690
        $numHdr = pack("d", $numHdr);
2690
        $numHdr = pack("d", $numHdr);
2691
        $numFtr = pack("d", $numFtr);
2691
        $numFtr = pack("d", $numFtr);
2692
        if ($this->_byte_order) { // if it's Big Endian
2692
        if ($this->_byte_order) { // if it's Big Endian
2693
            $numHdr = strrev($numHdr);
2693
            $numHdr = strrev($numHdr);
2694
            $numFtr = strrev($numFtr);
2694
            $numFtr = strrev($numFtr);
2695
        }
2695
        }
2696
 
2696
 
2697
        $header = pack("vv", $record, $length);
2697
        $header = pack("vv", $record, $length);
2698
        $data1  = pack("vvvvvvvv", $iPaperSize,
2698
        $data1  = pack("vvvvvvvv", $iPaperSize,
2699
                                   $iScale,
2699
                                   $iScale,
2700
                                   $iPageStart,
2700
                                   $iPageStart,
2701
                                   $iFitWidth,
2701
                                   $iFitWidth,
2702
                                   $iFitHeight,
2702
                                   $iFitHeight,
2703
                                   $grbit,
2703
                                   $grbit,
2704
                                   $iRes,
2704
                                   $iRes,
2705
                                   $iVRes);
2705
                                   $iVRes);
2706
        $data2  = $numHdr.$numFtr;
2706
        $data2  = $numHdr.$numFtr;
2707
        $data3  = pack("v", $iCopies);
2707
        $data3  = pack("v", $iCopies);
2708
        $this->_prepend($header . $data1 . $data2 . $data3);
2708
        $this->_prepend($header . $data1 . $data2 . $data3);
2709
    }
2709
    }
2710
 
2710
 
2711
    /**
2711
    /**
2712
    * Store the header caption BIFF record.
2712
    * Store the header caption BIFF record.
2713
    *
2713
    *
2714
    * @access private
2714
    * @access private
2715
    */
2715
    */
2716
    function _storeHeader()
2716
    function _storeHeader()
2717
    {
2717
    {
2718
        $record  = 0x0014;               // Record identifier
2718
        $record  = 0x0014;               // Record identifier
2719
 
2719
 
2720
        $str      = $this->_header;       // header string
2720
        $str      = $this->_header;       // header string
2721
        $cch      = strlen($str);         // Length of header string
2721
        $cch      = strlen($str);         // Length of header string
2722
        if ($this->_BIFF_version == 0x0600) {
2722
        if ($this->_BIFF_version == 0x0600) {
2723
            $encoding = 0x0;                  // TODO: Unicode support
2723
            $encoding = 0x0;                  // TODO: Unicode support
2724
            $length   = 3 + $cch;             // Bytes to follow
2724
            $length   = 3 + $cch;             // Bytes to follow
2725
        } else {
2725
        } else {
2726
            $length  = 1 + $cch;             // Bytes to follow
2726
            $length  = 1 + $cch;             // Bytes to follow
2727
        }
2727
        }
2728
 
2728
 
2729
        $header   = pack("vv", $record, $length);
2729
        $header   = pack("vv", $record, $length);
2730
        if ($this->_BIFF_version == 0x0600) {
2730
        if ($this->_BIFF_version == 0x0600) {
2731
            $data     = pack("vC",  $cch, $encoding);
2731
            $data     = pack("vC",  $cch, $encoding);
2732
        } else {
2732
        } else {
2733
            $data      = pack("C",  $cch);
2733
            $data      = pack("C",  $cch);
2734
        }
2734
        }
2735
 
2735
 
2736
        $this->_prepend($header.$data.$str);
2736
        $this->_prepend($header.$data.$str);
2737
    }
2737
    }
2738
 
2738
 
2739
    /**
2739
    /**
2740
    * Store the footer caption BIFF record.
2740
    * Store the footer caption BIFF record.
2741
    *
2741
    *
2742
    * @access private
2742
    * @access private
2743
    */
2743
    */
2744
    function _storeFooter()
2744
    function _storeFooter()
2745
    {
2745
    {
2746
        $record  = 0x0015;               // Record identifier
2746
        $record  = 0x0015;               // Record identifier
2747
 
2747
 
2748
        $str      = $this->_footer;       // Footer string
2748
        $str      = $this->_footer;       // Footer string
2749
        $cch      = strlen($str);         // Length of footer string
2749
        $cch      = strlen($str);         // Length of footer string
2750
        if ($this->_BIFF_version == 0x0600) {
2750
        if ($this->_BIFF_version == 0x0600) {
2751
            $encoding = 0x0;                  // TODO: Unicode support
2751
            $encoding = 0x0;                  // TODO: Unicode support
2752
            $length   = 3 + $cch;             // Bytes to follow
2752
            $length   = 3 + $cch;             // Bytes to follow
2753
        } else {
2753
        } else {
2754
            $length  = 1 + $cch;
2754
            $length  = 1 + $cch;
2755
        }
2755
        }
2756
 
2756
 
2757
        $header    = pack("vv", $record, $length);
2757
        $header    = pack("vv", $record, $length);
2758
        if ($this->_BIFF_version == 0x0600) {
2758
        if ($this->_BIFF_version == 0x0600) {
2759
            $data      = pack("vC",  $cch, $encoding);
2759
            $data      = pack("vC",  $cch, $encoding);
2760
        } else {
2760
        } else {
2761
            $data      = pack("C",  $cch);
2761
            $data      = pack("C",  $cch);
2762
        }
2762
        }
2763
 
2763
 
2764
        $this->_prepend($header . $data . $str);
2764
        $this->_prepend($header . $data . $str);
2765
    }
2765
    }
2766
 
2766
 
2767
    /**
2767
    /**
2768
    * Store the horizontal centering HCENTER BIFF record.
2768
    * Store the horizontal centering HCENTER BIFF record.
2769
    *
2769
    *
2770
    * @access private
2770
    * @access private
2771
    */
2771
    */
2772
    function _storeHcenter()
2772
    function _storeHcenter()
2773
    {
2773
    {
2774
        $record   = 0x0083;              // Record identifier
2774
        $record   = 0x0083;              // Record identifier
2775
        $length   = 0x0002;              // Bytes to follow
2775
        $length   = 0x0002;              // Bytes to follow
2776
 
2776
 
2777
        $fHCenter = $this->_hcenter;     // Horizontal centering
2777
        $fHCenter = $this->_hcenter;     // Horizontal centering
2778
 
2778
 
2779
        $header    = pack("vv", $record, $length);
2779
        $header    = pack("vv", $record, $length);
2780
        $data      = pack("v",  $fHCenter);
2780
        $data      = pack("v",  $fHCenter);
2781
 
2781
 
2782
        $this->_prepend($header.$data);
2782
        $this->_prepend($header.$data);
2783
    }
2783
    }
2784
 
2784
 
2785
    /**
2785
    /**
2786
    * Store the vertical centering VCENTER BIFF record.
2786
    * Store the vertical centering VCENTER BIFF record.
2787
    *
2787
    *
2788
    * @access private
2788
    * @access private
2789
    */
2789
    */
2790
    function _storeVcenter()
2790
    function _storeVcenter()
2791
    {
2791
    {
2792
        $record   = 0x0084;              // Record identifier
2792
        $record   = 0x0084;              // Record identifier
2793
        $length   = 0x0002;              // Bytes to follow
2793
        $length   = 0x0002;              // Bytes to follow
2794
 
2794
 
2795
        $fVCenter = $this->_vcenter;     // Horizontal centering
2795
        $fVCenter = $this->_vcenter;     // Horizontal centering
2796
 
2796
 
2797
        $header    = pack("vv", $record, $length);
2797
        $header    = pack("vv", $record, $length);
2798
        $data      = pack("v",  $fVCenter);
2798
        $data      = pack("v",  $fVCenter);
2799
        $this->_prepend($header . $data);
2799
        $this->_prepend($header . $data);
2800
    }
2800
    }
2801
 
2801
 
2802
    /**
2802
    /**
2803
    * Store the LEFTMARGIN BIFF record.
2803
    * Store the LEFTMARGIN BIFF record.
2804
    *
2804
    *
2805
    * @access private
2805
    * @access private
2806
    */
2806
    */
2807
    function _storeMarginLeft()
2807
    function _storeMarginLeft()
2808
    {
2808
    {
2809
        $record  = 0x0026;                   // Record identifier
2809
        $record  = 0x0026;                   // Record identifier
2810
        $length  = 0x0008;                   // Bytes to follow
2810
        $length  = 0x0008;                   // Bytes to follow
2811
 
2811
 
2812
        $margin  = $this->_margin_left;       // Margin in inches
2812
        $margin  = $this->_margin_left;       // Margin in inches
2813
 
2813
 
2814
        $header    = pack("vv",  $record, $length);
2814
        $header    = pack("vv",  $record, $length);
2815
        $data      = pack("d",   $margin);
2815
        $data      = pack("d",   $margin);
2816
        if ($this->_byte_order) { // if it's Big Endian
2816
        if ($this->_byte_order) { // if it's Big Endian
2817
            $data = strrev($data);
2817
            $data = strrev($data);
2818
        }
2818
        }
2819
 
2819
 
2820
        $this->_prepend($header . $data);
2820
        $this->_prepend($header . $data);
2821
    }
2821
    }
2822
 
2822
 
2823
    /**
2823
    /**
2824
    * Store the RIGHTMARGIN BIFF record.
2824
    * Store the RIGHTMARGIN BIFF record.
2825
    *
2825
    *
2826
    * @access private
2826
    * @access private
2827
    */
2827
    */
2828
    function _storeMarginRight()
2828
    function _storeMarginRight()
2829
    {
2829
    {
2830
        $record  = 0x0027;                   // Record identifier
2830
        $record  = 0x0027;                   // Record identifier
2831
        $length  = 0x0008;                   // Bytes to follow
2831
        $length  = 0x0008;                   // Bytes to follow
2832
 
2832
 
2833
        $margin  = $this->_margin_right;      // Margin in inches
2833
        $margin  = $this->_margin_right;      // Margin in inches
2834
 
2834
 
2835
        $header    = pack("vv",  $record, $length);
2835
        $header    = pack("vv",  $record, $length);
2836
        $data      = pack("d",   $margin);
2836
        $data      = pack("d",   $margin);
2837
        if ($this->_byte_order) { // if it's Big Endian
2837
        if ($this->_byte_order) { // if it's Big Endian
2838
            $data = strrev($data);
2838
            $data = strrev($data);
2839
        }
2839
        }
2840
 
2840
 
2841
        $this->_prepend($header . $data);
2841
        $this->_prepend($header . $data);
2842
    }
2842
    }
2843
 
2843
 
2844
    /**
2844
    /**
2845
    * Store the TOPMARGIN BIFF record.
2845
    * Store the TOPMARGIN BIFF record.
2846
    *
2846
    *
2847
    * @access private
2847
    * @access private
2848
    */
2848
    */
2849
    function _storeMarginTop()
2849
    function _storeMarginTop()
2850
    {
2850
    {
2851
        $record  = 0x0028;                   // Record identifier
2851
        $record  = 0x0028;                   // Record identifier
2852
        $length  = 0x0008;                   // Bytes to follow
2852
        $length  = 0x0008;                   // Bytes to follow
2853
 
2853
 
2854
        $margin  = $this->_margin_top;        // Margin in inches
2854
        $margin  = $this->_margin_top;        // Margin in inches
2855
 
2855
 
2856
        $header    = pack("vv",  $record, $length);
2856
        $header    = pack("vv",  $record, $length);
2857
        $data      = pack("d",   $margin);
2857
        $data      = pack("d",   $margin);
2858
        if ($this->_byte_order) { // if it's Big Endian
2858
        if ($this->_byte_order) { // if it's Big Endian
2859
            $data = strrev($data);
2859
            $data = strrev($data);
2860
        }
2860
        }
2861
 
2861
 
2862
        $this->_prepend($header . $data);
2862
        $this->_prepend($header . $data);
2863
    }
2863
    }
2864
 
2864
 
2865
    /**
2865
    /**
2866
    * Store the BOTTOMMARGIN BIFF record.
2866
    * Store the BOTTOMMARGIN BIFF record.
2867
    *
2867
    *
2868
    * @access private
2868
    * @access private
2869
    */
2869
    */
2870
    function _storeMarginBottom()
2870
    function _storeMarginBottom()
2871
    {
2871
    {
2872
        $record  = 0x0029;                   // Record identifier
2872
        $record  = 0x0029;                   // Record identifier
2873
        $length  = 0x0008;                   // Bytes to follow
2873
        $length  = 0x0008;                   // Bytes to follow
2874
 
2874
 
2875
        $margin  = $this->_margin_bottom;     // Margin in inches
2875
        $margin  = $this->_margin_bottom;     // Margin in inches
2876
 
2876
 
2877
        $header    = pack("vv",  $record, $length);
2877
        $header    = pack("vv",  $record, $length);
2878
        $data      = pack("d",   $margin);
2878
        $data      = pack("d",   $margin);
2879
        if ($this->_byte_order) { // if it's Big Endian
2879
        if ($this->_byte_order) { // if it's Big Endian
2880
            $data = strrev($data);
2880
            $data = strrev($data);
2881
        }
2881
        }
2882
 
2882
 
2883
        $this->_prepend($header . $data);
2883
        $this->_prepend($header . $data);
2884
    }
2884
    }
2885
 
2885
 
2886
    /**
2886
    /**
2887
    * Merges the area given by its arguments.
2887
    * Merges the area given by its arguments.
2888
    * This is an Excel97/2000 method. It is required to perform more complicated
2888
    * This is an Excel97/2000 method. It is required to perform more complicated
2889
    * merging than the normal setAlign('merge').
2889
    * merging than the normal setAlign('merge').
2890
    *
2890
    *
2891
    * @access public
2891
    * @access public
2892
    * @param integer $first_row First row of the area to merge
2892
    * @param integer $first_row First row of the area to merge
2893
    * @param integer $first_col First column of the area to merge
2893
    * @param integer $first_col First column of the area to merge
2894
    * @param integer $last_row  Last row of the area to merge
2894
    * @param integer $last_row  Last row of the area to merge
2895
    * @param integer $last_col  Last column of the area to merge
2895
    * @param integer $last_col  Last column of the area to merge
2896
    */
2896
    */
2897
    function mergeCells($first_row, $first_col, $last_row, $last_col)
2897
    function mergeCells($first_row, $first_col, $last_row, $last_col)
2898
    {
2898
    {
2899
        $record  = 0x00E5;                   // Record identifier
2899
        $record  = 0x00E5;                   // Record identifier
2900
        $length  = 0x000A;                   // Bytes to follow
2900
        $length  = 0x000A;                   // Bytes to follow
2901
        $cref     = 1;                       // Number of refs
2901
        $cref     = 1;                       // Number of refs
2902
 
2902
 
2903
        // Swap last row/col for first row/col as necessary
2903
        // Swap last row/col for first row/col as necessary
2904
        if ($first_row > $last_row) {
2904
        if ($first_row > $last_row) {
2905
            list($first_row, $last_row) = array($last_row, $first_row);
2905
            list($first_row, $last_row) = array($last_row, $first_row);
2906
        }
2906
        }
2907
 
2907
 
2908
        if ($first_col > $last_col) {
2908
        if ($first_col > $last_col) {
2909
            list($first_col, $last_col) = array($last_col, $first_col);
2909
            list($first_col, $last_col) = array($last_col, $first_col);
2910
        }
2910
        }
2911
 
2911
 
2912
        $header   = pack("vv",    $record, $length);
2912
        $header   = pack("vv",    $record, $length);
2913
        $data     = pack("vvvvv", $cref, $first_row, $last_row,
2913
        $data     = pack("vvvvv", $cref, $first_row, $last_row,
2914
                                  $first_col, $last_col);
2914
                                  $first_col, $last_col);
2915
		$data = $header . $data;
2915
		$data = $header . $data;
2916
		$this->_append($data);
2916
		$this->_append($data);
2917
    }
2917
    }
2918
 
2918
 
2919
    /**
2919
    /**
2920
    * Write the PRINTHEADERS BIFF record.
2920
    * Write the PRINTHEADERS BIFF record.
2921
    *
2921
    *
2922
    * @access private
2922
    * @access private
2923
    */
2923
    */
2924
    function _storePrintHeaders()
2924
    function _storePrintHeaders()
2925
    {
2925
    {
2926
        $record      = 0x002a;                   // Record identifier
2926
        $record      = 0x002a;                   // Record identifier
2927
        $length      = 0x0002;                   // Bytes to follow
2927
        $length      = 0x0002;                   // Bytes to follow
2928
 
2928
 
2929
        $fPrintRwCol = $this->_print_headers;     // Boolean flag
2929
        $fPrintRwCol = $this->_print_headers;     // Boolean flag
2930
 
2930
 
2931
        $header      = pack("vv", $record, $length);
2931
        $header      = pack("vv", $record, $length);
2932
        $data        = pack("v", $fPrintRwCol);
2932
        $data        = pack("v", $fPrintRwCol);
2933
        $this->_prepend($header . $data);
2933
        $this->_prepend($header . $data);
2934
    }
2934
    }
2935
 
2935
 
2936
    /**
2936
    /**
2937
    * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
2937
    * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
2938
    * GRIDSET record.
2938
    * GRIDSET record.
2939
    *
2939
    *
2940
    * @access private
2940
    * @access private
2941
    */
2941
    */
2942
    function _storePrintGridlines()
2942
    function _storePrintGridlines()
2943
    {
2943
    {
2944
        $record      = 0x002b;                    // Record identifier
2944
        $record      = 0x002b;                    // Record identifier
2945
        $length      = 0x0002;                    // Bytes to follow
2945
        $length      = 0x0002;                    // Bytes to follow
2946
 
2946
 
2947
        $fPrintGrid  = $this->_print_gridlines;    // Boolean flag
2947
        $fPrintGrid  = $this->_print_gridlines;    // Boolean flag
2948
 
2948
 
2949
        $header      = pack("vv", $record, $length);
2949
        $header      = pack("vv", $record, $length);
2950
        $data        = pack("v", $fPrintGrid);
2950
        $data        = pack("v", $fPrintGrid);
2951
        $this->_prepend($header . $data);
2951
        $this->_prepend($header . $data);
2952
    }
2952
    }
2953
 
2953
 
2954
    /**
2954
    /**
2955
    * Write the GRIDSET BIFF record. Must be used in conjunction with the
2955
    * Write the GRIDSET BIFF record. Must be used in conjunction with the
2956
    * PRINTGRIDLINES record.
2956
    * PRINTGRIDLINES record.
2957
    *
2957
    *
2958
    * @access private
2958
    * @access private
2959
    */
2959
    */
2960
    function _storeGridset()
2960
    function _storeGridset()
2961
    {
2961
    {
2962
        $record      = 0x0082;                        // Record identifier
2962
        $record      = 0x0082;                        // Record identifier
2963
        $length      = 0x0002;                        // Bytes to follow
2963
        $length      = 0x0002;                        // Bytes to follow
2964
 
2964
 
2965
        $fGridSet    = !($this->_print_gridlines);     // Boolean flag
2965
        $fGridSet    = !($this->_print_gridlines);     // Boolean flag
2966
 
2966
 
2967
        $header      = pack("vv",  $record, $length);
2967
        $header      = pack("vv",  $record, $length);
2968
        $data        = pack("v",   $fGridSet);
2968
        $data        = pack("v",   $fGridSet);
2969
        $this->_prepend($header . $data);
2969
        $this->_prepend($header . $data);
2970
    }
2970
    }
2971
 
2971
 
2972
    /**
2972
    /**
2973
    * Write the GUTS BIFF record. This is used to configure the gutter margins
2973
    * Write the GUTS BIFF record. This is used to configure the gutter margins
2974
    * where Excel outline symbols are displayed. The visibility of the gutters is
2974
    * where Excel outline symbols are displayed. The visibility of the gutters is
2975
    * controlled by a flag in WSBOOL.
2975
    * controlled by a flag in WSBOOL.
2976
    *
2976
    *
2977
    * @see _storeWsbool()
2977
    * @see _storeWsbool()
2978
    * @access private
2978
    * @access private
2979
    */
2979
    */
2980
    function _storeGuts()
2980
    function _storeGuts()
2981
    {
2981
    {
2982
        $record      = 0x0080;   // Record identifier
2982
        $record      = 0x0080;   // Record identifier
2983
        $length      = 0x0008;   // Bytes to follow
2983
        $length      = 0x0008;   // Bytes to follow
2984
 
2984
 
2985
        $dxRwGut     = 0x0000;   // Size of row gutter
2985
        $dxRwGut     = 0x0000;   // Size of row gutter
2986
        $dxColGut    = 0x0000;   // Size of col gutter
2986
        $dxColGut    = 0x0000;   // Size of col gutter
2987
 
2987
 
2988
        $row_level   = $this->_outline_row_level;
2988
        $row_level   = $this->_outline_row_level;
2989
        $col_level   = 0;
2989
        $col_level   = 0;
2990
 
2990
 
2991
        // Calculate the maximum column outline level. The equivalent calculation
2991
        // Calculate the maximum column outline level. The equivalent calculation
2992
        // for the row outline level is carried out in setRow().
2992
        // for the row outline level is carried out in setRow().
2993
        $colcount = count($this->_colinfo);
2993
        $colcount = count($this->_colinfo);
2994
        for ($i = 0; $i < $colcount; $i++) {
2994
        for ($i = 0; $i < $colcount; $i++) {
2995
           // Skip cols without outline level info.
2995
           // Skip cols without outline level info.
2996
           if (count($this->_colinfo[$i]) >= 6) {
2996
           if (count($this->_colinfo[$i]) >= 6) {
2997
              $col_level = max($this->_colinfo[$i][5], $col_level);
2997
              $col_level = max($this->_colinfo[$i][5], $col_level);
2998
           }
2998
           }
2999
        }
2999
        }
3000
 
3000
 
3001
        // Set the limits for the outline levels (0 <= x <= 7).
3001
        // Set the limits for the outline levels (0 <= x <= 7).
3002
        $col_level = max(0, min($col_level, 7));
3002
        $col_level = max(0, min($col_level, 7));
3003
 
3003
 
3004
        // The displayed level is one greater than the max outline levels
3004
        // The displayed level is one greater than the max outline levels
3005
        if ($row_level) {
3005
        if ($row_level) {
3006
            $row_level++;
3006
            $row_level++;
3007
        }
3007
        }
3008
        if ($col_level) {
3008
        if ($col_level) {
3009
            $col_level++;
3009
            $col_level++;
3010
        }
3010
        }
3011
 
3011
 
3012
        $header = pack("vv",   $record, $length);
3012
        $header = pack("vv",   $record, $length);
3013
        $data   = pack("vvvv", $dxRwGut, $dxColGut, $row_level, $col_level);
3013
        $data   = pack("vvvv", $dxRwGut, $dxColGut, $row_level, $col_level);
3014
 
3014
 
3015
        $this->_prepend($header.$data);
3015
        $this->_prepend($header.$data);
3016
    }
3016
    }
3017
 
3017
 
3018
 
3018
 
3019
    /**
3019
    /**
3020
    * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
3020
    * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
3021
    * with the SETUP record.
3021
    * with the SETUP record.
3022
    *
3022
    *
3023
    * @access private
3023
    * @access private
3024
    */
3024
    */
3025
    function _storeWsbool()
3025
    function _storeWsbool()
3026
    {
3026
    {
3027
        $record      = 0x0081;   // Record identifier
3027
        $record      = 0x0081;   // Record identifier
3028
        $length      = 0x0002;   // Bytes to follow
3028
        $length      = 0x0002;   // Bytes to follow
3029
        $grbit       = 0x0000;
3029
        $grbit       = 0x0000;
3030
 
3030
 
3031
        // The only option that is of interest is the flag for fit to page. So we
3031
        // The only option that is of interest is the flag for fit to page. So we
3032
        // set all the options in one go.
3032
        // set all the options in one go.
3033
        //
3033
        //
3034
        /*if ($this->_fit_page) {
3034
        /*if ($this->_fit_page) {
3035
            $grbit = 0x05c1;
3035
            $grbit = 0x05c1;
3036
        } else {
3036
        } else {
3037
            $grbit = 0x04c1;
3037
            $grbit = 0x04c1;
3038
        }*/
3038
        }*/
3039
        // Set the option flags
3039
        // Set the option flags
3040
        $grbit |= 0x0001;                           // Auto page breaks visible
3040
        $grbit |= 0x0001;                           // Auto page breaks visible
3041
        if ($this->_outline_style) {
3041
        if ($this->_outline_style) {
3042
            $grbit |= 0x0020; // Auto outline styles
3042
            $grbit |= 0x0020; // Auto outline styles
3043
        }
3043
        }
3044
        if ($this->_outline_below) {
3044
        if ($this->_outline_below) {
3045
            $grbit |= 0x0040; // Outline summary below
3045
            $grbit |= 0x0040; // Outline summary below
3046
        }
3046
        }
3047
        if ($this->_outline_right) {
3047
        if ($this->_outline_right) {
3048
            $grbit |= 0x0080; // Outline summary right
3048
            $grbit |= 0x0080; // Outline summary right
3049
        }
3049
        }
3050
        if ($this->_fit_page) {
3050
        if ($this->_fit_page) {
3051
            $grbit |= 0x0100; // Page setup fit to page
3051
            $grbit |= 0x0100; // Page setup fit to page
3052
        }
3052
        }
3053
        if ($this->_outline_on) {
3053
        if ($this->_outline_on) {
3054
            $grbit |= 0x0400; // Outline symbols displayed
3054
            $grbit |= 0x0400; // Outline symbols displayed
3055
        }
3055
        }
3056
 
3056
 
3057
        $header = pack("vv", $record, $length);
3057
        $header = pack("vv", $record, $length);
3058
        $data   = pack("v",  $grbit);
3058
        $data   = pack("v",  $grbit);
3059
        $this->_prepend($header . $data);
3059
        $this->_prepend($header . $data);
3060
    }
3060
    }
3061
 
3061
 
3062
    /**
3062
    /**
3063
    * Write the HORIZONTALPAGEBREAKS BIFF record.
3063
    * Write the HORIZONTALPAGEBREAKS BIFF record.
3064
    *
3064
    *
3065
    * @access private
3065
    * @access private
3066
    */
3066
    */
3067
    function _storeHbreak()
3067
    function _storeHbreak()
3068
    {
3068
    {
3069
        // Return if the user hasn't specified pagebreaks
3069
        // Return if the user hasn't specified pagebreaks
3070
        if (empty($this->_hbreaks)) {
3070
        if (empty($this->_hbreaks)) {
3071
            return;
3071
            return;
3072
        }
3072
        }
3073
 
3073
 
3074
        // Sort and filter array of page breaks
3074
        // Sort and filter array of page breaks
3075
        $breaks = $this->_hbreaks;
3075
        $breaks = $this->_hbreaks;
3076
        sort($breaks, SORT_NUMERIC);
3076
        sort($breaks, SORT_NUMERIC);
3077
        if ($breaks[0] == 0) { // don't use first break if it's 0
3077
        if ($breaks[0] == 0) { // don't use first break if it's 0
3078
            array_shift($breaks);
3078
            array_shift($breaks);
3079
        }
3079
        }
3080
 
3080
 
3081
        $record  = 0x001b;               // Record identifier
3081
        $record  = 0x001b;               // Record identifier
3082
        $cbrk    = count($breaks);       // Number of page breaks
3082
        $cbrk    = count($breaks);       // Number of page breaks
3083
        if ($this->_BIFF_version == 0x0600) {
3083
        if ($this->_BIFF_version == 0x0600) {
3084
            $length  = 2 + 6*$cbrk;      // Bytes to follow
3084
            $length  = 2 + 6*$cbrk;      // Bytes to follow
3085
        } else {
3085
        } else {
3086
            $length  = 2 + 2*$cbrk;      // Bytes to follow
3086
            $length  = 2 + 2*$cbrk;      // Bytes to follow
3087
        }
3087
        }
3088
 
3088
 
3089
        $header  = pack("vv", $record, $length);
3089
        $header  = pack("vv", $record, $length);
3090
        $data    = pack("v",  $cbrk);
3090
        $data    = pack("v",  $cbrk);
3091
 
3091
 
3092
        // Append each page break
3092
        // Append each page break
3093
        foreach ($breaks as $break) {
3093
        foreach ($breaks as $break) {
3094
            if ($this->_BIFF_version == 0x0600) {
3094
            if ($this->_BIFF_version == 0x0600) {
3095
                $data .= pack("vvv", $break, 0x0000, 0x00ff);
3095
                $data .= pack("vvv", $break, 0x0000, 0x00ff);
3096
            } else {
3096
            } else {
3097
                $data .= pack("v", $break);
3097
                $data .= pack("v", $break);
3098
            }
3098
            }
3099
        }
3099
        }
3100
 
3100
 
3101
        $this->_prepend($header.$data);
3101
        $this->_prepend($header.$data);
3102
    }
3102
    }
3103
 
3103
 
3104
 
3104
 
3105
    /**
3105
    /**
3106
    * Write the VERTICALPAGEBREAKS BIFF record.
3106
    * Write the VERTICALPAGEBREAKS BIFF record.
3107
    *
3107
    *
3108
    * @access private
3108
    * @access private
3109
    */
3109
    */
3110
    function _storeVbreak()
3110
    function _storeVbreak()
3111
    {
3111
    {
3112
        // Return if the user hasn't specified pagebreaks
3112
        // Return if the user hasn't specified pagebreaks
3113
        if (empty($this->_vbreaks)) {
3113
        if (empty($this->_vbreaks)) {
3114
            return;
3114
            return;
3115
        }
3115
        }
3116
 
3116
 
3117
        // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
3117
        // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
3118
        // It is slightly higher in Excel 97/200, approx. 1026
3118
        // It is slightly higher in Excel 97/200, approx. 1026
3119
        $breaks = array_slice($this->_vbreaks,0,1000);
3119
        $breaks = array_slice($this->_vbreaks,0,1000);
3120
 
3120
 
3121
        // Sort and filter array of page breaks
3121
        // Sort and filter array of page breaks
3122
        sort($breaks, SORT_NUMERIC);
3122
        sort($breaks, SORT_NUMERIC);
3123
        if ($breaks[0] == 0) { // don't use first break if it's 0
3123
        if ($breaks[0] == 0) { // don't use first break if it's 0
3124
            array_shift($breaks);
3124
            array_shift($breaks);
3125
        }
3125
        }
3126
 
3126
 
3127
        $record  = 0x001a;               // Record identifier
3127
        $record  = 0x001a;               // Record identifier
3128
        $cbrk    = count($breaks);       // Number of page breaks
3128
        $cbrk    = count($breaks);       // Number of page breaks
3129
        if ($this->_BIFF_version == 0x0600) {
3129
        if ($this->_BIFF_version == 0x0600) {
3130
            $length  = 2 + 6*$cbrk;      // Bytes to follow
3130
            $length  = 2 + 6*$cbrk;      // Bytes to follow
3131
        } else {
3131
        } else {
3132
            $length  = 2 + 2*$cbrk;      // Bytes to follow
3132
            $length  = 2 + 2*$cbrk;      // Bytes to follow
3133
        }
3133
        }
3134
 
3134
 
3135
        $header  = pack("vv",  $record, $length);
3135
        $header  = pack("vv",  $record, $length);
3136
        $data    = pack("v",   $cbrk);
3136
        $data    = pack("v",   $cbrk);
3137
 
3137
 
3138
        // Append each page break
3138
        // Append each page break
3139
        foreach ($breaks as $break) {
3139
        foreach ($breaks as $break) {
3140
            if ($this->_BIFF_version == 0x0600) {
3140
            if ($this->_BIFF_version == 0x0600) {
3141
                $data .= pack("vvv", $break, 0x0000, 0xffff);
3141
                $data .= pack("vvv", $break, 0x0000, 0xffff);
3142
            } else {
3142
            } else {
3143
                $data .= pack("v", $break);
3143
                $data .= pack("v", $break);
3144
            }
3144
            }
3145
        }
3145
        }
3146
 
3146
 
3147
        $this->_prepend($header . $data);
3147
        $this->_prepend($header . $data);
3148
    }
3148
    }
3149
 
3149
 
3150
    /**
3150
    /**
3151
    * Set the Biff PROTECT record to indicate that the worksheet is protected.
3151
    * Set the Biff PROTECT record to indicate that the worksheet is protected.
3152
    *
3152
    *
3153
    * @access private
3153
    * @access private
3154
    */
3154
    */
3155
    function _storeProtect()
3155
    function _storeProtect()
3156
    {
3156
    {
3157
        // Exit unless sheet protection has been specified
3157
        // Exit unless sheet protection has been specified
3158
        if ($this->_protect == 0) {
3158
        if ($this->_protect == 0) {
3159
            return;
3159
            return;
3160
        }
3160
        }
3161
 
3161
 
3162
        $record      = 0x0012;             // Record identifier
3162
        $record      = 0x0012;             // Record identifier
3163
        $length      = 0x0002;             // Bytes to follow
3163
        $length      = 0x0002;             // Bytes to follow
3164
 
3164
 
3165
        $fLock       = $this->_protect;    // Worksheet is protected
3165
        $fLock       = $this->_protect;    // Worksheet is protected
3166
 
3166
 
3167
        $header      = pack("vv", $record, $length);
3167
        $header      = pack("vv", $record, $length);
3168
        $data        = pack("v",  $fLock);
3168
        $data        = pack("v",  $fLock);
3169
 
3169
 
3170
        $this->_prepend($header.$data);
3170
        $this->_prepend($header.$data);
3171
    }
3171
    }
3172
 
3172
 
3173
    /**
3173
    /**
3174
    * Write the worksheet PASSWORD record.
3174
    * Write the worksheet PASSWORD record.
3175
    *
3175
    *
3176
    * @access private
3176
    * @access private
3177
    */
3177
    */
3178
    function _storePassword()
3178
    function _storePassword()
3179
    {
3179
    {
3180
        // Exit unless sheet protection and password have been specified
3180
        // Exit unless sheet protection and password have been specified
3181
        if (($this->_protect == 0) || (!isset($this->_password))) {
3181
        if (($this->_protect == 0) || (!isset($this->_password))) {
3182
            return;
3182
            return;
3183
        }
3183
        }
3184
 
3184
 
3185
        $record      = 0x0013;               // Record identifier
3185
        $record      = 0x0013;               // Record identifier
3186
        $length      = 0x0002;               // Bytes to follow
3186
        $length      = 0x0002;               // Bytes to follow
3187
 
3187
 
3188
        $wPassword   = $this->_password;     // Encoded password
3188
        $wPassword   = $this->_password;     // Encoded password
3189
 
3189
 
3190
        $header      = pack("vv", $record, $length);
3190
        $header      = pack("vv", $record, $length);
3191
        $data        = pack("v",  $wPassword);
3191
        $data        = pack("v",  $wPassword);
3192
 
3192
 
3193
        $this->_prepend($header . $data);
3193
        $this->_prepend($header . $data);
3194
    }
3194
    }
3195
 
3195
 
3196
 
3196
 
3197
    /**
3197
    /**
3198
    * Insert a 24bit bitmap image in a worksheet.
3198
    * Insert a 24bit bitmap image in a worksheet.
3199
    *
3199
    *
3200
    * @access public
3200
    * @access public
3201
    * @param integer $row     The row we are going to insert the bitmap into
3201
    * @param integer $row     The row we are going to insert the bitmap into
3202
    * @param integer $col     The column we are going to insert the bitmap into
3202
    * @param integer $col     The column we are going to insert the bitmap into
3203
    * @param string  $bitmap  The bitmap filename
3203
    * @param string  $bitmap  The bitmap filename
3204
    * @param integer $x       The horizontal position (offset) of the image inside the cell.
3204
    * @param integer $x       The horizontal position (offset) of the image inside the cell.
3205
    * @param integer $y       The vertical position (offset) of the image inside the cell.
3205
    * @param integer $y       The vertical position (offset) of the image inside the cell.
3206
    * @param integer $scale_x The horizontal scale
3206
    * @param integer $scale_x The horizontal scale
3207
    * @param integer $scale_y The vertical scale
3207
    * @param integer $scale_y The vertical scale
3208
    */
3208
    */
3209
    function insertBitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1)
3209
    function insertBitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1)
3210
    {
3210
    {
3211
        $bitmap_array = $this->_processBitmap($bitmap);
3211
        $bitmap_array = $this->_processBitmap($bitmap);
3212
        if ($this->isError($bitmap_array)) {
3212
        if ($this->isError($bitmap_array)) {
3213
            $this->writeString($row, $col, $bitmap_array->getMessage());
3213
            $this->writeString($row, $col, $bitmap_array->getMessage());
3214
            return;
3214
            return;
3215
        }
3215
        }
3216
        list($width, $height, $size, $data) = $bitmap_array; //$this->_processBitmap($bitmap);
3216
        list($width, $height, $size, $data) = $bitmap_array; //$this->_processBitmap($bitmap);
3217
 
3217
 
3218
        // Scale the frame of the image.
3218
        // Scale the frame of the image.
3219
        $width  *= $scale_x;
3219
        $width  *= $scale_x;
3220
        $height *= $scale_y;
3220
        $height *= $scale_y;
3221
 
3221
 
3222
        // Calculate the vertices of the image and write the OBJ record
3222
        // Calculate the vertices of the image and write the OBJ record
3223
        $this->_positionImage($col, $row, $x, $y, $width, $height);
3223
        $this->_positionImage($col, $row, $x, $y, $width, $height);
3224
 
3224
 
3225
        // Write the IMDATA record to store the bitmap data
3225
        // Write the IMDATA record to store the bitmap data
3226
        $record      = 0x007f;
3226
        $record      = 0x007f;
3227
        $length      = 8 + $size;
3227
        $length      = 8 + $size;
3228
        $cf          = 0x09;
3228
        $cf          = 0x09;
3229
        $env         = 0x01;
3229
        $env         = 0x01;
3230
        $lcb         = $size;
3230
        $lcb         = $size;
3231
 
3231
 
3232
        $header      = pack("vvvvV", $record, $length, $cf, $env, $lcb);
3232
        $header      = pack("vvvvV", $record, $length, $cf, $env, $lcb);
3233
		$data = $header . $data;
3233
		$data = $header . $data;
3234
		$this->_append($data);
3234
		$this->_append($data);
3235
    }
3235
    }
3236
 
3236
 
3237
    /**
3237
    /**
3238
    * Calculate the vertices that define the position of the image as required by
3238
    * Calculate the vertices that define the position of the image as required by
3239
    * the OBJ record.
3239
    * the OBJ record.
3240
    *
3240
    *
3241
    *         +------------+------------+
3241
    *         +------------+------------+
3242
    *         |     A      |      B     |
3242
    *         |     A      |      B     |
3243
    *   +-----+------------+------------+
3243
    *   +-----+------------+------------+
3244
    *   |     |(x1,y1)     |            |
3244
    *   |     |(x1,y1)     |            |
3245
    *   |  1  |(A1)._______|______      |
3245
    *   |  1  |(A1)._______|______      |
3246
    *   |     |    |              |     |
3246
    *   |     |    |              |     |
3247
    *   |     |    |              |     |
3247
    *   |     |    |              |     |
3248
    *   +-----+----|    BITMAP    |-----+
3248
    *   +-----+----|    BITMAP    |-----+
3249
    *   |     |    |              |     |
3249
    *   |     |    |              |     |
3250
    *   |  2  |    |______________.     |
3250
    *   |  2  |    |______________.     |
3251
    *   |     |            |        (B2)|
3251
    *   |     |            |        (B2)|
3252
    *   |     |            |     (x2,y2)|
3252
    *   |     |            |     (x2,y2)|
3253
    *   +---- +------------+------------+
3253
    *   +---- +------------+------------+
3254
    *
3254
    *
3255
    * Example of a bitmap that covers some of the area from cell A1 to cell B2.
3255
    * Example of a bitmap that covers some of the area from cell A1 to cell B2.
3256
    *
3256
    *
3257
    * Based on the width and height of the bitmap we need to calculate 8 vars:
3257
    * Based on the width and height of the bitmap we need to calculate 8 vars:
3258
    *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
3258
    *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
3259
    * The width and height of the cells are also variable and have to be taken into
3259
    * The width and height of the cells are also variable and have to be taken into
3260
    * account.
3260
    * account.
3261
    * The values of $col_start and $row_start are passed in from the calling
3261
    * The values of $col_start and $row_start are passed in from the calling
3262
    * function. The values of $col_end and $row_end are calculated by subtracting
3262
    * function. The values of $col_end and $row_end are calculated by subtracting
3263
    * the width and height of the bitmap from the width and height of the
3263
    * the width and height of the bitmap from the width and height of the
3264
    * underlying cells.
3264
    * underlying cells.
3265
    * The vertices are expressed as a percentage of the underlying cell width as
3265
    * The vertices are expressed as a percentage of the underlying cell width as
3266
    * follows (rhs values are in pixels):
3266
    * follows (rhs values are in pixels):
3267
    *
3267
    *
3268
    *       x1 = X / W *1024
3268
    *       x1 = X / W *1024
3269
    *       y1 = Y / H *256
3269
    *       y1 = Y / H *256
3270
    *       x2 = (X-1) / W *1024
3270
    *       x2 = (X-1) / W *1024
3271
    *       y2 = (Y-1) / H *256
3271
    *       y2 = (Y-1) / H *256
3272
    *
3272
    *
3273
    *       Where:  X is distance from the left side of the underlying cell
3273
    *       Where:  X is distance from the left side of the underlying cell
3274
    *               Y is distance from the top of the underlying cell
3274
    *               Y is distance from the top of the underlying cell
3275
    *               W is the width of the cell
3275
    *               W is the width of the cell
3276
    *               H is the height of the cell
3276
    *               H is the height of the cell
3277
    *
3277
    *
3278
    * @access private
3278
    * @access private
3279
    * @note  the SDK incorrectly states that the height should be expressed as a
3279
    * @note  the SDK incorrectly states that the height should be expressed as a
3280
    *        percentage of 1024.
3280
    *        percentage of 1024.
3281
    * @param integer $col_start Col containing upper left corner of object
3281
    * @param integer $col_start Col containing upper left corner of object
3282
    * @param integer $row_start Row containing top left corner of object
3282
    * @param integer $row_start Row containing top left corner of object
3283
    * @param integer $x1        Distance to left side of object
3283
    * @param integer $x1        Distance to left side of object
3284
    * @param integer $y1        Distance to top of object
3284
    * @param integer $y1        Distance to top of object
3285
    * @param integer $width     Width of image frame
3285
    * @param integer $width     Width of image frame
3286
    * @param integer $height    Height of image frame
3286
    * @param integer $height    Height of image frame
3287
    */
3287
    */
3288
    function _positionImage($col_start, $row_start, $x1, $y1, $width, $height)
3288
    function _positionImage($col_start, $row_start, $x1, $y1, $width, $height)
3289
    {
3289
    {
3290
        // Initialise end cell to the same as the start cell
3290
        // Initialise end cell to the same as the start cell
3291
        $col_end    = $col_start;  // Col containing lower right corner of object
3291
        $col_end    = $col_start;  // Col containing lower right corner of object
3292
        $row_end    = $row_start;  // Row containing bottom right corner of object
3292
        $row_end    = $row_start;  // Row containing bottom right corner of object
3293
 
3293
 
3294
        // Zero the specified offset if greater than the cell dimensions
3294
        // Zero the specified offset if greater than the cell dimensions
3295
        if ($x1 >= $this->_sizeCol($col_start)) {
3295
        if ($x1 >= $this->_sizeCol($col_start)) {
3296
            $x1 = 0;
3296
            $x1 = 0;
3297
        }
3297
        }
3298
        if ($y1 >= $this->_sizeRow($row_start)) {
3298
        if ($y1 >= $this->_sizeRow($row_start)) {
3299
            $y1 = 0;
3299
            $y1 = 0;
3300
        }
3300
        }
3301
 
3301
 
3302
        $width      = $width  + $x1 -1;
3302
        $width      = $width  + $x1 -1;
3303
        $height     = $height + $y1 -1;
3303
        $height     = $height + $y1 -1;
3304
 
3304
 
3305
        // Subtract the underlying cell widths to find the end cell of the image
3305
        // Subtract the underlying cell widths to find the end cell of the image
3306
        while ($width >= $this->_sizeCol($col_end)) {
3306
        while ($width >= $this->_sizeCol($col_end)) {
3307
            $width -= $this->_sizeCol($col_end);
3307
            $width -= $this->_sizeCol($col_end);
3308
            $col_end++;
3308
            $col_end++;
3309
        }
3309
        }
3310
 
3310
 
3311
        // Subtract the underlying cell heights to find the end cell of the image
3311
        // Subtract the underlying cell heights to find the end cell of the image
3312
        while ($height >= $this->_sizeRow($row_end)) {
3312
        while ($height >= $this->_sizeRow($row_end)) {
3313
            $height -= $this->_sizeRow($row_end);
3313
            $height -= $this->_sizeRow($row_end);
3314
            $row_end++;
3314
            $row_end++;
3315
        }
3315
        }
3316
 
3316
 
3317
        // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
3317
        // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
3318
        // with zero eight or width.
3318
        // with zero eight or width.
3319
        //
3319
        //
3320
        if ($this->_sizeCol($col_start) == 0) {
3320
        if ($this->_sizeCol($col_start) == 0) {
3321
            return;
3321
            return;
3322
        }
3322
        }
3323
        if ($this->_sizeCol($col_end)   == 0) {
3323
        if ($this->_sizeCol($col_end)   == 0) {
3324
            return;
3324
            return;
3325
        }
3325
        }
3326
        if ($this->_sizeRow($row_start) == 0) {
3326
        if ($this->_sizeRow($row_start) == 0) {
3327
            return;
3327
            return;
3328
        }
3328
        }
3329
        if ($this->_sizeRow($row_end)   == 0) {
3329
        if ($this->_sizeRow($row_end)   == 0) {
3330
            return;
3330
            return;
3331
        }
3331
        }
3332
 
3332
 
3333
        // Convert the pixel values to the percentage value expected by Excel
3333
        // Convert the pixel values to the percentage value expected by Excel
3334
        $x1 = $x1     / $this->_sizeCol($col_start)   * 1024;
3334
        $x1 = $x1     / $this->_sizeCol($col_start)   * 1024;
3335
        $y1 = $y1     / $this->_sizeRow($row_start)   *  256;
3335
        $y1 = $y1     / $this->_sizeRow($row_start)   *  256;
3336
        $x2 = $width  / $this->_sizeCol($col_end)     * 1024; // Distance to right side of object
3336
        $x2 = $width  / $this->_sizeCol($col_end)     * 1024; // Distance to right side of object
3337
        $y2 = $height / $this->_sizeRow($row_end)     *  256; // Distance to bottom of object
3337
        $y2 = $height / $this->_sizeRow($row_end)     *  256; // Distance to bottom of object
3338
 
3338
 
3339
        $this->_storeObjPicture($col_start, $x1,
3339
        $this->_storeObjPicture($col_start, $x1,
3340
                                 $row_start, $y1,
3340
                                 $row_start, $y1,
3341
                                 $col_end, $x2,
3341
                                 $col_end, $x2,
3342
                                 $row_end, $y2);
3342
                                 $row_end, $y2);
3343
    }
3343
    }
3344
 
3344
 
3345
    /**
3345
    /**
3346
    * Convert the width of a cell from user's units to pixels. By interpolation
3346
    * Convert the width of a cell from user's units to pixels. By interpolation
3347
    * the relationship is: y = 7x +5. If the width hasn't been set by the user we
3347
    * the relationship is: y = 7x +5. If the width hasn't been set by the user we
3348
    * use the default value. If the col is hidden we use a value of zero.
3348
    * use the default value. If the col is hidden we use a value of zero.
3349
    *
3349
    *
3350
    * @access private
3350
    * @access private
3351
    * @param integer $col The column
3351
    * @param integer $col The column
3352
    * @return integer The width in pixels
3352
    * @return integer The width in pixels
3353
    */
3353
    */
3354
    function _sizeCol($col)
3354
    function _sizeCol($col)
3355
    {
3355
    {
3356
        // Look up the cell value to see if it has been changed
3356
        // Look up the cell value to see if it has been changed
3357
        if (isset($this->col_sizes[$col])) {
3357
        if (isset($this->col_sizes[$col])) {
3358
            if ($this->col_sizes[$col] == 0) {
3358
            if ($this->col_sizes[$col] == 0) {
3359
                return(0);
3359
                return(0);
3360
            } else {
3360
            } else {
3361
                return(floor(7 * $this->col_sizes[$col] + 5));
3361
                return(floor(7 * $this->col_sizes[$col] + 5));
3362
            }
3362
            }
3363
        } else {
3363
        } else {
3364
            return(64);
3364
            return(64);
3365
        }
3365
        }
3366
    }
3366
    }
3367
 
3367
 
3368
    /**
3368
    /**
3369
    * Convert the height of a cell from user's units to pixels. By interpolation
3369
    * Convert the height of a cell from user's units to pixels. By interpolation
3370
    * the relationship is: y = 4/3x. If the height hasn't been set by the user we
3370
    * the relationship is: y = 4/3x. If the height hasn't been set by the user we
3371
    * use the default value. If the row is hidden we use a value of zero. (Not
3371
    * use the default value. If the row is hidden we use a value of zero. (Not
3372
    * possible to hide row yet).
3372
    * possible to hide row yet).
3373
    *
3373
    *
3374
    * @access private
3374
    * @access private
3375
    * @param integer $row The row
3375
    * @param integer $row The row
3376
    * @return integer The width in pixels
3376
    * @return integer The width in pixels
3377
    */
3377
    */
3378
    function _sizeRow($row)
3378
    function _sizeRow($row)
3379
    {
3379
    {
3380
        // Look up the cell value to see if it has been changed
3380
        // Look up the cell value to see if it has been changed
3381
        if (isset($this->_row_sizes[$row])) {
3381
        if (isset($this->_row_sizes[$row])) {
3382
            if ($this->_row_sizes[$row] == 0) {
3382
            if ($this->_row_sizes[$row] == 0) {
3383
                return(0);
3383
                return(0);
3384
            } else {
3384
            } else {
3385
                return(floor(4/3 * $this->_row_sizes[$row]));
3385
                return(floor(4/3 * $this->_row_sizes[$row]));
3386
            }
3386
            }
3387
        } else {
3387
        } else {
3388
            return(17);
3388
            return(17);
3389
        }
3389
        }
3390
    }
3390
    }
3391
 
3391
 
3392
    /**
3392
    /**
3393
    * Store the OBJ record that precedes an IMDATA record. This could be generalise
3393
    * Store the OBJ record that precedes an IMDATA record. This could be generalise
3394
    * to support other Excel objects.
3394
    * to support other Excel objects.
3395
    *
3395
    *
3396
    * @access private
3396
    * @access private
3397
    * @param integer $colL Column containing upper left corner of object
3397
    * @param integer $colL Column containing upper left corner of object
3398
    * @param integer $dxL  Distance from left side of cell
3398
    * @param integer $dxL  Distance from left side of cell
3399
    * @param integer $rwT  Row containing top left corner of object
3399
    * @param integer $rwT  Row containing top left corner of object
3400
    * @param integer $dyT  Distance from top of cell
3400
    * @param integer $dyT  Distance from top of cell
3401
    * @param integer $colR Column containing lower right corner of object
3401
    * @param integer $colR Column containing lower right corner of object
3402
    * @param integer $dxR  Distance from right of cell
3402
    * @param integer $dxR  Distance from right of cell
3403
    * @param integer $rwB  Row containing bottom right corner of object
3403
    * @param integer $rwB  Row containing bottom right corner of object
3404
    * @param integer $dyB  Distance from bottom of cell
3404
    * @param integer $dyB  Distance from bottom of cell
3405
    */
3405
    */
3406
    function _storeObjPicture($colL,$dxL,$rwT,$dyT,$colR,$dxR,$rwB,$dyB)
3406
    function _storeObjPicture($colL,$dxL,$rwT,$dyT,$colR,$dxR,$rwB,$dyB)
3407
    {
3407
    {
3408
        $record      = 0x005d;   // Record identifier
3408
        $record      = 0x005d;   // Record identifier
3409
        $length      = 0x003c;   // Bytes to follow
3409
        $length      = 0x003c;   // Bytes to follow
3410
 
3410
 
3411
        $cObj        = 0x0001;   // Count of objects in file (set to 1)
3411
        $cObj        = 0x0001;   // Count of objects in file (set to 1)
3412
        $OT          = 0x0008;   // Object type. 8 = Picture
3412
        $OT          = 0x0008;   // Object type. 8 = Picture
3413
        $id          = 0x0001;   // Object ID
3413
        $id          = 0x0001;   // Object ID
3414
        $grbit       = 0x0614;   // Option flags
3414
        $grbit       = 0x0614;   // Option flags
3415
 
3415
 
3416
        $cbMacro     = 0x0000;   // Length of FMLA structure
3416
        $cbMacro     = 0x0000;   // Length of FMLA structure
3417
        $Reserved1   = 0x0000;   // Reserved
3417
        $Reserved1   = 0x0000;   // Reserved
3418
        $Reserved2   = 0x0000;   // Reserved
3418
        $Reserved2   = 0x0000;   // Reserved
3419
 
3419
 
3420
        $icvBack     = 0x09;     // Background colour
3420
        $icvBack     = 0x09;     // Background colour
3421
        $icvFore     = 0x09;     // Foreground colour
3421
        $icvFore     = 0x09;     // Foreground colour
3422
        $fls         = 0x00;     // Fill pattern
3422
        $fls         = 0x00;     // Fill pattern
3423
        $fAuto       = 0x00;     // Automatic fill
3423
        $fAuto       = 0x00;     // Automatic fill
3424
        $icv         = 0x08;     // Line colour
3424
        $icv         = 0x08;     // Line colour
3425
        $lns         = 0xff;     // Line style
3425
        $lns         = 0xff;     // Line style
3426
        $lnw         = 0x01;     // Line weight
3426
        $lnw         = 0x01;     // Line weight
3427
        $fAutoB      = 0x00;     // Automatic border
3427
        $fAutoB      = 0x00;     // Automatic border
3428
        $frs         = 0x0000;   // Frame style
3428
        $frs         = 0x0000;   // Frame style
3429
        $cf          = 0x0009;   // Image format, 9 = bitmap
3429
        $cf          = 0x0009;   // Image format, 9 = bitmap
3430
        $Reserved3   = 0x0000;   // Reserved
3430
        $Reserved3   = 0x0000;   // Reserved
3431
        $cbPictFmla  = 0x0000;   // Length of FMLA structure
3431
        $cbPictFmla  = 0x0000;   // Length of FMLA structure
3432
        $Reserved4   = 0x0000;   // Reserved
3432
        $Reserved4   = 0x0000;   // Reserved
3433
        $grbit2      = 0x0001;   // Option flags
3433
        $grbit2      = 0x0001;   // Option flags
3434
        $Reserved5   = 0x0000;   // Reserved
3434
        $Reserved5   = 0x0000;   // Reserved
3435
 
3435
 
3436
 
3436
 
3437
        $header      = pack("vv", $record, $length);
3437
        $header      = pack("vv", $record, $length);
3438
        $data        = pack("V", $cObj);
3438
        $data        = pack("V", $cObj);
3439
        $data       .= pack("v", $OT);
3439
        $data       .= pack("v", $OT);
3440
        $data       .= pack("v", $id);
3440
        $data       .= pack("v", $id);
3441
        $data       .= pack("v", $grbit);
3441
        $data       .= pack("v", $grbit);
3442
        $data       .= pack("v", $colL);
3442
        $data       .= pack("v", $colL);
3443
        $data       .= pack("v", $dxL);
3443
        $data       .= pack("v", $dxL);
3444
        $data       .= pack("v", $rwT);
3444
        $data       .= pack("v", $rwT);
3445
        $data       .= pack("v", $dyT);
3445
        $data       .= pack("v", $dyT);
3446
        $data       .= pack("v", $colR);
3446
        $data       .= pack("v", $colR);
3447
        $data       .= pack("v", $dxR);
3447
        $data       .= pack("v", $dxR);
3448
        $data       .= pack("v", $rwB);
3448
        $data       .= pack("v", $rwB);
3449
        $data       .= pack("v", $dyB);
3449
        $data       .= pack("v", $dyB);
3450
        $data       .= pack("v", $cbMacro);
3450
        $data       .= pack("v", $cbMacro);
3451
        $data       .= pack("V", $Reserved1);
3451
        $data       .= pack("V", $Reserved1);
3452
        $data       .= pack("v", $Reserved2);
3452
        $data       .= pack("v", $Reserved2);
3453
        $data       .= pack("C", $icvBack);
3453
        $data       .= pack("C", $icvBack);
3454
        $data       .= pack("C", $icvFore);
3454
        $data       .= pack("C", $icvFore);
3455
        $data       .= pack("C", $fls);
3455
        $data       .= pack("C", $fls);
3456
        $data       .= pack("C", $fAuto);
3456
        $data       .= pack("C", $fAuto);
3457
        $data       .= pack("C", $icv);
3457
        $data       .= pack("C", $icv);
3458
        $data       .= pack("C", $lns);
3458
        $data       .= pack("C", $lns);
3459
        $data       .= pack("C", $lnw);
3459
        $data       .= pack("C", $lnw);
3460
        $data       .= pack("C", $fAutoB);
3460
        $data       .= pack("C", $fAutoB);
3461
        $data       .= pack("v", $frs);
3461
        $data       .= pack("v", $frs);
3462
        $data       .= pack("V", $cf);
3462
        $data       .= pack("V", $cf);
3463
        $data       .= pack("v", $Reserved3);
3463
        $data       .= pack("v", $Reserved3);
3464
        $data       .= pack("v", $cbPictFmla);
3464
        $data       .= pack("v", $cbPictFmla);
3465
        $data       .= pack("v", $Reserved4);
3465
        $data       .= pack("v", $Reserved4);
3466
        $data       .= pack("v", $grbit2);
3466
        $data       .= pack("v", $grbit2);
3467
        $data       .= pack("V", $Reserved5);
3467
        $data       .= pack("V", $Reserved5);
3468
 
3468
 
3469
		$data = $header . $data;
3469
		$data = $header . $data;
3470
		$this->_append($data);
3470
		$this->_append($data);
3471
    }
3471
    }
3472
 
3472
 
3473
    /**
3473
    /**
3474
    * Convert a 24 bit bitmap into the modified internal format used by Windows.
3474
    * Convert a 24 bit bitmap into the modified internal format used by Windows.
3475
    * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
3475
    * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
3476
    * MSDN library.
3476
    * MSDN library.
3477
    *
3477
    *
3478
    * @access private
3478
    * @access private
3479
    * @param string $bitmap The bitmap to process
3479
    * @param string $bitmap The bitmap to process
3480
    * @return array Array with data and properties of the bitmap
3480
    * @return array Array with data and properties of the bitmap
3481
    */
3481
    */
3482
    function _processBitmap($bitmap)
3482
    function _processBitmap($bitmap)
3483
    {
3483
    {
3484
        // Open file.
3484
        // Open file.
3485
        $bmp_fd = @fopen($bitmap,"rb");
3485
        $bmp_fd = @fopen($bitmap,"rb");
3486
        if (!$bmp_fd) {
3486
        if (!$bmp_fd) {
3487
            $this->raiseError("Couldn't import $bitmap");
3487
            $this->raiseError("Couldn't import $bitmap");
3488
        }
3488
        }
3489
 
3489
 
3490
        // Slurp the file into a string.
3490
        // Slurp the file into a string.
3491
        $data = fread($bmp_fd, filesize($bitmap));
3491
        $data = fread($bmp_fd, filesize($bitmap));
3492
 
3492
 
3493
        // Check that the file is big enough to be a bitmap.
3493
        // Check that the file is big enough to be a bitmap.
3494
        if (strlen($data) <= 0x36) {
3494
        if (strlen($data) <= 0x36) {
3495
            $this->raiseError("$bitmap doesn't contain enough data.\n");
3495
            $this->raiseError("$bitmap doesn't contain enough data.\n");
3496
        }
3496
        }
3497
 
3497
 
3498
        // The first 2 bytes are used to identify the bitmap.
3498
        // The first 2 bytes are used to identify the bitmap.
3499
        $identity = unpack("A2ident", $data);
3499
        $identity = unpack("A2ident", $data);
3500
        if ($identity['ident'] != "BM") {
3500
        if ($identity['ident'] != "BM") {
3501
            $this->raiseError("$bitmap doesn't appear to be a valid bitmap image.\n");
3501
            $this->raiseError("$bitmap doesn't appear to be a valid bitmap image.\n");
3502
        }
3502
        }
3503
 
3503
 
3504
        // Remove bitmap data: ID.
3504
        // Remove bitmap data: ID.
3505
        $data = substr($data, 2);
3505
        $data = substr($data, 2);
3506
 
3506
 
3507
        // Read and remove the bitmap size. This is more reliable than reading
3507
        // Read and remove the bitmap size. This is more reliable than reading
3508
        // the data size at offset 0x22.
3508
        // the data size at offset 0x22.
3509
        //
3509
        //
3510
        $size_array   = unpack("Vsa", substr($data, 0, 4));
3510
        $size_array   = unpack("Vsa", substr($data, 0, 4));
3511
        $size   = $size_array['sa'];
3511
        $size   = $size_array['sa'];
3512
        $data   = substr($data, 4);
3512
        $data   = substr($data, 4);
3513
        $size  -= 0x36; // Subtract size of bitmap header.
3513
        $size  -= 0x36; // Subtract size of bitmap header.
3514
        $size  += 0x0C; // Add size of BIFF header.
3514
        $size  += 0x0C; // Add size of BIFF header.
3515
 
3515
 
3516
        // Remove bitmap data: reserved, offset, header length.
3516
        // Remove bitmap data: reserved, offset, header length.
3517
        $data = substr($data, 12);
3517
        $data = substr($data, 12);
3518
 
3518
 
3519
        // Read and remove the bitmap width and height. Verify the sizes.
3519
        // Read and remove the bitmap width and height. Verify the sizes.
3520
        $width_and_height = unpack("V2", substr($data, 0, 8));
3520
        $width_and_height = unpack("V2", substr($data, 0, 8));
3521
        $width  = $width_and_height[1];
3521
        $width  = $width_and_height[1];
3522
        $height = $width_and_height[2];
3522
        $height = $width_and_height[2];
3523
        $data   = substr($data, 8);
3523
        $data   = substr($data, 8);
3524
        if ($width > 0xFFFF) {
3524
        if ($width > 0xFFFF) {
3525
            $this->raiseError("$bitmap: largest image width supported is 65k.\n");
3525
            $this->raiseError("$bitmap: largest image width supported is 65k.\n");
3526
        }
3526
        }
3527
        if ($height > 0xFFFF) {
3527
        if ($height > 0xFFFF) {
3528
            $this->raiseError("$bitmap: largest image height supported is 65k.\n");
3528
            $this->raiseError("$bitmap: largest image height supported is 65k.\n");
3529
        }
3529
        }
3530
 
3530
 
3531
        // Read and remove the bitmap planes and bpp data. Verify them.
3531
        // Read and remove the bitmap planes and bpp data. Verify them.
3532
        $planes_and_bitcount = unpack("v2", substr($data, 0, 4));
3532
        $planes_and_bitcount = unpack("v2", substr($data, 0, 4));
3533
        $data = substr($data, 4);
3533
        $data = substr($data, 4);
3534
        if ($planes_and_bitcount[2] != 24) { // Bitcount
3534
        if ($planes_and_bitcount[2] != 24) { // Bitcount
3535
            $this->raiseError("$bitmap isn't a 24bit true color bitmap.\n");
3535
            $this->raiseError("$bitmap isn't a 24bit true color bitmap.\n");
3536
        }
3536
        }
3537
        if ($planes_and_bitcount[1] != 1) {
3537
        if ($planes_and_bitcount[1] != 1) {
3538
            $this->raiseError("$bitmap: only 1 plane supported in bitmap image.\n");
3538
            $this->raiseError("$bitmap: only 1 plane supported in bitmap image.\n");
3539
        }
3539
        }
3540
 
3540
 
3541
        // Read and remove the bitmap compression. Verify compression.
3541
        // Read and remove the bitmap compression. Verify compression.
3542
        $compression = unpack("Vcomp", substr($data, 0, 4));
3542
        $compression = unpack("Vcomp", substr($data, 0, 4));
3543
        $data = substr($data, 4);
3543
        $data = substr($data, 4);
3544
 
3544
 
3545
        //$compression = 0;
3545
        //$compression = 0;
3546
        if ($compression['comp'] != 0) {
3546
        if ($compression['comp'] != 0) {
3547
            $this->raiseError("$bitmap: compression not supported in bitmap image.\n");
3547
            $this->raiseError("$bitmap: compression not supported in bitmap image.\n");
3548
        }
3548
        }
3549
 
3549
 
3550
        // Remove bitmap data: data size, hres, vres, colours, imp. colours.
3550
        // Remove bitmap data: data size, hres, vres, colours, imp. colours.
3551
        $data = substr($data, 20);
3551
        $data = substr($data, 20);
3552
 
3552
 
3553
        // Add the BITMAPCOREHEADER data
3553
        // Add the BITMAPCOREHEADER data
3554
        $header  = pack("Vvvvv", 0x000c, $width, $height, 0x01, 0x18);
3554
        $header  = pack("Vvvvv", 0x000c, $width, $height, 0x01, 0x18);
3555
        $data    = $header . $data;
3555
        $data    = $header . $data;
3556
 
3556
 
3557
        return (array($width, $height, $size, $data));
3557
        return (array($width, $height, $size, $data));
3558
    }
3558
    }
3559
 
3559
 
3560
    /**
3560
    /**
3561
    * Store the window zoom factor. This should be a reduced fraction but for
3561
    * Store the window zoom factor. This should be a reduced fraction but for
3562
    * simplicity we will store all fractions with a numerator of 100.
3562
    * simplicity we will store all fractions with a numerator of 100.
3563
    *
3563
    *
3564
    * @access private
3564
    * @access private
3565
    */
3565
    */
3566
    function _storeZoom()
3566
    function _storeZoom()
3567
    {
3567
    {
3568
        // If scale is 100 we don't need to write a record
3568
        // If scale is 100 we don't need to write a record
3569
        if ($this->_zoom == 100) {
3569
        if ($this->_zoom == 100) {
3570
            return;
3570
            return;
3571
        }
3571
        }
3572
 
3572
 
3573
        $record      = 0x00A0;               // Record identifier
3573
        $record      = 0x00A0;               // Record identifier
3574
        $length      = 0x0004;               // Bytes to follow
3574
        $length      = 0x0004;               // Bytes to follow
3575
 
3575
 
3576
        $header      = pack("vv", $record, $length);
3576
        $header      = pack("vv", $record, $length);
3577
        $data        = pack("vv", $this->_zoom, 100);
3577
        $data        = pack("vv", $this->_zoom, 100);
3578
		$data = $header . $data;
3578
		$data = $header . $data;
3579
		$this->_append($data);
3579
		$this->_append($data);
3580
    }
3580
    }
3581
 
3581
 
3582
    /**
3582
    /**
3583
    * FIXME: add comments
3583
    * FIXME: add comments
3584
    */
3584
    */
3585
    function setValidation($row1, $col1, $row2, $col2, &$validator)
3585
    function setValidation($row1, $col1, $row2, $col2, &$validator)
3586
    {
3586
    {
3587
        $this->_dv[] = $validator->_getData() .
3587
        $this->_dv[] = $validator->_getData() .
3588
                       pack("vvvvv", 1, $row1, $row2, $col1, $col2);
3588
                       pack("vvvvv", 1, $row1, $row2, $col1, $col2);
3589
    }
3589
    }
3590
 
3590
 
3591
    /**
3591
    /**
3592
    * Store the DVAL and DV records.
3592
    * Store the DVAL and DV records.
3593
    *
3593
    *
3594
    * @access private
3594
    * @access private
3595
    */
3595
    */
3596
    function _storeDataValidity()
3596
    function _storeDataValidity()
3597
    {
3597
    {
3598
        $record      = 0x01b2;      // Record identifier
3598
        $record      = 0x01b2;      // Record identifier
3599
        $length      = 0x0012;      // Bytes to follow
3599
        $length      = 0x0012;      // Bytes to follow
3600
 
3600
 
3601
        $grbit       = 0x0002;      // Prompt box at cell, no cached validity data at DV records
3601
        $grbit       = 0x0002;      // Prompt box at cell, no cached validity data at DV records
3602
        $horPos      = 0x00000000;  // Horizontal position of prompt box, if fixed position
3602
        $horPos      = 0x00000000;  // Horizontal position of prompt box, if fixed position
3603
        $verPos      = 0x00000000;  // Vertical position of prompt box, if fixed position
3603
        $verPos      = 0x00000000;  // Vertical position of prompt box, if fixed position
3604
        $objId       = 0xffffffff;  // Object identifier of drop down arrow object, or -1 if not visible
3604
        $objId       = 0xffffffff;  // Object identifier of drop down arrow object, or -1 if not visible
3605
 
3605
 
3606
        $header      = pack('vv', $record, $length);
3606
        $header      = pack('vv', $record, $length);
3607
        $data        = pack('vVVVV', $grbit, $horPos, $verPos, $objId,
3607
        $data        = pack('vVVVV', $grbit, $horPos, $verPos, $objId,
3608
                                     count($this->_dv));
3608
                                     count($this->_dv));
3609
		$data = $header . $data;
3609
		$data = $header . $data;
3610
		$this->_append($data);
3610
		$this->_append($data);
3611
 
3611
 
3612
        $record = 0x01be;              // Record identifier
3612
        $record = 0x01be;              // Record identifier
3613
        foreach ($this->_dv as $dv) {
3613
        foreach ($this->_dv as $dv) {
3614
            $length = strlen($dv);      // Bytes to follow
3614
            $length = strlen($dv);      // Bytes to follow
3615
            $header = pack("vv", $record, $length);
3615
            $header = pack("vv", $record, $length);
3616
			$header .= $dv;
3616
			$header .= $dv;
3617
            $this->_append($header);
3617
            $this->_append($header);
3618
        }
3618
        }
3619
    }
3619
    }
3620
}
3620
}
3621
?>
3621
?>