package com.smtscript.lib.jsexcel; import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.NativeObject; import com.smtscript.lib.JSColLineReader; import com.smtscript.utils.SMTStatic; import jxl.Sheet; public class JSExcelReaderSheet implements JSColLineReader { protected Sheet _sheet; protected JSExcelReader _parentReader; public JSExcelReaderSheet(Sheet sheet, JSExcelReader parentReader) { _sheet = sheet; _parentReader = parentReader; } public JSExcelReader __parentReader__() { return _parentReader; } public String getName() { return _sheet.getName(); } public int getColCount() { return _sheet.getColumns(); } public int getRowCount() { return _sheet.getRows(); } public String readCellValue(int col, int row) { return _sheet.getCell(col, row).getContents(); } public NativeObject readCellInfo(int col, int row) { return null; } public String[] __readColValues__(int col, int row, int count) { if(col >= _sheet.getColumns()) return null; int maxCount = _sheet.getRows(); if(count < 0) count = maxCount - row; if(count < 1) return null; String[] values = new String[count]; for(int i = 0; i < count; i ++) { int curRow = i + row; values[i] = curRow < maxCount ? _sheet.getCell(col, curRow).getContents() : ""; } return values; } public NativeArray readColValues(int col, int row, int count) { String[] values = __readColValues__(col, row, count); if(values == null) return null; return new NativeArray(values); } public NativeArray readColValues(int col) { return readColValues(col, 0, -1); } public NativeObject readColMap(int col, int row, NativeArray rowNames) { String[] values = __readColValues__(col, row, rowNames.size()); if(values == null) return null; NativeObject map = new NativeObject(); for(int i = 0; i < rowNames.size(); i ++) { String name = (String) rowNames.get(i); if(SMTStatic.isNullOrEmpty(name)) continue; map.put(name, map, values[i]); } return map; } public NativeObject readColMap(int col, NativeArray rowNames) { return readColMap(col, 0, rowNames); } public String[] __readRowValues__(int row, int col, int count) { if(row >= _sheet.getRows()) return null; int maxCount = _sheet.getColumns(); if(count < 0) count = maxCount - col; if(count < 1) return null; String[] values = new String[count]; for(int i = 0; i < count; i ++) { int curCol = i + col; values[i] = curCol < maxCount ? _sheet.getCell(i + col, row).getContents() : ""; } return values; } public NativeArray readRowValues(int row, int col, int count) { String[] values = __readRowValues__(row, col, count); if(values == null) return null; return new NativeArray(values); } public NativeArray readColNames(int row) { String[] values = __readRowValues__(row, 0, -1); if(values == null) return null; for(int i = 0; i < values.length; i ++) { values[i] = values[i].replace("\r", "").replace("\n", "").replace(" ", "").replace("\t", "").trim(); } return new NativeArray(values); } public NativeArray readRowValues(int row) { return readRowValues(row, 0, -1); } public NativeObject readRowMap(int row, NativeArray colNames) { return readRowMap(row, 0, colNames); } public NativeObject readRowMap(int row, int col, NativeArray colNames) { String[] values = __readRowValues__(row, col, colNames.size()); if(values == null) return null; NativeObject map = new NativeObject(); for(int i = 0; i < colNames.size(); i ++) { String name = (String) colNames.get(i); if(SMTStatic.isNullOrEmpty(name)) continue; map.put(name, map, values[i]); } return map; } }