Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

little extensions and bugfixes #22

Open
Peyotito opened this issue Sep 28, 2021 · 0 comments
Open

little extensions and bugfixes #22

Peyotito opened this issue Sep 28, 2021 · 0 comments

Comments

@Peyotito
Copy link

Peyotito commented Sep 28, 2021

ADD function getColCount()

/**
* This method returns number of cols in active sheet.
*
* @return int - col number of the last col.
*
* @category Sheet manipulations
*/
public function getColCount() {
return count($this->_cSheet->cols->col);
}

CHANGE date format in function formatDataRead() and ADD Currency

private function formatDataRead($style, $data){
    // get style tag
    if ((string)$style=="")
        return (string)$data;
    
    $numFmt = (string)$this->styles->cellXfs->xf[(int)$style]["numFmtId"];
    
    switch ($numFmt){
        case "14": // = 'mm-dd-yy';
        case "15": // = 'd-mmm-yy';
        case "16": // = 'd-mmm';
        case "17": // = 'mmm-yy';
        case "22": // = 'm/d/yy h:mm';
        case "18": // = 'h:mm AM/PM';
        case "19": // = 'h:mm:ss AM/PM';
        case "20": // = 'h:mm';
        case "21": // = 'h:mm:ss';
            return $this->getDateTimeString($data);
        default: 
            if ((int)$numFmt>=164){ //look for custom format number
                foreach($this->styles->numFmts[0]->numFmt as $o_numFmt){
                    if ((int)$o_numFmt["numFmtId"]==(int)$numFmt){
                        $formatCode = (string)$o_numFmt["formatCode"];
                        // look for date format
		if (preg_match("/(([d]{1,2}|[y]{2,4})[-|.|\/|:][m]{1,2}[-|.|\/|:]([y]{2,4}|[d]{1,2}))/i", $formatCode)) { // CHECK THIS OUT!!! it's just a guess!
			return $this->getDateTimeString($data);
		}
		elseif (preg_match("/(€|$)/i", $formatCode)) { //currency $ OR €
			return $data;
		}
                    }
                }
            }
            return $data;
            break;
    }
}

Bugfixes

if (!is_resource($zip))

/** @ignore */
public function unzipToDirectory($zipFilePath, $targetDirName){

    if (file_exists($targetDirName)){
        self::rmrf($targetDirName);
    }
    
    if(!@mkdir($targetDirName, 0777, true)){

        throw new eiseXLSX_Exception('Unable to create directory to unpack files');

    }


    if(!file_exists($zipFilePath))
        throw new eiseXLSX_Exception("File not found: {$zipFilePath}"); 

    $zip=zip_open($zipFilePath);
    if (!is_resource($zip)) {
        throw new eiseXLSX_Exception("Wrong file format: {$zipFilePath}"); 
    }

    while($zip_entry=zip_read($zip)) {
        $strFileName=$targetDirName. self::DS .str_replace("/", self::DS, zip_entry_name($zip_entry));
        $dir = dirname($strFileName);
        if (!file_exists($dir)) mkdir($dir, 0777, true);
        zip_entry_open($zip, $zip_entry);
        $strFile = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        file_put_contents($strFileName, $strFile);
        unset($strFile);
        zip_entry_close($zip_entry);
    }
    zip_close($zip);
    unset($zip);

}

// preserve line breaks and spaces
$data->t->addAttribute("dump:xml:space", "preserve");

/**********************************************/
// sheet data manipulation
/**********************************************/
/** @ignore  */
private function updateSharedString($o_si, $data){
    
    $dom_si = dom_import_simplexml($o_si);
         
    while ($dom_si->hasChildNodes()) {
        $dom_si->removeChild($dom_si->firstChild);
    }
    
    if (!is_object($data)){
        $data = simplexml_load_string("<richText><t>".htmlspecialchars($data)."</t></richText>");
		// preserve line breaks and spaces
		$data->t->addAttribute("dump:xml:space", "preserve");							  
    }
    
    foreach($data->children() as $childNode){
    
        $domInsert = $dom_si->ownerDocument->importNode(dom_import_simplexml($childNode), true);
        $dom_si->appendChild($domInsert);
        
    }
    
    return simplexml_import_dom($o_si);

}

if (!isset($this->_cSheet->mergeCells->mergeCell) || count($this->_cSheet->mergeCells->mergeCell)==0)

/** @ignore  */
private function shiftDownMergedCells($yStart, $yOrigin = null){
    
    if (!isset($this->_cSheet->mergeCells->mergeCell) || count($this->_cSheet->mergeCells->mergeCell)==0)
        return;
    
    $toAdd = Array();
    
    foreach($this->_cSheet->mergeCells->mergeCell as $mergeCell){
        list($cell1, $cell2) = explode(":", $mergeCell["ref"]);
        
        list($x1, $y1) = self::cellAddress($cell1);
        list($x2, $y2) = self::cellAddress($cell2);
        
        if (max($y1, $y2)>=$yStart && min($y1, $y2)<$yStart){ // if mergeCells are crossing inserted row
            throw new eiseXLSX_Exception("mergeCell {$mergeCell["ref"]} is crossing newly inserted row at {$yStart}");
        }
        
        if (min($y1, $y2)>=$yStart){
            $mergeCell["ref"] = $this->index2letter($x1).($y1+1).":".$this->index2letter($x2).($y2+1);
        }
        
        if ($yOrigin!==null)
            if ($y1==$y2 && $y1==$yOrigin){ // if there're merged cells on cloned row we add new <mergeCell>
                $toAdd[] = $this->index2letter($x1).($yStart).":".$this->index2letter($x2).($yStart);
            }
    }
    
    foreach($toAdd as $newMergeCellRange){
            $newMC = $this->_cSheet->mergeCells->addChild("mergeCell");
            $newMC["ref"] = $newMergeCellRange;
            $this->_cSheet->mergeCells["count"] = $this->_cSheet->mergeCells["count"]+1;
    }
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant