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

Using Same Table Style on Different Tables Links Cell Sizes #608

Open
ComputerTinker opened this issue Aug 31, 2015 · 1 comment
Open

Using Same Table Style on Different Tables Links Cell Sizes #608

ComputerTinker opened this issue Aug 31, 2015 · 1 comment

Comments

@ComputerTinker
Copy link
Contributor

ComputerTinker commented Aug 31, 2015

Ran into an interesting problem the other day. If you create two separate tables in PHPWord but use the same table style for both of them, when you resize the columns in either table, the columns in both tables get resized. Code to reproduce:

$objPHPWord->addTableStyle("Style1", null, null);

$section = $objPHPWord->addSection();

$t1 = $section->addTable("Style1");
$t1->addRow();
  $t1r1c1 = $t1->addCell();
    $t1r1c1->addText("T1 R1 C1");
  $t1r1c2 = $t1->addCell();
    $t1r1c2->addText("T1 R1 C2");
$t1->addRow();
  $t1r2c1 = $t1->addCell();
    $t1r2c1->addText("T1 R2 C1");
  $t1r2c2 = $t1->addCell();
    $t1r2c2->addText("T1 R2 C2");

$t2 = $section->addTable("Style1");
$t2->addRow();
  $t2r1c1 = $t2->addCell();
    $t2r1c1->addText("T2 R1 C1");
  $t2r1c2 = $t2->addCell();
    $t2r1c2->addText("T2 R1 C2");
$t2->addRow();
  $t2r2c1 = $t2->addCell();
    $t2r2c1->addText("T2 R2 C1");
  $t2r2c2 = $t2->addCell();
    $t2r2c2->addText("T2 R2 C2");

Screenshot after resizing just the columns in one of the tables. Word 2013 acts as though you really only have one table on the page instead of two:

screen1

Now, here's the code for the workaround. Note that the only change is that I created a second table style and told the second table I created to use Style2 instead of Style1:

$objPHPWord->addTableStyle("Style1", null, null);
$objPHPWord->addTableStyle("Style2", null, null);

$section = $objPHPWord->addSection();

$t1 = $section->addTable("Style1");
$t1->addRow();
  $t1r1c1 = $t1->addCell();
    $t1r1c1->addText("T1 R1 C1");
  $t1r1c2 = $t1->addCell();
    $t1r1c2->addText("T1 R1 C2");
$t1->addRow();
  $t1r2c1 = $t1->addCell();
    $t1r2c1->addText("T1 R2 C1");
  $t1r2c2 = $t1->addCell();
    $t1r2c2->addText("T1 R2 C2");

$t2 = $section->addTable("Style2");
$t2->addRow();
  $t2r1c1 = $t2->addCell();
    $t2r1c1->addText("T2 R1 C1");
  $t2r1c2 = $t2->addCell();
    $t2r1c2->addText("T2 R1 C2");
$t2->addRow();
  $t2r2c1 = $t2->addCell();
    $t2r2c1->addText("T2 R2 C1");
  $t2r2c2 = $t2->addCell();
    $t2r2c2->addText("T2 R2 C2");

Here's the screenshot showing that the columns now resize correctly, and Word is indeed acting as though I've got two separate tables on my page:

screen2

I'm writing the file out in Word2007 format. Wondering if there's some matching being done on the style name for the tables, and the writer is assuming they are the same table when in fact they are separate.


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

@ComputerTinker
Copy link
Contributor Author

Hmmmm. Maybe it's not the writer's fault? I just looked at the document.xml which got written out, and I can clearly see two tables. Could it be a bug in Microsoft's XML parsing when reading the file in on their end?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http:https://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http:https://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http:https://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http:https://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http:https://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http:https://schemas.microsoft.com/office/word/2006/wordml">

<w:body>

<w:tbl>
  <w:tblGrid><w:gridCol/><w:gridCol/></w:tblGrid>
  <w:tblPr><w:tblStyle w:val="Style1"/></w:tblPr>
  <w:tr><w:trPr/>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T1 R1 C1</w:t></w:r></w:p></w:tc>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T1 R1 C2</w:t></w:r></w:p></w:tc>
  </w:tr>
  <w:tr><w:trPr/>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T1 R2 C1</w:t></w:r></w:p></w:tc>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T1 R2 C2</w:t></w:r></w:p></w:tc>
  </w:tr>
</w:tbl>

<w:tbl>
  <w:tblGrid><w:gridCol/><w:gridCol/></w:tblGrid>
  <w:tblPr><w:tblStyle w:val="Style1"/></w:tblPr>
  <w:tr><w:trPr/>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T2 R1 C1</w:t></w:r></w:p></w:tc>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T2 R1 C2</w:t></w:r></w:p></w:tc>
  </w:tr>
  <w:tr><w:trPr/>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T2 R2 C1</w:t></w:r></w:p></w:tc>
    <w:tc><w:tcPr><w:tcW w:w="" w:type="dxa"/></w:tcPr><w:p><w:pPr/><w:r><w:rPr/><w:t xml:space="preserve">T2 R2 C2</w:t></w:r></w:p></w:tc>
  </w:tr>
</w:tbl>

<w:sectPr>
  <w:pgSz w:orient="portrait" w:w="11870" w:h="16787"/>
  <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
  <w:cols w:num="1" w:space="720"/>
</w:sectPr>

</w:body>
</w:document>

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

1 participant