Search in sources :

Example 16 with KCell

use of org.pentaho.di.core.spreadsheet.KCell in project pentaho-kettle by pentaho.

the class StaxPoiSheetTest method testReadData.

@Test
public void testReadData() throws Exception {
    KSheet sheet1 = getSampleSheet();
    assertEquals(5, sheet1.getRows());
    KCell[] row = sheet1.getRow(2);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("One", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283817600000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("75"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.TRUE, row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("75"), row[5].getValue());
    row = sheet1.getRow(3);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Two", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283904000000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("42"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.FALSE, row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("117"), row[5].getValue());
    row = sheet1.getRow(4);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Three", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283990400000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("93"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.TRUE, row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("210"), row[5].getValue());
    try {
        row = sheet1.getRow(5);
        fail("No out of bounds exception thrown when expected");
    } catch (ArrayIndexOutOfBoundsException e) {
    // OK!
    }
}
Also used : KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date) Test(org.junit.Test)

Example 17 with KCell

use of org.pentaho.di.core.spreadsheet.KCell in project pentaho-kettle by pentaho.

the class StaxPoiSheetTest method testNullDateCell.

@Test
public void testNullDateCell() throws Exception {
    // cell had null value instead of being null
    final String sheetId = "1";
    final String sheetName = "Sheet 1";
    XSSFReader reader = mockXSSFReader(sheetId, SHEET_DATE_NO_V, mockSharedStringsTable("Some Date"), mockStylesTable(Collections.singletonMap(2, 165), Collections.singletonMap(165, "M/D/YYYY")));
    StaxPoiSheet spSheet = spy(new StaxPoiSheet(reader, sheetName, sheetId));
    doReturn(true).when(spSheet).isDateCell(any());
    KCell cell = spSheet.getRow(1)[0];
    assertNotNull(cell);
    assertEquals(KCellType.DATE, cell.getType());
    cell = spSheet.getRow(2)[0];
    assertNull("cell must be null", cell);
}
Also used : KCell(org.pentaho.di.core.spreadsheet.KCell) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) Test(org.junit.Test)

Example 18 with KCell

use of org.pentaho.di.core.spreadsheet.KCell in project pentaho-kettle by pentaho.

the class ExcelInputDialogTest method getFieldsTest.

@Test
public /**
 * http://jira.pentaho.com/browse/PDI-13930
 */
void getFieldsTest() throws Exception {
    ExcelInputDialog dialog = Mockito.mock(ExcelInputDialog.class);
    RowMeta fields = new RowMeta();
    ExcelInputMeta info = Mockito.mock(ExcelInputMeta.class);
    Mockito.doReturn(true).when(info).readAllSheets();
    int[] startColumn = { 0 };
    Mockito.doReturn(startColumn).when(info).getStartColumn();
    int[] startRow = { 0 };
    Mockito.doReturn(startRow).when(info).getStartRow();
    KWorkbook workbook = Mockito.mock(KWorkbook.class);
    Mockito.doReturn(1).when(workbook).getNumberOfSheets();
    KSheet sheet = Mockito.mock(KSheet.class);
    Mockito.doReturn(sheet).when(workbook).getSheet(0);
    KCell cell = Mockito.mock(KCell.class);
    int fieldCount = 400;
    for (int i = 0; i <= fieldCount - 1; i++) {
        Mockito.doReturn(cell).when(sheet).getCell(i, 0);
        Mockito.doReturn(cell).when(sheet).getCell(i, 1);
    }
    Mockito.doReturn("testValue").when(cell).getContents();
    Mockito.doReturn(KCellType.NUMBER).when(cell).getType();
    PluginRegistry pluginRegistry = Mockito.mock(PluginRegistry.class);
    PluginInterface stringPlugin = Mockito.mock(PluginInterface.class);
    Mockito.doReturn(stringPlugin).when(pluginRegistry).getPlugin(ValueMetaPluginType.class, "1");
    Mockito.doReturn(Mockito.mock(ValueMetaInterface.class)).when(pluginRegistry).loadClass(stringPlugin, ValueMetaInterface.class);
    ValueMetaFactory.pluginRegistry = pluginRegistry;
    Method processingWorkbookMethod = ExcelInputDialog.class.getDeclaredMethod("processingWorkbook", RowMetaInterface.class, ExcelInputMeta.class, KWorkbook.class);
    processingWorkbookMethod.setAccessible(true);
    processingWorkbookMethod.invoke(dialog, fields, info, workbook);
    Assert.assertEquals(fieldCount, fields.size());
}
Also used : ExcelInputMeta(org.pentaho.di.trans.steps.excelinput.ExcelInputMeta) RowMeta(org.pentaho.di.core.row.RowMeta) KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) PluginInterface(org.pentaho.di.core.plugins.PluginInterface) KSheet(org.pentaho.di.core.spreadsheet.KSheet) Method(java.lang.reflect.Method) KCell(org.pentaho.di.core.spreadsheet.KCell) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 19 with KCell

use of org.pentaho.di.core.spreadsheet.KCell in project pentaho-kettle by pentaho.

the class ExcelInputDialog method processingWorkbook.

/**
 * Processing excel workbook, filling fields
 *
 * @param fields RowMetaInterface for filling fields
 * @param info ExcelInputMeta
 * @param workbook excel workbook for processing
 * @throws KettlePluginException
 */
