信不信由你,这是第一次有人问起从电子表格中删除图表或图像的问题。PHPExcel实际上没有任何方法,但它们对您自己添加来说足够简单。在这些集合中添加条目的方法在PHPExcel_Worksheet类中;由于集合只是一个数组,因此只需要使用unset()、array_splice()或类似的方法来删除您不想要的条目。
我正在使用PHPExcel类,我正在加载模板规范表,然后修改数据等等。
根据我动态传递脚本的内容,模板中的某些图像应该被删除,某些图形(图表)应该被删除。
我已经使用了getChartCollection()
和getDrawingCollection()
来获取所有内容,但我还没有找到修改集合的方法,以便从加载的模板。
谢谢你的帮助。Jason K.
更新-感谢Mark Baker在下面提供的信息。我写了以下代码,需要添加到工作表类中,直到1.7.9发布。我的代码在下面。
/**
* Remove drawing from collection
*
* @return PHPExcel_Worksheet_BaseDrawing[]
*/
public function removeImageByIDs($IDs)
{
if(!is_array($IDs)) $IDs = array($IDs);
$drawing_collection = $this->_drawingCollection;
$drawing_collection_copy = $drawing_collection->getArrayCopy();
foreach($IDs as $ID){
unset($drawing_collection_copy[$ID]);
}
$drawing_collection_copy = array_values($drawing_collection_copy);
$drawing_collection->exchangeArray($drawing_collection_copy);
$this->_drawingCollection = $drawing_collection;
unset($drawing_collection_copy);
unset($drawing_collection);
return $this->_drawingCollection;
}
/**
* Remove chart from collection
*
* @return PHPExcel_Worksheet_BaseDrawing[]
*/
public function removeChartByIDs($IDs)
{
if(!is_array($IDs)) $IDs = array($IDs);
$chart_collection = $this->_chartCollection;
$chart_collection_copy = $chart_collection->getArrayCopy();
foreach($IDs as $ID){
unset($chart_collection_copy[$ID]);
}
$chart_collection_copy = array_values($chart_collection_copy);
$chart_collection->exchangeArray($chart_collection_copy);
$this->_chartCollection = $chart_collection;
unset($chart_collection_copy);
unset($chart_collection);
return $this->_chartCollection;
}
享受吧。Jason K.