• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

How to Make Sum to Each Column Seperatley?

Enthusiast ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

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 

:Summing Columns Seperatley.jpg

 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

415

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 08, 2021 Dec 08, 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-s
...

Votes

Translate

Translate
Community Expert , Dec 08, 2021 Dec 08, 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

Votes

Translate

Translate
Community Expert ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

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');

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

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.

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot @m1b , Have a Great Day

 

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines