Skip to content

Commit

Permalink
Merge pull request #140 from mk-j/2017-07-0.35-rc1
Browse files Browse the repository at this point in the history
2017 07 0.35 rc1
  • Loading branch information
mk-j authored Aug 23, 2017
2 parents 94d5bb0 + 8f1aaac commit 34188d1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 30 deletions.
13 changes: 13 additions & 0 deletions examples/ex07-widths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
set_include_path( get_include_path().PATH_SEPARATOR."..");
include_once("xlsxwriter.class.php");


$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $rowdata = array(300,234,456,789), $col_options = ['widths'=>[10,20,30,40]] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>20] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>30] );
$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>40] );
$writer->writeToFile('xlsx-widths.xlsx');


21 changes: 12 additions & 9 deletions examples/ex07-advanced.php → examples/ex08-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
array(100, 200, 300, 400, 500),
array(110, 210, 310, 410, 510),
);
$writer->writeSheetHeader($sheet1, $header, $suppress_header_row = true);
$writer->writeSheetHeader($sheet1, $header, $col_options = ['suppress_row'=>true] );
foreach($rows as $row)
$writer->writeSheetRow($sheet1, $row);
$writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=4);

//----
$sheet2 = 'utf8_examples';
$sheet2 = 'utf8';
$rows = array(
array('Spreadsheet','_'),
array("Hoja de cálculo", "Hoja de c\xc3\xa1lculo"),
Expand All @@ -30,15 +30,18 @@
$writer->writeSheet($rows, $sheet2);

//----
$sheet3 = 'font_example';
$sheet3 = 'fonts';
$format = array('font'=>'Arial','font-size'=>10,'font-style'=>'bold,italic', 'fill'=>'#eee','color'=>'#f00','fill'=>'#ffc', 'border'=>'top,bottom', 'halign'=>'center');
$writer->writeSheetRow($sheet3, $row=array(101,102,103,104,105,106,107,108,109,110), $format);
$writer->writeSheetRow($sheet3, $row=array(201,202,203,204,205,206,207,208,209,210), $format);

$rows = array(
array(101,102,103,104,105,106,107,108,109,110),
array(201,202,203,204,205,206,207,208,209,210),
);
foreach($rows as $row)
$writer->writeSheetRow($sheet3, $row, $format);

//----
$sheet4 = 'row_options';
$writer->writeSheetHeader($sheet4, ["col1"=>"string", "col2"=>"string"], $col_options = array('widths'=>[10,10]) );
$writer->writeSheetRow($sheet4, array(101,'this text will wrap' ), $row_options = array('height'=>30,'wrap_text'=>true));
$writer->writeSheetRow($sheet4, array(201,'this text is hidden' ), $row_options = array('height'=>30,'hidden'=>true));
$writer->writeSheetRow($sheet4, array(301,'this text will not wrap'), $row_options = array('height'=>30,'collapsed'=>true));
$writer->writeToFile('xlsx-advanced.xlsx');


94 changes: 73 additions & 21 deletions xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function writeToFile($filename)
$zip->close();
}

protected function initializeSheet($sheet_name)
protected function initializeSheet($sheet_name, $col_widths=array() )
{
//if already initialized
if ($this->current_sheet==$sheet_name || isset($this->sheets[$sheet_name]))
Expand Down Expand Up @@ -143,7 +143,14 @@ protected function initializeSheet($sheet_name)
$sheet->file_writer->write( '</sheetView>');
$sheet->file_writer->write( '</sheetViews>');
$sheet->file_writer->write( '<cols>');
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="1025" min="1" style="0" width="11.5"/>');
$i=0;
if (!empty($col_widths)) {
foreach($col_widths as $column_width) {
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="'.($i+1).'" min="'.($i+1).'" style="0" width="'.floatval($column_width).'"/>');
$i++;
}
}
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="1024" min="'.($i+1).'" style="0" width="11.5"/>');
$sheet->file_writer->write( '</cols>');
$sheet->file_writer->write( '<sheetData>');
}
Expand Down Expand Up @@ -172,21 +179,30 @@ private function initializeColumnTypes($header_types)
return $column_types;
}

