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
:
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
...
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
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');
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.
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
Copy link to clipboard
Copied