<commit_before><?php
/** @package    verysimple::Phreeze */

/**
 * ExportUtility Class
 *
 * This contains various utility functions for exporting Phreezable objects into other formats
 * such as Excel, CSV, tab-delimited, XML, etc
 *
 * @package    verysimple::Phreeze
 * @author     VerySimple Inc. <<EMAIL>>
 * @copyright  1997-2005 VerySimple Inc.
 * @license    http://www.gnu.org/licenses/lgpl.html  LGPL
 * @version    1.0
 */
class ExportUtility
{
	/**
	 * Streams to the browser the provided array of objects as a basic Excel document
	 * with headers.  if the objects have an associated Map class, then footers will be
	 * added to sum any numeric fields.  otherwise no footers are added
	 * 
	 * Note that PEAR PHPExcel must be installed
	 * @link https://phpexcel.codeplex.com/
	 * 
	 * @param Array an array of Phreezable objects, obtained for example, using DataSet->ToObjectArray
	 * @param Phreezer $phreezer is needed to get field maps
	 * @param string (optional) The title of the report
	 */
	static function OutputAsExcel(Array $objects, Phreezer $phreezer, $reportTitle = "Data Export", $fileName = "export.xls", $creator = "Phreeze Library")
	{
		require_once("PEAR/PHPExcel.php");
		
		// create the workbook and worksheet
		$workbook = new PHPExcel();
		
		// set workbook properties
		$workbook->getProperties()->setCreator($creator)->setTitle($fileName);
		
		$workbook->setActiveSheetIndex(0);
		$worksheet = $workbook->getActiveSheet();

		$current_column = "A";
		$current_row = 1;
		
		$worksheet->setCellValue($current_column . $current_row, $reportTitle);
		$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true)->setSize(16);
		$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');

		// default to no columns
		$fields = Array();
		$columns = Array();
		$is_numeric = Array();
		$fieldmap_exists = false;
		
		$current_row = 3;
		
		// print the headers
		// while we're looping, also parse the fields so we don't have to do 
		// it repeatedly when looping through data
		if (isset($objects[0]))
		{
			try
			{
				// see if there is a fieldmap for this object
				$fields = $phreezer->GetFieldMaps( get_class($objects[0]) );
				$fieldmap_exists = true;

				// these are the columns we'll use for enumeration from here on
				$columns = array_keys($fields);
			}
			catch (Exception $ex)
			{
				// no fieldmaps exist, so use the reflection class instead
				$reflect = new ReflectionClass($objects[0]);
				$publicAttributes = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
				$staticAttributes = $reflect->getStaticProperties();
				// only include non-static public properties
				$props = array_diff($publicAttributes,$staticAttributes);

				foreach ($props as $prop) 
				{
					$column = $prop->getName();
				    $columns[] = $column;
				}
			}
			
			foreach ($columns as $column) 
			{
				// save this so we don't check it every time when looping through data
				$is_numeric[$column] = $fieldmap_exists ? $fields[$column]->IsNumeric() : false;

    			$worksheet->setCellValue($current_column . $current_row, $column);
    			$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true)->setSize(11);
    			$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');
    					
    			$current_column++;
			}

		}
		
		$current_row = 4;
		
		// loop through all of the data
		foreach ($objects as $object)
		{
			$current_column = "A";
			foreach ($columns as $column) 
			{
				if ($fieldmap_exists == false || $is_numeric[$column] == true)
				{
					$worksheet->setCellValue($current_column . $current_row, $object->$column);
				}
				else
				{
					$worksheet->setCellValue($current_column . $current_row, $object->$column);
				}
				
    			$current_column++;
			}
			$current_row++;
		}
		
		// lastly write to the footer to sum the numeric columns
		$col = 0;
		foreach ($columns as $column) 
		{
			if ($is_numeric[$column])
			{
				$columnLetter = ExportUtility::GetColumnLetter($current_column);
				$formula = "=SUM(".$columnLetter."3:".$columnLetter.($current_row-1).")";
				
				// notice the @ sign in front because this will fire a deprecated warning due to use of "split"
				@$worksheet->setCellValue($current_row, $current_column, $formula);
				$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true);
				$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');
			}
			
    		$current_column++;
		}
		
		$workbook->getDefaultStyle()->getFont()->setName('Arial')->setSize(11);
		<commit_msg>fixed "Invalid cell coordinate" error reported in issue #162<commit_after><?php
/** @package    verysimple::Phreeze */

