Skip to main content
M.Hasanin
Inspiring
December 8, 2021
Answered

How to Make Sum to Each Column Seperatley?

  • December 8, 2021
  • 1 reply
  • 1088 views

Hi Experts, I Need your generouse help for learning purpose, How to Make Sum to Each Column Seperatley? 

var myNumbers = app.selection[0].contents;
var mySum = 0;

if (app.selection[0].constructor.name != 'Cell') exit();
for (var i = 0; i < myNumbers.length-1; i++) {
mySum += Number (myNumbers[i]);
}
app.selection[0].cells[-1].contents = String (mySum);

in the above code it just summing the selection of all columns but i need it to be seperated like this picture 

:

 

This topic has been closed for replies.
Correct answer m1b

A nested loop is a loop that happens inside another loop. Each loop must have its own variable (i for first loop and j for second loop are common var names, but I've used c and r for column and row). At a quick glance, this nested loops tutorial looks promising. - Mark

1 reply

m1b
Community Expert
Community Expert
December 8, 2021

Hi @M.Hasanin, you need to get the columns property. Have a look at the function I've written below, and you'll see that I first check if the selection has columns, then I get the columns, then I get the cells of each column and add them up, and put the total in the last cell of the column, even if that cell wasn't selected, and will overwrite the value currently there! The script will ignore non-numbers.

- Mark

 

 

 

// by m1b
// here https://community.adobe.com/t5/indesign-discussions/how-to-make-sum-to-each-column-seperatley/m-p/12579469

function main() {

    sumSelectedTableColumns();

    function sumSelectedTableColumns() {
        var s = app.activeDocument.selection[0];
        if (!s.hasOwnProperty('columns')) return;
        var columns = app.activeDocument.selection[0].columns;

        // iterate over columns of selection
        for (var c = 0; c < columns.length; c++) {
            var columnTotal = 0,
                cells = columns[c].cells;

            // iterate over cells of column
            for (var r = 0; r < cells.length; r++) {
                var cell = cells[r];
                if (r == cells.length - 1) {
                    // last row of selected column - put total
                    cell.contents = String(columnTotal);
                } else {
                    var n = Number(cell.contents);
                    if (n == n /* check if NaN */)
                        columnTotal += n;
                }
            }

        }

    } // end sumSelectedTableColumns

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Sum Table Columns');

 

 

M.Hasanin
M.HasaninAuthor
Inspiring
December 8, 2021

Thanks a lot @m1b , Thats Great and Cool, actually I know the property of column but all the examples i saw was only one loop of columns for such changing width or other properties, my main problem now is im struggling to understand nested loops, please can you give me a name of book or website that has examples of nested loops so i can practice more in those kind of loops and thanks a lot again.

BestMohammad Hasanin
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 8, 2021

A nested loop is a loop that happens inside another loop. Each loop must have its own variable (i for first loop and j for second loop are common var names, but I've used c and r for column and row). At a quick glance, this nested loops tutorial looks promising. - Mark