Kirby Spreadsheet Plugin tweak

@texnixe i am editing your spreadsheet plugin a little to generate a bespoke table. Ive done this by copying your kirby tag in the plugin and using it as a starting point. Ive got classes on alternate TD’s working but my last hurdle is to add alternate classes to the TR tags inside the tbody. heres what ive got… can you give me a hint please? I think the answer similar to what i have but im not sure how to make it work with the $btr = new Brick('tr'); part

    $tbody = new Brick('tbody');

    while ($row = $reader->next()) {

      $cellcount = 0;

      $btr = new Brick('tr');

      foreach($row as $key => $cell) {

        $cellclass = (++$cellcount % 2) ? "odd " : "even ";

        $btr->append('<td class="' . $cellclass . 'cell">' . $cell . '</td>');

      }
      $tbody->append($btr);
    }

    $table->append($tbody);

Thanks.

You can use $btr->addClass('someclass') to add a class to each row.

Sure, but what i need is alternate classes on the rows, not the same class.

I tried this:

    while ($row = $reader->next()) {

      $cellcount = 0;
      $rowcount = 0;

      $btr = new Brick('tr');

      foreach($row as $key => $cell) {

        $rowclass = (++$rowcount % 2) ? "oddrow " : "evenrow ";
        $btr->addClass($rowclass);

        $cellclass = (++$cellcount % 2) ? "odd " : "even ";
        $btr->append('<td class="' . $cellclass . 'cell">' . $cell . '</td>');

      }
      $tbody->append($btr);
    }

    $table->append($tbody);

    return $table;

But that gives me both classes on the TR at once:

<tr class="odd even">...</tr>

Sure, but you (only) wanted a hint.

You have to add the classes outside of the foreach loop, i.e. in the while loop where the rows are created.

Blimey. Not used to being taken that literally. It was just a turn of phrase…

Anyway… i got it going with:

    $tbody = new Brick('tbody');

    $cellcount = 0;
    $rowcount = 0;

    while ($row = $reader->next()) {

      $btr = new Brick('tr');

      $rowclass = (++$rowcount % 2) ? "oddrow " : "evenrow ";
      $btr->addClass($rowclass);

      foreach($row as $key => $cell) {

        $cellclass = (++$cellcount % 2) ? "odd" : "even";
        $btr->append('<td class="' . $cellclass . 'cell">' . $cell . '</td>');

      }
      $tbody->append($btr);
    }

    $table->append($tbody);

    return $table;

How would I prevent an empty TD if it has no content? I want to spread the TD in the even rows across two columns, and remove the empty cell.

Also, is there some way to pick up on the formatting in the spreadsheet? One of these tables has a bulleted list in the cell.

Check if the cell is empty and only add a cell if not (and add the colspan attribute)

As regards the formatting, I don’t know. What does it do with the bulleted list? I don’t think that the Spreadsheet Reader supports anything more fancy than numbers and dates, but you can check the source code.

If you need more power, you might want to check out this library: https://github.com/PHPOffice/PhpSpreadsheet

Well of course, but i was looking for the best way. I tried with this but it didnt work:

    while ($row = $reader->next()) {

      $btr = new Brick('tr');

      $rowclass = (++$rowcount % 2) ? "oddrow " : "evenrow ";
      $btr->addClass($rowclass);

      foreach($row as $key => $cell) {

        if (isset($cell)) {
          $cellsettings = (++$cellcount % 2) ? 'class="odd"' : 'class="even"';
          $btr->append('<td ' . $cellsettings . '>' . $cell . '</td>');
        } else {
           // empty
        }

      }
      $tbody->append($btr);
    }

    $table->append($tbody);

Beginning to think it might be easier to build the table up with a structure field. Think i’ve opened a can of worms here.

Well, empty() would be more useful in this case than isset()

Sometimes i can be a little stupid :slight_smile: It’s been a busy morning.

Actually i think PHP reader can do this. I used to use Textpattern, and there was a very similar plugin for that using the same reader that you used for your plugin. It was clever enough to merge cells and add the rowspans and colspans accordingly. if i remember rightly it could even pull in cell colors and border styles as set in the spreadsheet.

I have a copy of this old plugins souce code for Textpattern, but I wouldnt know where to begin to port this over to Kirby.

That’s another Spreadsheet-Reader, I’m afraid.

It’s based on the same code. vendor/php-excel-reader/excel_reader2.php in your plugin is the same library for reading the excel file that is in the Textpattern plugin. Looks like they built extensions on top to handle a couple more file formats then the original reader did.

The php-excel-reader is only used for xls file parsing and needs to be required specifically.

I see. I might have a crack at this over christmas lull to see if I can build on your excellent effort and add the auto merging of cells and automatic odd / even classes on demand. Will keep me out of trouble. :slight_smile:

I don’t really understand why you would want to add odd/even classes, anyway. This can be done with CSS alone.

Of course, and this is the best way, but there are some cranky old browsers and some devices that have issues with certain uses of nth-child. Personally I don’t support old browsers, but that doesn’t mean that someone using the plugin won’t have to. I won’t render the classes by default.