private void processingWorkbook(RowMetaInterface fields, ExcelInputMeta info, KWorkbook workbook) throws KettlePluginException {
    int nrSheets = workbook.getNumberOfSheets();
    for (int j = 0; j < nrSheets; j++) {
        KSheet sheet = workbook.getSheet(j);
        // See if it's a selected sheet:
        int sheetIndex;
        if (info.readAllSheets()) {
            sheetIndex = 0;
        } else {
            sheetIndex = Const.indexOfString(sheet.getName(), info.getSheetName());
        }
        if (sheetIndex >= 0) {
            // We suppose it's the complete range we're looking for...
            // 
            int rownr = 0;
            int startcol = 0;
            if (info.readAllSheets()) {
                if (info.getStartColumn().length == 1) {
                    startcol = info.getStartColumn()[0];
                }
                if (info.getStartRow().length == 1) {
                    rownr = info.getStartRow()[0];
                }
            } else {
                rownr = info.getStartRow()[sheetIndex];
                startcol = info.getStartColumn()[sheetIndex];
            }
            boolean stop = false;
            for (int colnr = startcol; !stop; colnr++) {
                try {
                    String fieldname = null;
                    int fieldtype = ValueMetaInterface.TYPE_NONE;
                    KCell cell = sheet.getCell(colnr, rownr);
                    if (cell == null) {
                        stop = true;
                    } else {
                        if (cell.getType() != KCellType.EMPTY) {
                            // We found a field.
                            fieldname = cell.getContents();
                        }
                        // System.out.println("Fieldname = "+fieldname);
                        KCell below = sheet.getCell(colnr, rownr + 1);
                        if (below != null) {
                            if (below.getType() == KCellType.BOOLEAN) {
                                fieldtype = ValueMetaInterface.TYPE_BOOLEAN;
                            } else if (below.getType() == KCellType.DATE) {
                                fieldtype = ValueMetaInterface.TYPE_DATE;
                            } else if (below.getType() == KCellType.LABEL) {
                                fieldtype = ValueMetaInterface.TYPE_STRING;
                            } else if (below.getType() == KCellType.NUMBER) {
                                fieldtype = ValueMetaInterface.TYPE_NUMBER;
                            } else {
                                fieldtype = ValueMetaInterface.TYPE_STRING;
                            }
                        } else {
                            fieldtype = ValueMetaInterface.TYPE_STRING;
                        }
                        if (Utils.isEmpty(fieldname)) {
                            stop = true;
                        } else {
                            if (fieldtype != ValueMetaInterface.TYPE_NONE) {
                                ValueMetaInterface field = ValueMetaFactory.createValueMeta(fieldname, fieldtype);
                                fields.addValueMeta(field);
                            }
                        }
                    }
                } catch (ArrayIndexOutOfBoundsException aioobe) {
                    // System.out.println("index out of bounds at column "+colnr+" : "+aioobe.toString());
                    stop = true;
                }
            }
        }
    }
}
Also used : KSheet(org.pentaho.di.core.spreadsheet.KSheet) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KCell(org.pentaho.di.core.spreadsheet.KCell) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 20 with KCell

use of org.pentaho.di.core.spreadsheet.KCell in project pentaho-kettle by pentaho.

the class PoiWorkBookIT method readData.

private void readData() throws KettleException {
    KWorkbook workbook = WorkbookFactory.getWorkbook(SpreadSheetType.POI, "src/it/resources/sample-file.xlsx", null);
    int numberOfSheets = workbook.getNumberOfSheets();
    assertEquals(3, numberOfSheets);
    KSheet sheet1 = workbook.getSheet(0);
    assertEquals("Sheet1", sheet1.getName());
    sheet1 = workbook.getSheet("Sheet1");
    assertEquals("Sheet1", sheet1.getName());
    assertEquals(5, sheet1.getRows());
    KCell[] row = sheet1.getRow(2);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("One", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283817600000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("75"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("75"), row[5].getValue());
    row = sheet1.getRow(3);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Two", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283904000000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("42"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(false), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("117"), row[5].getValue());
    row = sheet1.getRow(4);
    assertEquals(KCellType.LABEL, row[1].getType());
    assertEquals("Three", row[1].getValue());
    assertEquals(KCellType.DATE, row[2].getType());
    assertEquals(new Date(1283990400000L), row[2].getValue());
    assertEquals(KCellType.NUMBER, row[3].getType());
    assertEquals(Double.valueOf("93"), row[3].getValue());
    assertEquals(KCellType.BOOLEAN, row[4].getType());
    assertEquals(Boolean.valueOf(true), row[4].getValue());
    assertEquals(KCellType.NUMBER_FORMULA, row[5].getType());
    assertEquals(Double.valueOf("210"), row[5].getValue());
    try {
        sheet1.getRow(5);
        fail("No out of bounds exception thrown when expected");
    } catch (ArrayIndexOutOfBoundsException e) {
    // OK!
    }
    workbook.close();
}
Also used : KWorkbook(org.pentaho.di.core.spreadsheet.KWorkbook) KSheet(org.pentaho.di.core.spreadsheet.KSheet) KCell(org.pentaho.di.core.spreadsheet.KCell) Date(java.util.Date)

Aggregations

KCell (org.pentaho.di.core.spreadsheet.KCell)23 KSheet (org.pentaho.di.core.spreadsheet.KSheet)16 Test (org.junit.Test)12 Date (java.util.Date)9 KWorkbook (org.pentaho.di.core.spreadsheet.KWorkbook)9 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)3 IOException (java.io.IOException)2 FileObject (org.apache.commons.vfs2.FileObject)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)2 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)2 KettleException (org.pentaho.di.core.exception.KettleException)2 KCellType (org.pentaho.di.core.spreadsheet.KCellType)2 FileInputStream (java.io.FileInputStream)1 Method (java.lang.reflect.Method)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)1 StylesTable (org.apache.poi.xssf.model.StylesTable)1