We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table sum is not correct

@conniem,

Thanks for sharing the detail. We will inform you via this forum thread about the first issue WORDSNET-16581 once there is any update available on it.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

@conniem,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-16581) as ‘Not a Bug’.

Please use the following code example to get the desired output.

string s = Directory.GetCurrentDirectory();
Document AsposeDocument = new Document(s + "\\test.dot");

testmerge(AsposeDocument);

AsposeDocument.UpdatePageLayout();
//AsposeDocument.UpdateFields();
Table tbl = (Table)AsposeDocument.GetChild(NodeType.Table, 0, true);
Field field = tbl.Range.Fields.OfType<FieldFormula>().Single();
field.Result = string.Empty;
field.Update();
// lock field
field.IsLocked = true;
// or unlock field
//field.Unlink();

AsposeDocument.Save(s + "\\18.4.docx");

so basically, before I save a document,
I need to iterate through ALL the tables and ALL the fields of the tables, and call that code?

I know how to get all the table nodes, how do I get all the fields with formulas (the code snip only gets one)?

Also, my c# code doesn’t compile, it says

‘FieldCollection’ does not contain a definition for ‘OfType’ and no extension method ‘OfType’ accepting a first argument of type ‘FieldCollection’ could be found (are you missing a using directive or an assembly reference?)
The type or namespace name ‘FieldFormula’ could not be found (are you missing a using directive or an assembly reference?)

fields.zip (43.9 KB)
as you can see from the attached project, the solution did not work, my table is still no summing correctly.
the actual sum should be about 160.88

@conniem

Thanks for your inquiry. Please use the following code example to iterate over all tables of document and update the Sum field. Hope this helps you.

If you want to use Enumerable.OfType, you need to import System.Linq namespace in your project.

foreach (Table table in AsposeDocument.GetChildNodes(NodeType.Table, true))
{
    foreach (Field field in table.Range.Fields)
    {
        if (field.Type == FieldType.FieldFormula)
        {
            field.Result = string.Empty;
            field.Update();
            // lock field
            field.IsLocked = true;
            // or unlock field
            //field.Unlink();
        }
    }
}

see above reply.
this didn’t work, the sum is still incorrect
using 18.4

@conniem,

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the same scenario using MS Word, you will get the same output.

Moreover, the ‘SUM(ABOVE)’ field works as expected in both Aspose.Words and MS Word. It is not a bug. MS Word does aggregates all above cell, only the range below first cell with non number value.

okay.
but I need the sum to total correctly, as that is what the end user expects.
any suggestions?

@conniem,

Thanks for your inquiry. In this case, we suggest you following solution.

  1. Please iterate over the cells of last column and sum the numeric values.
  2. Insert the sum value in the desired cell of table using DocumentBuilder.Write method.

okay.

so, how can I tell which column has a sum on it, which column to sum (because sometimes, I have a total in column d, that is a sum(e:e), for example), and where to place the total?

because I am
a) not designing the templates, my end users are
b) not in control of future template design
c) trying to support 50 + sites, some with 200 + templates
d) really trying to get away from using word, because my end users complain that the totals don’t match up (among other things)

and the ONLY way I’ve been able to make them total correctly is to force the formatting into ‘-#.##’, vs the user’s desired formatting of ‘(#.##)’, and the users are not real happy with that.

your help in this matter would be greatly appreciated.

@conniem,

It is to update you that we have completed the analysis of WORDSNET-16581 feature and has come to a conclusion that we won’t be able to provide this feature. This feature has been closed with “Won’t Fix” resolution. Please note that Aspose.Words mimics the behavior of MS Word. MS Word does aggregates all above cell, only the range below first cell with non number value. Aspose.Words does the same.