public function writeSheetHeader($sheet_name, array $header_types, $suppress_row = false)
public function writeSheetHeader($sheet_name, array $header_types, $col_options = null)
{
if (empty($sheet_name) || empty($header_types) || !empty($this->sheets[$sheet_name]))
return;

self::initializeSheet($sheet_name);
$suppress_row = isset($col_options['suppress_row']) ? intval($col_options['suppress_row']) : false;
if (is_bool($col_options))
{
self::log( "Warning! passing $suppress_row=false|true to writeSheetHeader() is deprecated, this will be removed in a future version." );
$suppress_row = intval($col_options);
}
$style = &$col_options;

$col_widths = isset($col_options['widths']) ? (array)$col_options['widths'] : array();
self::initializeSheet($sheet_name, $col_widths);
$sheet = &$this->sheets[$sheet_name];
$sheet->columns = $this->initializeColumnTypes($header_types);
if (!$suppress_row)
{
$header_row = array_keys($header_types);
$header_row = array_keys($header_types);

$sheet->file_writer->write('<row collapsed="false" customFormat="false" customHeight="false" hidden="false" ht="12.1" outlineLevel="0" r="' . (1) . '">');
foreach ($header_row as $c => $v) {
$cell_style_idx = $this->addCellStyle( 'GENERAL', $style_string=null );
$cell_style_idx = empty($style) ? $sheet->columns[$c]['default_cell_style'] : $this->addCellStyle( 'GENERAL', json_encode(isset($style[0]) ? $style[$c] : $style) );
$this->writeCell($sheet->file_writer, 0, $c, $v, $number_format_type='n_string', $cell_style_idx);
}
$sheet->file_writer->write('</row>');
Expand All @@ -195,19 +211,32 @@ public function writeSheetHeader($sheet_name, array $header_types, $suppress_row
$this->current_sheet = $sheet_name;
}

public function writeSheetRow($sheet_name, array $row, $style=null)
public function writeSheetRow($sheet_name, array $row, $row_options=null)
{
if (empty($sheet_name) || empty($row))
if (empty($sheet_name))
return;

self::initializeSheet($sheet_name);
$sheet = &$this->sheets[$sheet_name];
if (empty($sheet->columns))
if (count($sheet->columns) < count($row)) {
$default_column_types = $this->initializeColumnTypes( array_fill($from=0, $until=count($row), 'GENERAL') );//will map to n_auto
$sheet->columns = array_merge((array)$sheet->columns, $default_column_types);
}

if (!empty($row_options))
{
$ht = isset($row_options['height']) ? floatval($row_options['height']) : 12.1;
$customHt = isset($row_options['height']) ? true : false;
$hidden = isset($row_options['hidden']) ? boolval($row_options['hidden']) : false;
$collapsed = isset($row_options['collapsed']) ? boolval($row_options['collapsed']) : false;
$sheet->file_writer->write('<row collapsed="'.($collapsed).'" customFormat="false" customHeight="'.($customHt).'" hidden="'.($hidden).'" ht="'.($ht).'" outlineLevel="0" r="' . ($sheet->row_count + 1) . '">');
}
else
{
$sheet->columns = $this->initializeColumnTypes( array_fill($from=0, $until=count($row), 'GENERAL') );//will map to n_auto
$sheet->file_writer->write('<row collapsed="false" customFormat="false" customHeight="false" hidden="false" ht="12.1" outlineLevel="0" r="' . ($sheet->row_count + 1) . '">');
}

$sheet->file_writer->write('<row collapsed="false" customFormat="false" customHeight="false" hidden="false" ht="12.1" outlineLevel="0" r="' . ($sheet->row_count + 1) . '">');
$style = &$row_options;
$c=0;
foreach ($row as $v) {
$number_format = $sheet->columns[$c]['number_format'];
Expand All @@ -221,6 +250,12 @@ public function writeSheetRow($sheet_name, array $row, $style=null)
$this->current_sheet = $sheet_name;
}

public function countSheetRows($sheet_name = '')
{
$sheet_name = $sheet_name ?: $this->current_sheet;
return array_key_exists($sheet_name, $this->sheets) ? $this->sheets[$sheet_name]->row_count : 0;
}

protected function finalizeSheet($sheet_name)
{
if (empty($sheet_name) || $this->sheets[$sheet_name]->finalized)
Expand Down Expand Up @@ -350,6 +385,11 @@ protected function styleFontIndexes()
$style_indexes[$i]['alignment'] = true;
$style_indexes[$i]['valign'] = $style['valign'];
}
if (isset($style['wrap_text']))
{
$style_indexes[$i]['alignment'] = true;
$style_indexes[$i]['wrap_text'] = $style['wrap_text'];
}

$font = $default_font;
if (isset($style['font-size']))
Expand Down Expand Up @@ -391,7 +431,7 @@ protected function writeStylesXML()
$fonts = $r['fonts'];
$borders = $r['borders'];
$style_indexes = $r['styles'];

$temporary_filename = $this->tempFilename();
$file = new XLSXWriter_BuffererWriter($temporary_filename);
$file->write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n");
Expand Down Expand Up @@ -437,7 +477,7 @@ protected function writeStylesXML()
}
}
$file->write('</fills>');

$file->write('<borders count="'.(count($borders)).'">');
$file->write( '<border diagonalDown="false" diagonalUp="false"><left/><right/><top/><bottom/><diagonal/></border>');
foreach($borders as $border) {
Expand All @@ -453,7 +493,7 @@ protected function writeStylesXML()
}
}
$file->write('</borders>');

$file->write('<cellStyleXfs count="20">');
$file->write( '<xf applyAlignment="true" applyBorder="true" applyFont="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="164">');
$file->write( '<alignment horizontal="general" indent="0" shrinkToFit="false" textRotation="0" vertical="bottom" wrapText="false"/>');
Expand All @@ -479,7 +519,7 @@ protected function writeStylesXML()
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="42"/>');
$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="1" numFmtId="9"/>');
$file->write('</cellStyleXfs>');

