• Resolved wpusr007

    (@wpusr007)


    Hello,

    I am trying to do something rather simple but I cannot get it to work. I am trying to SUM all rows of a certain column representing the monetary value of items. Each row represents an item.

    I’d like the total to be on the last row (like in Excel with a table). If the table size (number of rows) was “static” (not changing), that’d be very simple, but the table changes constantly (items are added or removed) so the number of rows will always change.

    Therefore I cannot use something like
    =Total Cost: {SUM(G2:G80)}USD
    because the end row (G80) is “hardcoded” in the formula and when the table has 80+ rows, the SUM will be erroneous…

    I also cannot use
    =Total Cost: {SUM(G2:G1000)}USD
    The system returns “!ERROR! Cell G80 does not exist” because my table currently has only 79 rows… (it’s out of bound).

    Finally I tried to use a function inside of the SUM equation so it can start the SUM at G2 (the first data row after the header row) and end at NUM_ROWS-1 (which is the last data row before the footer row) but it returns all kind of errors…

    How can this be achieved?

    Second issue, I would like to display the number of rows on the page. Using a shortcode like

    [table-info id=1 field=number_rows /]

    returns “1”…. (By the way if the last ROW is used as footer, the above shortcode will return 0, not sure if this is a bug or not).

    Thanks!

    • This topic was modified 2 years, 11 months ago by wpusr007.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    This is indeed a rather tricky thing, and there’s no way with just formulas that I’m aware of.

    The approach with the [table-info] Shortcode should however make this possible. It’s really strange though that [table-info id=1 field=number_rows /] is returning just a 1, if the table has more rows (at least that’s what I assume?).
    That difference with the footer active is indeed expected. To get the footer row counted as well, simply use

    [table-info id=1 field=number_rows format=raw /]
    

    Then, you could use the TablePress Extension from https://tablepress.org/extensions/shortcodes-before-formulas/ which will allow using Shortcodes inside formulas. To turn that on, use the extended Shortcode

    [table id=1 shortcodes_before_formulas=true /]
    

    when showing the table.
    In the formula in the footer row, it should then be possible to use

    =Total Cost: {SUM(G2:G[table-info id=1 field=number_rows /])}USD
    

    (but I haven’t tested this).

    Regards,
    Tobias

    Thread Starter wpusr007

    (@wpusr007)

    Hello Tobias,

    No worries, there is no troubles at all! ??

    I installed your plugin for the shortcode before formulae. However, I found that the shortcode

    [table-info id=1 field=number_rows /]

    returns 1 (or 2 if I use format=raw in the shortcode) because the table indeed has only 1 row (excluding the header). The table is built with only one row and using Pods magic tags in the table cells (using TablePress Extension Pods tables). The table dynamically adjusts (number of rows) depending on how many entries there are in the pod. There may be an incompatibility between tablepress and this plugin.

    The shortcode seems to retrieve the number of rows from the table before it is filled by the “Pods tables” extension hence returning the number of the “virgin” table and not the table that is displayed on the page….

    Unfortunately because of this “bug”/error/issue, I cannot try what you suggested…

    • This reply was modified 2 years, 11 months ago by wpusr007.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    ok, I see. That indeed explains this behavior. I’m not familiar with Pods or that extension, but I assume that it is only modifying the table data on the fly, but not the returned data from the [table-info] Shortcode.
    So, what you would need is a way (other Shortcode or PHP code) to get the number of rows that Pods will be adding to the table. Then, we could use that inside a formula (via a Shortcode wrapper).

    Regards,
    Tobias

    Thread Starter wpusr007

    (@wpusr007)

    @tobiasbg : The following PHP snippet, converted to a shortcode (or via Insert PHP Code Snippet plugin which creates a shortcode for a PHP snippet) will allow to retrieve the number of posts in a specific Pods and can be used inside of a tablepress table and allow to do a dynamic SUM in the table!

    <?php
    $count_posts = wp_count_posts( 'pod_name' )->publish;
    echo $count_posts;
    ?>

    I am using your “DataTables Buttons” plugin (EXCELLENT!!!) and when exporting the table to a PDF file, I am wondering if it is possible to do the following:

    -Print the table description and name BEFORE the table?
    -Print a text footer AFTER the table?
    -Insert a shortcode (or something else) to print the export date and time (something like “Printed by Tobias on Dec. 28, 2021 at 04:06”)?

    • This reply was modified 2 years, 10 months ago by wpusr007.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    awesome! Great to hear that you found a solution to get that number! ??

    As for the PDF button: Unfortunately, I’m not aware of an easy solution here. It’s not trivial to configure the external JS library that’s creating the PDF, and I don’t have experience with it. You might be able to find more details at https://datatables.net/reference/button/pdf
    There are a few options that might be working, and which can be added via the “Custom Commands” text field of a table.

    Regards,
    Tobias

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dynamic summing of all rows’ is closed to new replies.