1

Does any way to add formula into table using aspose word java API. I writen a code to generate aggregate row but I search related for formula content control.

I found API FieldFormula, this field API. Can we add field into Cell?

  private void prepareAggregateRow(Table table, Row headerRow, Map<String, WsAttribute> attrValueMap, List<Attribute> attrDefs) throws Exception{
        Row totalRow = (Row)headerRow.deepClone(true);
        for (Attribute attribute : attrDefs) {
            WsAttribute wsAttribute = attrValueMap.get(attribute.getName());
            Cell cell = totalRow.getCells().get(attribute.getIdx());
            Run run = (Run)cell.getChild(NodeType.RUN, 0,true);
            if(wsAttribute!=null && wsAttribute.getValue()!=null) {
                run.setText(wsAttribute.getValue());
            } else {
                run.setText("");
            }
        }
        table.appendChild(totalRow);
    }
bNd
  • 7,512
  • 7
  • 39
  • 72
  • It seems to be Aspose.Words issue not Aspose.Cells. Please remove Aspose.Cells tag and add Aspose.Words tag so that Aspose.Words community could help you resolve this issue. Thank you. – shakeel Aug 11 '16 at 07:57

1 Answers1

1

FieldBuilder class builds a field from field code tokens (arguments and switches). Following code example inserts the FieldFormula in table's cell. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Cell cell = table.getFirstRow().getFirstCell();
builder.moveTo(cell.getFirstParagraph());

FieldBuilder fbuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
fbuilder.addArgument("20000").addSwitch("\\# \"#,##0\"").buildAndInsert(builder.getCurrentParagraph());

doc.updateFields();
doc.save(MyDir + "Out.docx");

I work with Aspose as Developer evangelist.

Tahir Manzoor
  • 597
  • 2
  • 9
  • Thanks for reply. It was done similar to this. There is other way to add field code into `builder.insertField` and define formula for same. – bNd Aug 12 '16 at 06:47