bNd
bNd

Reputation: 7640

How to add FieldFormula into Cell using aspose word in java

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);
    }

Upvotes: 1

Views: 387

Answers (1)

Tahir Manzoor
Tahir Manzoor

Reputation: 597

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.

Upvotes: 1

Related Questions