Skip to content

row.styles in tables #63

@MP70

Description

@MP70

When adding new rows to a table using the ModifyTable class, it currently applies the styles of row 0 to the new rows (well actually an empty style object and this seems to be powerpoints default behaviour for that). For instance, if row 0 is a header with larger and bold text, and rows 1-3 have normal smaller text, calling modify with 10 rows results in rows 1-3 keeping their original text style, while rows 4-9 have the header text style.

Although it's possible to override this using the style object for each row, it requires the application to be aware of the table styling, instead of allowing the user to freely style it in the template file and have the styles respected.

It doesn't actually matter in my use of this library whatsoever but I did stumble upon it, and it seemed perhaps suboptimal.

To address this, we could introduce a "has header" toggle or a "repeatLastRowStyles" option that repeats last row style rather than empty row style. This would allow the user to choose how new rows should inherit styles from existing rows, providing a more flexible solution.

Something like this ? What do you think :)

setRows() {
  const lastRowIndex = this.data.body.length - 1;
  const lastRowStyles = this.data.body[lastRowIndex]?.styles || {};

  this.data.body.forEach((row: TableRow, r: number) => {
    row.values.forEach((cell: number | string, c: number) => {
      let rowStyles = {};

      if (row.styles && row.styles[c]) {
        rowStyles = row.styles[c];
      } else if (this.data.repeatLastRowStyles) {
        rowStyles = lastRowStyles[c] ? lastRowStyles[c] : {};
      } else {
        rowStyles = {};
      }

      this.table.modify(
        this.row(r, this.column(c, this.cell(cell, rowStyles))),
      );
      this.table.modify({
        'a16:rowId': {
          index: r,
          modify: ModifyXmlHelper.attribute('val', r),
        },
      });
    });
  });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions