<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Square MI &#187; PHPExcel</title>
	<atom:link href="http://www.square-mi.com/wp/category/development/php-language/phpexcel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.square-mi.com/wp</link>
	<description>oracle, php, java, etc... technical memo &#38; つれづれなこと</description>
	<lastBuildDate>Tue, 12 Feb 2013 12:22:08 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Notice: Undefined offset &#8230; PHPExcel.php on line 191</title>
		<link>http://www.square-mi.com/wp/263/notice-undefined-offset-phpexcel-php-on-line-191/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=notice-undefined-offset-phpexcel-php-on-line-191</link>
		<comments>http://www.square-mi.com/wp/263/notice-undefined-offset-phpexcel-php-on-line-191/#comments</comments>
		<pubDate>Thu, 01 Nov 2012 06:00:13 +0000</pubDate>
		<dc:creator>commerce</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPExcel]]></category>
		<category><![CDATA[開発]]></category>

		<guid isPermaLink="false">http://www.square-mi.com/wp/?p=263</guid>
		<description><![CDATA[で、掲題の Notice: Undefined offset &#8230; PHPExcel.php on line 191 が、出てしまう。 出現箇所は、 $writer-&#62;save(&#8216;new.xl &#8230; <a href="http://www.square-mi.com/wp/263/notice-undefined-offset-phpexcel-php-on-line-191/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<pre class="brush: php; light: true; title: ; notranslate">
$reader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $reader-&gt;load('template.xls');
$tempSheet = $excel-&gt;getSheet(0);

for( ある回数分繰り返し ) {
  $cloneSheet = $tempSheet-&gt;copy();
  $excel-&gt;addSheet( $cloneSheet );
  ( $cloneSheet への編集処理 )
}

$excel-&gt;removeSheetByIndex( 0 );

$writer =
  PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer-&gt;save('new.xls');
</pre>
<p>で、掲題の<br />
Notice: Undefined offset &#8230; PHPExcel.php on line 191<br />
が、出てしまう。</p>
<p>出現箇所は、<br />
$writer-&gt;save(&#8216;new.xls&#8217;);</p>
<p>編集処理のところに</p>
<p>$cloneSheet-&gt;getStyle(&#8216;A1&#8242;)-&gt;getFont()-&gt;setBold(true);</p>
<p>を入れた時点で、このメッセージが出るようになった。</p>
<p>それで、メッセージ内容に従い、PHPExcel.php 191行目をみてみると getActiveSheet() の中で出ていることが判明。</p>
<p>echo &#8220;count=&#8221; . $this-&gt;_workSheetCollection;<br />
echo &#8220;index=&#8221; . $this-&gt;_activeSheetIndex;</p>
<p>とやって、配列数とインデックスを表示するという調査を行ったところ、</p>
<p>$excel-&gt;removeSheetByIndex(0)</p>
<p>でシート削除した後も</p>
<p>$excel-&gt;addSheet()</p>
<p>した、最終Sheetのインデックスを$this-&gt;_activeSheetIndex<br />
として、内部で持っているようである。</p>
<p>なので、シート削除直後に</p>
<p><strong>$excel-&gt;setActiveSheetIndex(0);</strong></p>
<p>を挿入することで、掲題のメッセージは表示されなくなった。</p>
<p>なんか、バグっぽい！！</p>
<p>それともうひとつ、</p>
<p>$cloneSheet への編集処理を</p>
<p>$excel-&gt;addSheet( $cloneSheet );</p>
<p>の後で行なっているのは何故か？</p>
<p>先に編集処理を行うと、また　編集処理中の setBold() のせいで</p>
<h4>PHP Fatal error:  Call to a member function getStyle() on a non-object in Style/Font.php on line 386</h4>
<p>になってしまう。</p>
<p>それで、またメッセージ内容に従い、Font.php 386行目を見てみた。</p>
<p>$this-&gt;getActiveSheet()-&gt;getStyle(&#8230;);</p>
<p>でエラーになっているらしい。</p>
<p>つまり、コピーしたばかりの $cloneSheet には、所属するBook(PHPExcelオブジェクト)がなく、getActiveSheet() が失敗するということらしい。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.square-mi.com/wp/263/notice-undefined-offset-phpexcel-php-on-line-191/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
