org.ttzero.excel.entity.e3.IndexParser Maven / Gradle / Ivy
/*
* Copyright (c) 2019-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;
/**
* 5.59 INDEX
*
* This record stores the range of used rows and stream
* positions of several records of the current sheet.
* In particular the position of the first DEFINEDNAME
* record and XF record is stored (BIFF2-BIFF4) and the
* position of a specific record in each Row Block
* (the first ROW record in BIFF2-BIFF4, and the DBCELL
* record in BIFF5-BIFF8).
*
* This stream position array also contains stream offsets
* to empty Row Blocks, they will point to the next extant
* Row Block.
*
* The number of entries nm in this array can be calculated
* from the row range given in this record (rf is the index
* to the first used row, rl is the index to
* the first row of unused tail of sheet):
* {@code nm = (rl – rf – 1) / 32 + 1} (using integer division).
*
* @author guanquan.wang at 2019-03-02 15:09
*/
public class IndexParser {
public static Index get(Block block) {
block.ready();
// 0 4 Not used
block.skip(4);
Index index = new Index();
// 4 4 Index to first used row (rf, 0-based)
index.rf = block.nextInt();
// 8 4 Index to first row of unused tail of sheet (rl, last used row + 1, 0-based)
index.rl = block.nextInt();
// 12 4Absolute stream position of the DEFCOLWIDTH record (➜5.32) of the current sheet.
// If this record does not exist, the offset points to the record at the position where the
// DEFCOLWIDTH record would occur
index.defColWidthPosition = block.nextInt();
// 16 4∙nm Array of nm absolute stream positions to the DBCELL record (➜5.29) of each Row Block
int nm = ((index.rl - index.rf - 1) >> 5) + 1;
index.nm = nm;
int[] nms = new int[nm];
for (int i = 0; i < nm; i++) {
nms[i] = block.nextInt();
}
index.nms = nms;
block.commit();
return index;
}
public static short getId() {
return ParserIdentifier.INDEX;
}
}