/**
 * ExportUtility Class
 *
 * This contains various utility functions for exporting Phreezable objects into other formats
 * such as Excel, CSV, tab-delimited, XML, etc
 *
 * @package    verysimple::Phreeze
 * @author     VerySimple Inc. <<EMAIL>>
 * @copyright  1997-2005 VerySimple Inc.
 * @license    http://www.gnu.org/licenses/lgpl.html  LGPL
 * @version    1.0
 */
class ExportUtility
{
	/**
	 * Streams to the browser the provided array of objects as a basic Excel document
	 * with headers.  if the objects have an associated Map class, then footers will be
	 * added to sum any numeric fields.  otherwise no footers are added
	 * 
	 * Note that PEAR PHPExcel must be installed
	 * @link https://phpexcel.codeplex.com/
	 * 
	 * @param Array an array of Phreezable objects, obtained for example, using DataSet->ToObjectArray
	 * @param Phreezer $phreezer is needed to get field maps
	 * @param string (optional) The title of the report
	 */
	static function OutputAsExcel(Array $objects, Phreezer $phreezer, $reportTitle = "Data Export", $fileName = "export.xls", $creator = "Phreeze Library")
	{
		require_once("PEAR/PHPExcel.php");
		
		// create the workbook and worksheet
		$workbook = new PHPExcel();
		
		// set workbook properties
		$workbook->getProperties()->setCreator($creator)->setTitle($fileName);
		
		$workbook->setActiveSheetIndex(0);
		$worksheet = $workbook->getActiveSheet();

		$current_column = "A";
		$current_row = 1;
		
		$worksheet->setCellValue($current_column . $current_row, $reportTitle);
		$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true)->setSize(16);
		$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');

		// default to no columns
		$fields = Array();
		$columns = Array();
		$is_numeric = Array();
		$fieldmap_exists = false;
		
		$current_row = 3;
		
		// print the headers
		// while we're looping, also parse the fields so we don't have to do 
		// it repeatedly when looping through data
		if (isset($objects[0]))
		{
			try
			{
				// see if there is a fieldmap for this object
				$fields = $phreezer->GetFieldMaps( get_class($objects[0]) );
				$fieldmap_exists = true;

				// these are the columns we'll use for enumeration from here on
				$columns = array_keys($fields);
			}
			catch (Exception $ex)
			{
				// no fieldmaps exist, so use the reflection class instead
				$reflect = new ReflectionClass($objects[0]);
				$publicAttributes = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
				$staticAttributes = $reflect->getStaticProperties();
				// only include non-static public properties
				$props = array_diff($publicAttributes,$staticAttributes);

				foreach ($props as $prop) 
				{
					$column = $prop->getName();
				    $columns[] = $column;
				}
			}
			
			foreach ($columns as $column) 
			{
				// save this so we don't check it every time when looping through data
				$is_numeric[$column] = $fieldmap_exists ? $fields[$column]->IsNumeric() : false;

    			$worksheet->setCellValue($current_column . $current_row, $column);
    			$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true)->setSize(11);
    			$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');
    					
    			$current_column++;
			}

		}
		
		$current_row = 4;
		
		// loop through all of the data
		foreach ($objects as $object)
		{
			$current_column = "A";
			foreach ($columns as $column) 
			{
				if ($fieldmap_exists == false || $is_numeric[$column] == true)
				{
					$worksheet->setCellValue($current_column . $current_row, $object->$column);
				}
				else
				{
					$worksheet->setCellValue($current_column . $current_row, $object->$column);
				}
				
    			$current_column++;
			}
			$current_row++;
		}
		
		// lastly write to the footer to sum the numeric columns
		$current_column = "A";
		foreach ($columns as $column) 
		{
			if ($is_numeric[$column])
			{
				$columnLetter = ExportUtility::GetColumnLetter($current_column);
				$formula = "=SUM(".$columnLetter."3:".$columnLetter.($current_row-1).")";
				
				// notice the @ sign in front because this will fire a deprecated warning due to use of "split"
				@$worksheet->setCellValue($current_column . $current_row, $formula);
				$worksheet->getStyle($current_column . $current_row)->getFont()->setBold(true);
				$worksheet->getStyle($current_column . $current_row)->getFont()->setName('Arial');
			}
			
    		$current_column++;
		}
		
		$workbook->getDefaultStyle()->getFont()->setName('Arial')->setSize(11);
		
{'fraction_non_alphanumeric': 0.13766792047286405, 'fraction_numerical': 0.005803331542181623, 'mean_word_length': 3.5461651196873474, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 91, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4320328', 'n_tokens_mistral': 3390, 'n_tokens_neox': 2829, 'n_words': 744}