$file->write('<cellXfs count="'.(count($style_indexes)).'">');
//$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="164" xfId="0"/>');
//$file->write( '<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="0" numFmtId="165" xfId="0"/>');
Expand All @@ -488,6 +528,7 @@ protected function writeStylesXML()
foreach($style_indexes as $v)
{
$applyAlignment = isset($v['alignment']) ? 'true' : 'false';
$wrapText = isset($v['wrap_text']) ? boolval($v['wrap_text']) : 'false';
$horizAlignment = isset($v['halign']) ? $v['halign'] : 'general';
$vertAlignment = isset($v['valign']) ? $v['valign'] : 'bottom';
$applyBorder = isset($v['border_idx']) ? 'true' : 'false';
Expand All @@ -497,7 +538,7 @@ protected function writeStylesXML()
$fontIdx = isset($v['font_idx']) ? intval($v['font_idx']) : 0;
//$file->write('<xf applyAlignment="'.$applyAlignment.'" applyBorder="'.$applyBorder.'" applyFont="'.$applyFont.'" applyProtection="false" borderId="'.($borderIdx).'" fillId="'.($fillIdx).'" fontId="'.($fontIdx).'" numFmtId="'.(164+$v['num_fmt_idx']).'" xfId="0"/>');
$file->write('<xf applyAlignment="'.$applyAlignment.'" applyBorder="'.$applyBorder.'" applyFont="'.$applyFont.'" applyProtection="false" borderId="'.($borderIdx).'" fillId="'.($fillIdx).'" fontId="'.($fontIdx).'" numFmtId="'.(164+$v['num_fmt_idx']).'" xfId="0">');
$file->write(' <alignment horizontal="'.$horizAlignment.'" vertical="'.$vertAlignment.'" textRotation="0" wrapText="false" indent="0" shrinkToFit="false"/>');
$file->write(' <alignment horizontal="'.$horizAlignment.'" vertical="'.$vertAlignment.'" textRotation="0" wrapText="'.$wrapText.'" indent="0" shrinkToFit="false"/>');
$file->write(' <protection locked="true" hidden="false"/>');
$file->write('</xf>');
}
Expand Down Expand Up @@ -558,7 +599,8 @@ protected function buildWorkbookXML()
$workbook_xml.='<bookViews><workbookView activeTab="0" firstSheet="0" showHorizontalScroll="true" showSheetTabs="true" showVerticalScroll="true" tabRatio="212" windowHeight="8192" windowWidth="16384" xWindow="0" yWindow="0"/></bookViews>';
$workbook_xml.='<sheets>';
foreach($this->sheets as $sheet_name=>$sheet) {
$workbook_xml.='<sheet name="'.self::xmlspecialchars($sheet->sheetname).'" sheetId="'.($i+1).'" state="visible" r:id="rId'.($i+2).'"/>';
$sheetname = self::sanitize_sheetname($sheet->sheetname);
$workbook_xml.='<sheet name="'.self::xmlspecialchars($sheetname).'" sheetId="'.($i+1).'" state="visible" r:id="rId'.($i+2).'"/>';
$i++;
}
$workbook_xml.='</sheets>';
Expand Down Expand Up @@ -629,11 +671,21 @@ public static function sanitize_filename($filename) //http://msdn.microsoft.com/
return str_replace($all_invalids, "", $filename);
}
//------------------------------------------------------------------
public static function sanitize_sheetname($sheetname)
{
static $badchars = '\\/?*:[]';
static $goodchars = ' ';
$sheetname = strtr($sheetname, $badchars, $goodchars);
$sheetname = substr($sheetname, 0, 31);
$sheetname = trim(trim(trim($sheetname),"'"));//trim before and after trimming single quotes
return !empty($sheetname) ? $sheetname : 'Sheet'.((rand()%900)+100);
}
//------------------------------------------------------------------
public static function xmlspecialchars($val)
{
//note, badchars includes \t\n\r \x09\x0a\x0d
static $badchars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f";
static $goodchars = " ";
//note, badchars does not include \t\n\r (\x09\x0a\x0d)
static $badchars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f";
static $goodchars = " ";
return strtr(htmlspecialchars($val, ENT_QUOTES | ENT_XML1), $badchars, $goodchars);//strtr appears to be faster than str_replace
}
//------------------------------------------------------------------
Expand Down Expand Up @@ -715,7 +767,7 @@ public static function convert_date_time($date_input) //thanks to Excel::Writer:
{
list($junk,$year,$month,$day) = $matches;
}
if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $date_time, $matches))
if (preg_match("/(\d+):(\d{2}):(\d{2})/", $date_time, $matches))
{
list($junk,$hour,$min,$sec) = $matches;
$seconds = ( $hour * 60 * 60 + $min * 60 + $sec ) / ( 24 * 60 * 60 );
Expand Down

0 comments on commit 34188d1

Please sign in to comment.