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

cloneRow Merge malfunction #600

Open
31b41a59l26u53 opened this issue Aug 25, 2015 · 3 comments
Open

cloneRow Merge malfunction #600

31b41a59l26u53 opened this issue Aug 25, 2015 · 3 comments

Comments

@31b41a59l26u53
Copy link

31b41a59l26u53 commented Aug 25, 2015

If the first column of the table is merged, the cloneRow will duplicate some content after the table. Here's an example:
Input:
input
Output:
output
My code:

cloneRow('emg', 3); $templateProcessor->setValue('emg#1', htmlspecialchars('1')); $templateProcessor->setValue('emg#2', htmlspecialchars('2')); $templateProcessor->setValue('emg#3', htmlspecialchars('3')); $templateProcessor->saveAs('results/WPOutput.docx'); ?>

As you can see it's an error.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@31b41a59l26u53
Copy link
Author

I managed to fix this issue by altering the source. A added a few extra lines to TemplateProcessor.php
I copied here the segment witch contains the changes, and I commented the extra lines.

public function cloneRow($search, $numberOfClones)
{
    if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
        $search = '${' . $search . '}';
    }

    $tagPos = strpos($this->temporaryDocumentMainPart, $search);
    if (!$tagPos) {
        throw new Exception("Can not clone row, template variable not found or variable contains markup.");
    }

    $rowStart = $this->findRowStart($tagPos);
    $rowEnd = $this->findRowEnd($tagPos);
    $xmlRow = $this->getSlice($rowStart, $rowEnd);

    // Check if there's a cell spanning multiple rows.
    if (preg_match('#<w:vMerge w:val="restart"/>#', $xmlRow)) {
        // $extraRowStart = $rowEnd;
        $extraRowEnd = $rowEnd;
        $saveExtraRowStart = -1;//NEW
        while (true) {
            $extraRowStart = $this->findRowStart($extraRowEnd + 1);
            $extraRowEnd = $this->findRowEnd($extraRowEnd + 1);

            // If extraRowEnd is lower then 7, there was no next row found.
            if ($extraRowEnd < 7) {
                break;
            }

            // If tmpXmlRow doesn't contain continue, this row is no longer part of the spanned row.
            $tmpXmlRow = $this->getSlice($extraRowStart, $extraRowEnd);
            if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
                !preg_match('#<w:vMerge w:val="continue" />#', $tmpXmlRow)) {
                break;
            }
            if ($saveExtraRowStart == $extraRowStart){//NEW
                break;//NEW
            }//NEW
            // This row was a spanned row, update $rowEnd and search for the next row.
            $rowEnd = $extraRowEnd;
            $saveExtraRowStart = $extraRowStart;//NEW
        }
        $xmlRow = $this->getSlice($rowStart, $rowEnd);
    }

    $result = $this->getSlice(0, $rowStart);
    for ($i = 1; $i <= $numberOfClones; $i++) {
        $result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlRow);
    }
    $result .= $this->getSlice($rowEnd);

    $this->temporaryDocumentMainPart = $result;
}

@ruros
Copy link

ruros commented Nov 23, 2018

Additionally in the code above you need to rename $this->temporaryDocumentMainPart property to $this->tempDocumentMainPart. It was renamed in 9770f44 .

@matthieubarbaresco
Copy link

Hi guys,

I'm trying to do a "complex" table with this code, but the result is :

Véhicule 1 Type de véhicule Voiture
Véhicule 2 Type de véhicule Moto
Véhicule 3 Type de véhicule Autre
-- Numéro d’immatriculation ${vehicle_registration_number}
-- D’une valeur de ${vehicle_value}
-- Est attribué à ${vehicle_owner}

Vehicule 3 is merged on 4 lines.

Here, the template :

Véhicule ${vehicle_ID} Type de véhicule ${typeof_vehicle}
-- Numéro d’immatriculation ${vehicle_registration_number}
-- D’une valeur de ${vehicle_value}
-- Est attribué à ${vehicle_owner}

Véhicule ${vehicle_ID} is merged on 4 lines.

Here, the code :

`
$cpt = 1;
$Otemplate_processor->cloneRow( 'vehicle_ID', count( $answers["side_1"]["vehicles"] ) );

	foreach ( $answers["side_1"]["vehicles"] as $id => $vehicle ) {
		$Otemplate_processor->setValue( "vehicle_ID#$cpt", $cpt );
		$Otemplate_processor->setValue( "typeof_vehicle#$cpt", ucfirst( $vehicle["typeof_vehicle"] ) );
		$Otemplate_processor->setValue( "vehicle_registration_number#$cpt", $vehicle["vehicle_registration_number"] );
		$Otemplate_processor->setValue( "vehicle_value#$cpt", $vehicle["vehicle_value"] . " €" );
		$Otemplate_processor->setValue( "vehicle_owner#$cpt", ucfirst( t_get_user_meta( $vehicle["vehicle_owner"], "last_name", true ) ) . " " . ucfirst( t_get_user_meta( $vehicle["vehicle_owner"], "first_name", true ) ) );

		$cpt ++;
	}

`

Number rows to clone is right.

What do you think about ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants