We are using v8.0 of Aspose.Words.
When our document has a table in it and at the bottom of the table contains the following field
{
= SUM(ABOVE)\ # "$#,##0.00;($#,##0.00)"
}
when the document is streamed to disk / end user so that field is displayed as
!Undefined Bookmark, ABOVE
The functionallity is still there when we hit F9 - it will sum all the cells above it.
In version 6.5 it would keep the display as $0.00.
I’ve used the following code to reproduce it:
var streamFromManifest = typeof(SimpleDocumentAssemblerFixture).Assembly.GetManifestResourceStream("PRS.CMS.IntegrationTests.MergeEngine.Server." + "TestSumField.dotx");
// Open an existing document.
var doc = new Document(streamFromManifest);
// Fill the fields in the document with user data.
doc.MailMerge.Execute(new []
{
"FullName",
"Company",
"Address",
"Address2",
"City"
},
new object[]
{
"James Bond",
"MI5 Headquarters",
"Milbank",
"",
"London"
});
// doc.UpdateFields();
doc.Save("TestSumField - ok.doc", SaveFormat.Doc);
I’ve included 4 files in the files.zip attachment.
1 - TestSumField.dotx: that is the template used to generate the document
2 - TestSumField - ok -v6.5.doc: that is the output of the code above when using v6.5
3 - TestSumField - ok -v8.0.doc: that is the output of the code above when using v8.0
4 - TestSumField - ok -v8.0-update fields.doc: that is the output of the code above when using v8.0 and uncommenting the //doc.UpdateFields line.
Thanks for your request. Unfortunately, table cell references like ABOVE are not currently supported. This feature will be supported in a month or two. Your request has been linked to the appropriate issue. You will be notified as soon as this feature is available.
You can also try a workaround as described in the following forum thread:
Thanks for your request. Sorry, I missed that the thread is private. Here is code example and documents provided there.
// Open document.
Document doc = new Document(@"Test001\in.doc");
// We will use DocuemntBuider to insert new value of the field
DocumentBuilder buider = new DocumentBuilder(doc);
// Get collection of FieldStart nodes in the document.
// This is needed to fing formula fields.
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
// Loop through all FieldStarts.
foreach(FieldStart start in starts)
{
// Check type of teh field.
// We will process only formula like =SUM(ABOVE).
if (start.FieldType == FieldType.FieldFormula)
{
// We need also get field code.
string fieldCode = "";
Node currentNode = start;
while (currentNode.NodeType != NodeType.FieldSeparator)
{
if (currentNode.NodeType == NodeType.Run)
fieldCode += ((Run) currentNode).Text;
currentNode = currentNode.NextSibling;
}
// Check if the current node is =SUM(ABOVE).
if (fieldCode.Trim() != "=SUM(ABOVE)")
continue;
// Calculate value of the field.
// This field should be placed in the table cell.
Cell cell = (Cell) start.GetAncestor(NodeType.Cell);
// If this field is placed outside table cell, we will stop calculation.
if (cell == null)
continue;
// Get index of the cell.
int cellIdx = cell.ParentRow.Cells.IndexOf(cell);
int sum = 0;
// Loop through all rows before row, where field is located.
Table table = cell.ParentRow.ParentTable;
for (Row row = table.FirstRow; row != null && row != cell.ParentRow; row = (Row) row.NextSibling)
{
// Get cell text by index.
string text = row.Cells[cellIdx].ToTxt().Trim();
// Convert value to int.
int val;
Int32.TryParse(text, out val);
// add value to the sum
sum += val;
}
// Now we should insert calculated value as a field result.
// We shoudl remove old value first.
currentNode = currentNode.NextSibling;
while (currentNode.NodeType != NodeType.FieldEnd)
{
Node nextNode = currentNode.NextSibling;
currentNode.Remove();
currentNode = nextNode;
}
// Insert new value.
buider.MoveTo(currentNode);
buider.Write(sum.ToString());
}
}
// Save output document.
doc.Save(@"Test001\out.doc");