• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Summing Column/all Columns in One Hit!

Enthusiast ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

Hi Experts,

I Read Old Answer in Old Thread Here :

https://community.adobe.com/t5/indesign-discussions/summing-cells-in-a-row/m-p/6220837#M296472 

and I Changed the Solution Provided to Support Summing Columns not Row as Original Post But I noticed after testing the code that it only work if One Column Selected but not like the Row Version that Support Calculating the Sums if the user select Row or Rows!, So Please Help to find how to make the code work not only with one Column but with multiple ones selected by the user and Many Thanks in Advance, Here is the Modified Code :

 

//Summing cells in one selected Column
var
  mT = app.selection[0].cells.everyItem().getElements(),
  currSum, sumCell,
  len = mT.length,
  currCell, prevCell;
while (len-->0) {
  currCell = mT[len];
  prevCell = mT[len-1];
  currSum = Number(currCell.contents);
  sumCell = prevCell.parentColumn.cells.lastItem(prevCell); 
  while (prevCell && currCell.parentColumn == prevCell.parentColumn && len-->0) {
       currSum +=  Number(prevCell.contents); //SUM
       currCell = mT[len];
       prevCell = mT[len-1];
      }
  sumCell.contents = currSum.toString();
  }

 

 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

255

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 1 Correct answer

Community Expert , Dec 03, 2021 Dec 03, 2021

Wrong implementation. Try this:

var table = app.selection[0].parent;
var sum = eval( app.selection[0].contents.join("+") );
table.rows[-1].cells[0].contents = sum.toString();

 

Result:

SumOfAllSelectedCells.PNG

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate
Community Expert ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

Hi M.Hasanain,

already answered in this other thread.

 

If you are sure that all cells contain "reasonable" contents you could add all the numbers with this:

var sumOfAllValuesInSelectedCells = eval( app.selection[0].contents.join("+") );

And this is a very big if.

 

For example this will fail in the moment that a selected cell does not contain anything at all.

Or if it happens that one of the selected cells is a graphic cell. Etc.pp.

 

Better test the contents of all selected cells with isNaN before adding anything.

Before you can do that you must convert its contents to a number.

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#global.html#d1e3846__d1e4164

 

Regards,
Uwe Laubender

( ACP )

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 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

Hi @Laubender , Thanks alot for replying, 

Actually i Modified the Code again,  your added code is good and simplify the solution to make the whole sum for one colum in one hit (for each selected column individually) but if i selecte all the columns it fails!., Please See the Screenshots here and the modified script :

Give Error of all Columns Selected and Run Script.jpgWroking Great Here.jpg

the modified script here :

 

 

 

//Summing Selected cells in one Column
var
  mT = app.selection[0].cells.everyItem().getElements(),
  currSum, sumCell,
  len = mT.length,
  currCell, prevCell;
while (len-->0) {
  currCell = mT[len];
  prevCell = mT[len-1];
  currSum = Number(currCell.contents);
  sumCell = prevCell.parentColumn.cells.lastItem(prevCell); 
  while (prevCell && currCell.parentColumn == prevCell.parentColumn && len-->0) {
  var sumOfAllValuesInSelectedCells = eval(app.selection[0].contents.join("+") );
      }
  sumCell.contents = sumOfAllValuesInSelectedCells.toString();
  }

 

 

But in the Origial Version for row it works if one row selected even multiple row selected ! :

 

//Summing Selected cells in a row or rows
var
  mT = app.selection[0].cells.everyItem().getElements(),
  currSum, sumCell,
  len = mT.length,
  currCell, prevCell;
while (len-->0) {
  currCell = mT[len];
  prevCell = mT[len-1];
  currSum = Number(currCell.contents);
  sumCell = currCell.parentRow.cells.lastItem(currCell);
  while (prevCell && currCell.parentRow == prevCell.parentRow && len-->0) {
       currSum +=  Number(prevCell.contents); //SUM
       currCell = mT[len];
       prevCell = mT[len-1];
       }
  sumCell.contents = currSum.toString();
  }

 

Here is Screenshot :

Working for Row or Rows.jpg

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 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

Wrong implementation. Try this:

var table = app.selection[0].parent;
var sum = eval( app.selection[0].contents.join("+") );
table.rows[-1].cells[0].contents = sum.toString();

 

Result:

SumOfAllSelectedCells.PNG

 

Regards,
Uwe Laubender

( ACP )

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 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot @Laubender , thats great and simple, do you think its possible to show the total sum for each column with one selection for the three columns?

I mean Like this :

each.jpg

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