se.minstrel.tools.xssfbuilder.impl.FormulaBuilderImpl.java Source code

Java tutorial

Introduction

Here is the source code for se.minstrel.tools.xssfbuilder.impl.FormulaBuilderImpl.java

Source

/*
 * XSSFBuilder - an API making work with poi spreadsheets more time efficient.
 * (C) 2013, Magnus Drougge <magnus.drougge@gmail.com>
 *
 * This file is part of XSSFBuilder.
 *
 * XSSFBuilder is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XSSFBuilder is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XSSFBuilder.  If not, see <http://www.gnu.org/licenses/>.
 */
package se.minstrel.tools.xssfbuilder.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;

import se.minstrel.tools.xssfbuilder.FormulaBuilder;
import se.minstrel.tools.xssfbuilder.markers.Marker;

public class FormulaBuilderImpl implements FormulaBuilder {

    private Support support;

    public FormulaBuilderImpl(Support support) {
        this.support = support;
    }

    public String sum(int rowStart, int rowEnd, int colStart, int colEnd) {
        CellRangeAddress cra = new CellRangeAddress(rowStart, rowEnd, colStart, colEnd);
        return String.format("SUM(%s)", cra.formatAsString());
    }

    public String sumMarkers(String marker) {
        List<String> refs = new ArrayList<String>();
        for (Marker m : support.getMarkerManager().get(marker)) {
            refs.add(String.format("%s!%s", m.getSheetName(),
                    new CellReference(m.getRow(), m.getCol()).formatAsString()));
        }
        StringBuilder sb = new StringBuilder("SUM(");
        Iterator<String> it = refs.iterator();
        while (it.hasNext()) {
            sb.append(it.next());
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }

}