Summing extra column(s) with script

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
highend
Posts: 14566
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Summing extra column(s) with script

Post by highend »

Btw, a slightly modified version...
It lets you exclude columns in the dialog which to sum up so you have less clutter there...
You can extend that list by adding one entry for each column name

Code: Select all

    // Exclude columns (language specific!)
    $excludeColumns = <<<>>>
        Name
        Created
        Modified
        Accessed
    >>>;
    $excludeColumns = regexreplace($excludeColumns, "^[ \t]+");
    $excludeColumns = "(" . replace($excludeColumns, <crlf>, "|") . ")";


    // Main script
    $columnHeaders = setcolumns(, 64); // ","-separated
    // E.g.: Name,Price $,Sold $,Modified,Created

    $visibleColumns = regexmatches(columnlayout(, "get"), "\+[^.]+");
    // E.g.: +Name|+Extra 1|+Extra 2|+Modified|+Created

    // Make sure both counts are equal, otherwise a "," is used as a caption in a column
    // $columnHeaders have "readable" names
    $cntColumnHeaders  = gettoken($columnHeaders, "count", ",");
    $cntVisibleColumns = gettoken($visibleColumns, "count", "|");
    end ($cntColumnHeaders != $cntVisibleColumns), "A ',' is not allowed in column header(s), aborted!";

    // Remove unwanted columns
    if ($excludeColumns != "()") {
        $columnHeaders = regexreplace($columnHeaders, $excludeColumns);
        $columnHeaders = trim(regexreplace($columnHeaders, ",{2,}", ","), ",");
    }

    $chosenColumns = inputselect("Select column(s) to sum up", $columnHeaders, ",", 2+8192+16384, , 300, 400);
    end !($chosenColumns), "No column(s) chosen, aborted!";

    // Create report
    $selected = <get SelectedItemsNames>;
    $itemList = ($selected) ? 1 : 0;
    $report   = report("|", $itemList);
    // E.g.: 1|deep cool fans|$30,00||21.09.2023 14:15:03|21.09.2023 14:15:03
    //       2|dewalt - drill 12v|$25,00|$20,00|21.09.2023 14:15:09|21.09.2023 14:15:09
    //       3|dewalt - drill 20v|$100,00|$90,00|21.09.2023 14:15:21|21.09.2023 14:15:21
    //       4|tripod||$10,50|21.09.2023 14:15:36|21.09.2023 14:15:36

    // Remove line numbers if visible
    $report = get("#406") ? regexreplace($report, "^\d+\|") : $report;

    // Get the digits of the columns to autosum
    $chosenDigitColumns = "";
    foreach($column, $chosenColumns, ",", "e") {
        $chosenDigitColumns .= gettokenindex($column, $columnHeaders, ",", "") . "|";
    }
    $chosenDigitColumns = trim($chosenDigitColumns, "|");

    // Sum up
    $log = "";
    foreach($num, $chosenDigitColumns, "|", "e") {
        // Create and initialize count variables
        $var  = '$col_' . $num;
        *$var = 0;

        // Get the real column name
        $realColumn = gettoken($columnHeaders, $num, ",", "t");

        status "Calculating " . quote($realColumn) . " ...", , "progress";
        foreach($line, $report, <crlf>, "e") {
            $value  = gettoken($line, $num, "|", "t");
            $value  = regexmatches($value, "[0-9,.]+");
            *$var  += $value;
        }
        // Create log
        $log .= "Sum [" . $realColumn . "]: " . *$var . <crlf>;
    }
    end !($log), "No log created?";

    text $log;
One of my scripts helped you out? Please donate via Paypal

Schuller
Posts: 184
Joined: 02 May 2023 21:08

Re: Summing extra column(s) with script

Post by Schuller »

Tested and works, thanks!
For sure some column shown in dialog are definitely not necessary to show.

Post Reply