Edit 2024-11-19: changed script because my original script did not work with OP's demo document. The following is updated answer:
Hi @alma_3660 I have updated the script to work with the demo document you gave us. I hope it will be useful now.
To use it, make a selection of table cells (one row or many rows) and run the script. It will put the total for each row's cells into either the first or last cell on each row (you can adjust this in the script).

Let me know how it goes.
- Mark
/**
* @file Sum Table Rows.js
*
* Usage:
* - select some table cells
* - script will sum the selected cells and put result into
* either the first or last cell of each row (see `settings` below)
*
* @author m1b
* @version 2024-11-18
* @discussion https://community.adobe.com/t5/indesign-discussions/need-help-with-a-script/m-p/14985806
*/
function main() {
var settings = {
// setting this to 'first' or 'last' will populate
// the first or last cell in each row with the total
// of the selected cells in that row
populateFirstEmptyCellFrom: 'last',
};
if (
0 === app.documents.length
|| 0 === app.activeDocument.selection.length
)
return alert('Please select a range of table cells and try again.');
var doc = app.activeDocument,
rows = getCellsInRows(doc.selection[0]);
if (!rows)
return alert('Please select a range of table cells and try again.');
for (var r = 0; r < rows.length; r++) {
var cells = rows[r],
targetCell = undefined,
total = 0,
count = 0;
if ('first' === settings.populateFirstEmptyCellFrom)
targetCell = cells[0];
else if ('last' === settings.populateFirstEmptyCellFrom)
targetCell = cells[cells.length - 1];
// add up the cells' contents
for (var c = 0; c < cells.length; c++)
sumCell(cells[c]);
if (count)
// if any cells had numbers, set cell's contents to the total
targetCell.contents = String(total);
}
/**
* Adds a cell's number to `total`.
* @param {Cell} cell - a Table Cell.
*/
function sumCell(cell) {
if (targetCell === cell)
return;
var n = Number(cell.contents);
if (!n || isNaN(n))
return;
total += n;
count++;
};
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Sum Cells');
/**
* Returns array of arrays of cells by row.
* Useful when given a selection of cells
* to divide into rows.
* @author m1b
* @version 2024-11-19
* @param {Cell} cells - the cells to separate.
* @returns {Array<Array<Cell>>?}
*/
function getCellsInRows(cells) {
if (
undefined == cells
|| 'Cell' !== cells.constructor.name
|| !cells.hasOwnProperty('cells')
|| cells.cells.length < 2
)
return
var cellsInRows = [],
lookup = {};
for (var i = 0; i < cells.cells.length; i++) {
var rowIndex = cells.cells[i].parentRow.index;
if (undefined == lookup[rowIndex])
lookup[rowIndex] = cellsInRows.length;
if (undefined == cellsInRows[lookup[rowIndex]])
cellsInRows[lookup[rowIndex]] = [];
cellsInRows[lookup[rowIndex]].push(cells.cells[i]);
}
return cellsInRows;
};