All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ttzero.excel.entity.e3.MergedSheetSubStream Maven / Gradle / Ivy

/*
 * Copyright (c) 2017-2020, [email protected] All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.ttzero.excel.entity.e3;


import org.ttzero.excel.reader.BIFF8Row;
import org.ttzero.excel.reader.Dimension;
import org.ttzero.excel.reader.Grid;
import org.ttzero.excel.reader.GridFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author guanquan.wang at 2020-04-08 11:57
 */
public class MergedSheetSubStream extends SheetSubStream {
    // A merge cells grid
    protected Grid mergeCells;
    protected List list;
    protected Block privateBlock;
    protected boolean hasMergedCell;

    public MergedSheetSubStream(GlobalsSetting gs, SheetInfo sheet) {
        super(gs, sheet);
        list = new ArrayList<>();
    }

    public static MergedSheetSubStream of(SheetSubStream other) {
        MergedSheetSubStream stream = new MergedSheetSubStream(other.gs, other.sheet);
        stream.sheet = other.sheet;

        stream.index = other.index;
        stream.range = other.range;
        stream.block = other.block;
        stream.rowBlock = other.rowBlock;
        stream.firstRow = other.firstRow;
        stream.eof = other.eof;
        stream.rowStartIndex = other.rowStartIndex;
        stream.artDgContainer = other.artDgContainer;
        stream.rowSupplier = other.rowSupplier;
        // Look up Merged cells
        stream.loopParse();

        if (!stream.hasMergedCell)
            stream.beforeParseRowBlock();

        return stream;
    }

    @Override
    public void get(Block block) {
        super.get(block);

        // Merged
        if (!eof && hasMergedCell && firstRow != null) {
            for (int i = firstRow.getFirstColumnIndex(); i < firstRow.getLastColumnIndex(); i++)
                mergeCells.merge(firstRow.getRowNum(), firstRow.getCell(i));
        }
    }

    /**
     * Parse whitelist record
     *
     * @param id the record id
     * @param block block data
     * @return true if break parse
     */
    @Override
    protected boolean parseWhiteListRecord(int id, Block block) {
        switch (id) {
            // 4.7 Cell Table and Row Blocks
            case ParserIdentifier.INDEX:
                index = IndexParser.get(block);
                break;
            // 5.35 DIMENSION
            case ParserIdentifier.DIMENSION:
                block.ready();
                // Index to first used row
                int fr = block.nextInt() + 1;
                // Index to last used row, increased by 1
                int lr = block.nextInt();
                // Index to first used column
                short fc = (short) (block.nextShort() + 1);
                // Index to last used column, increased by 1
                short lc = block.nextShort();
                range = new Dimension(fr, fc, lr, lc);
                block.commit();
                break;
            // 4.7 Cell Table and Row Blocks
            case ParserIdentifier.ROW:
                if (privateBlock == null) {
                    this.privateBlock = block.deepClone();
                    this.privateBlock.cacheIdentifier();
                }
                IgnoreParser.get(block);
                break;
            // 5.67 MERGEDCELLS
            case ParserIdentifier.MERGEDCELLS:
                list.addAll(Arrays.asList(MergedCellsParser.get(block)));
                break;
            default:
                IgnoreParser.get(block);
        }
        return false;
    }

    /**
     * Do something before parse RowBlock
     *
     * @return boolean value, abort if returns false
     */
    @Override
    protected boolean beforeParseRowBlock() {
        if (!list.isEmpty()) {
            mergeCells = GridFactory.create(list);
            LOGGER.debug("Grid: Size: {} {}", this.mergeCells.size(), this.mergeCells.getClass());
            hasMergedCell = true;
            list = null; // Free
        }
        this.block = privateBlock;
        if (this.block != null)
            this.block.getContext().sectorTable.moveTo(this.block.currentSecID());
        return super.beforeParseRowBlock();
    }

    /**
     * iterator rows
     *
     * @return Row
     */
    @Override
    public BIFF8Row nextRow() {
        BIFF8Row row = super.nextRow();
        if (hasMergedCell && row != null) {
            for (int i = row.getFirstColumnIndex(); i < row.getLastColumnIndex(); i++)
                mergeCells.merge(row.getRowNum(), row.getCell(i));
        }
        return row;
    }

    public Grid getMergeCells() {
        return mergeCells;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy