]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelImport.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / ExcelImport.java
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelImport.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/ExcelImport.java
new file mode 100644 (file)
index 0000000..81aa67e
--- /dev/null
@@ -0,0 +1,591 @@
+package org.simantics.spreadsheet.graph;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.StringReader;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellStyle;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.ss.usermodel.WorkbookFactory;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;\r
+import org.apache.poi.xssf.usermodel.XSSFColor;\r
+import org.apache.poi.xssf.usermodel.XSSFFont;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.datatypes.literal.Font;\r
+import org.simantics.datatypes.literal.RGB;\r
+import org.simantics.datatypes.utils.BTree;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.DelayedWriteRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.BindingException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.indexing.DatabaseIndexing;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.service.XSupport;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.spreadsheet.graph.SpreadsheetStyle.SpreadsheetStyleBuilder;\r
+import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment;\r
+import org.simantics.spreadsheet.graph.parser.SheetFormulaParser;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstValue;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.spreadsheet.util.SpreadsheetUtils;\r
+import org.simantics.utils.DataContainer;\r
+\r
+public class ExcelImport {\r
+       \r
+       private static final double POINT_TO_PIXEL_RATIO = 1.33;\r
+       \r
+    public static void importBook(Resource container, File file) {\r
+        \r
+        try {\r
+            \r
+            DataContainer<List<BTree>> btreeContainer = new DataContainer<>();\r
+            DataContainer<Resource> bookContainer = new DataContainer<>();\r
+            \r
+            Simantics.getSession().sync(new DelayedWriteRequest() {\r
+                \r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+\r
+                    List<BTree> result = new ArrayList<>();\r
+                    try {\r
+                        FileInputStream fis = new FileInputStream(file);\r
+                        Workbook workBook = WorkbookFactory.create(fis);\r
+                        fis.close();\r
+                    \r
+                        Layer0 L0 = Layer0.getInstance(graph);\r
+                        SpreadsheetResource SR = SpreadsheetResource.getInstance(graph);\r
+\r
+                        String originalFileName = file.getName();\r
+                        int lastDot = originalFileName.lastIndexOf('.');\r
+                        String suggestion = originalFileName;\r
+                        if(lastDot!=-1)\r
+                               suggestion = originalFileName.substring(0, lastDot);\r
+                        \r
+                        String uniqueName = NameUtils.findFreshEscapedName(graph, suggestion, container);\r
+                        \r
+                        Resource book = SpreadsheetGraphUtils.createBook(graph, container, uniqueName);\r
+                        \r
+                     // Initial empty style for every book\r
+//                        Resource emptyStyle = SpreadsheetGraphUtils.createStyle(graph, book, SpreadsheetStyle.empty());\r
+                        \r
+                        Map<Integer, Resource> existingStyles = new HashMap<>();\r
+                        Map<Integer, SpreadsheetStyle> existingStyles2 = new HashMap<>();\r
+                        \r
+//                        Collection<Resource> styles = graph.syncRequest(new ObjectsWithType(book, L0.ConsistsOf, SR.Style));\r
+//                        for (Resource eStyle : styles) {\r
+//                            int styleId = graph.getRelatedValue2(eStyle, SR.Style_id, Bindings.INTEGER);\r
+//                            existingStyles.put(styleId, eStyle);\r
+//                        }\r
+                        \r
+                        for(int sheetNumber = 0; sheetNumber < workBook.getNumberOfSheets(); sheetNumber++) {\r
+\r
+                            Sheet sheet_ = workBook.getSheetAt(sheetNumber);\r
+                            String sheetName = sheet_.getSheetName();\r
+\r
+                            Resource sheet = graph.newResource();\r
+                            graph.claim(sheet, L0.InstanceOf, SR.Spreadsheet);\r
+                            graph.claimLiteral(sheet, L0.HasName, L0.NameOf, L0.String, sheetName, Bindings.STRING);\r
+                            graph.claim(book, L0.ConsistsOf, sheet);\r
+\r
+                            {\r
+                                Resource newCell = graph.newResource();\r
+                                graph.claim(newCell, L0.InstanceOf, null, SR.Dimensions);\r
+                                graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Dimensions", Bindings.STRING);\r
+                                graph.addLiteral(newCell, SR.Dimensions_fitColumns, SR.Dimensions_fitColumns_Inverse, L0.Boolean, false, Bindings.BOOLEAN);\r
+                                graph.addLiteral(newCell, SR.Dimensions_fitRows, SR.Dimensions_fitRows_Inverse, L0.Boolean, false, Bindings.BOOLEAN);\r
+                                graph.addLiteral(newCell, SR.Dimensions_columnCount, SR.Dimensions_columnCount_Inverse, L0.Integer, 128, Bindings.INTEGER);\r
+                                graph.addLiteral(newCell, SR.Dimensions_rowCount, SR.Dimensions_rowCount_Inverse, L0.Integer, 4096, Bindings.INTEGER);\r
+                                graph.claim(sheet, L0.ConsistsOf, L0.PartOf, newCell);\r
+                            }\r
+\r
+                            BTree bt = new BTree(graph, SpreadsheetUtils.SPREADSHEET_BTREE_SIZE, SR.Lines, SR.LineNode, L0.PartOf, true);\r
+                            result.add(bt);\r
+                            Resource lines = bt.rootOfBTree();\r
+\r
+                            graph.claimLiteral(lines, L0.HasName, L0.NameOf, L0.String, "Lines", Bindings.STRING);\r
+                            graph.claim(sheet, L0.ConsistsOf, L0.PartOf, lines);\r
+                            \r
+                            List<Integer> columnWidths = new ArrayList<>();\r
+                            List<Integer> rowHeights = new ArrayList<>();\r
+                            \r
+                            for(int rowN = 0;rowN<=sheet_.getLastRowNum();rowN++) {\r
+                                \r
+                                Row row = sheet_.getRow(rowN);\r
+                                if(row == null) continue;\r
+\r
+                                boolean hasSomething = false;\r
+                                int count = row.getLastCellNum();\r
+                                for(int i=0;i<count;i++) {\r
+                                    Cell val = row.getCell(i, Row.RETURN_BLANK_AS_NULL);\r
+                                    if(val != null) {\r
+                                        hasSomething = true;\r
+                                        break;\r
+                                    }\r
+                                }\r
+\r
+                                if(!hasSomething) continue;\r
+                                \r
+                                Resource line = graph.newResource();\r
+                                graph.claim(line, L0.InstanceOf, null, SR.Line);\r
+                                graph.claimLiteral(line, L0.HasName, L0.NameOf, L0.String, "Row" + rowN, Bindings.STRING);\r
+                                bt.insertBTree(graph, Variant.ofInstance(rowN), line);\r
+\r
+                                if(rowHeights.size()<=rowN){\r
+                                       for(int zz = rowHeights.size(); zz <= rowN; zz++)\r
+                                               rowHeights.add(null);\r
+                                }\r
+                                if(rowHeights.get(rowN)==null){\r
+                                       Double rowHeightInPoints = row.getHeight()/20.0;\r
+                                    Double rowHeightInPixels = rowHeightInPoints*POINT_TO_PIXEL_RATIO+1;\r
+                                    int rH = rowHeightInPixels.intValue();\r
+                                                       //System.out.println("rowHeightInPixels: " + rH);\r
+                                       rowHeights.set(rowN, rH);\r
+                                }\r
+                                \r
+                                for(int i=0;i<count;i++) {\r
+                                    Cell val = row.getCell(i, Row.RETURN_BLANK_AS_NULL);\r
+                                    if(val != null) {\r
+                                       \r
+                                        String cellName = SpreadsheetUtils.cellName(rowN, i);\r
+                                        \r
+                                        int ccIndx = val.getColumnIndex();\r
+                                        if(columnWidths.size()<=ccIndx){\r
+                                               for(int zz = columnWidths.size(); zz <= ccIndx; zz++)\r
+                                                       columnWidths.add(null);\r
+                                        }\r
+                                        if(columnWidths.get(ccIndx)==null){\r
+                                               Integer ccWidth = sheet_.getColumnWidth(ccIndx);\r
+                                               Double characterWidth = null;\r
+                                               Double pixelWidthD = null;\r
+                                               if(ccWidth<=438) {\r
+                                                       characterWidth = (ccWidth/256.0)/(1.0+181.92/256.0);\r
+                                                       characterWidth = Math.round(characterWidth*100)/100.0;\r
+                                                       pixelWidthD = (characterWidth*11.986)+0.0078;\r
+                                               }\r
+                                               else {\r
+                                                       characterWidth = (ccWidth-181.92)/256.0;\r
+                                                       characterWidth = Math.round(characterWidth*100)/100.0;\r
+                                                       pixelWidthD = (characterWidth*7.0)+5.0;\r
+                                               }\r
+                                               columnWidths.set(ccIndx, (int)Math.round(pixelWidthD));\r
+                                        }\r
+                                        \r
+                                        Resource cell = graph.newResource();\r
+                                        graph.claim(cell, L0.InstanceOf, null, SR.TextCell);\r
+                                        graph.claimLiteral(cell, L0.HasName, L0.NameOf, L0.String, cellName, Bindings.STRING);\r
+                                        graph.claim(line, L0.ConsistsOf, L0.PartOf, cell);\r
+\r
+                                        if(Cell.CELL_TYPE_BOOLEAN == val.getCellType()){\r
+                                            boolean bool = val.getBooleanCellValue();\r
+                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(bool), Bindings.VARIANT);\r
+                                        }\r
+                                        else if(Cell.CELL_TYPE_FORMULA == val.getCellType()) {\r
+                                            Variant v = null;\r
+                                            String formula = val.getCellFormula();\r
+                                            if(val.isPartOfArrayFormulaGroup()) {\r
+                                                CellRangeAddress addr = val.getArrayFormulaRange();\r
+                                                v = new Variant(ExcelArrayFormula.BINDING, new ExcelArrayFormula(addr.formatAsString(), formula));\r
+                                            } else {\r
+                                                v = new Variant(ExcelFormula.BINDING, new ExcelFormula(formula));\r
+                                            }\r
+                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, v, Bindings.VARIANT);\r
+                                            \r
+                                        } else if(Cell.CELL_TYPE_STRING == val.getCellType()) {\r
+                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);\r
+                                        } else if(Cell.CELL_TYPE_NUMERIC == val.getCellType()) {\r
+                                            Double value = Double.parseDouble(val.toString());\r
+                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(value), Bindings.VARIANT);\r
+                                        } else {\r
+                                            graph.claimLiteral(cell, SR.Cell_content, SR.Cell_content_Inverse, L0.Variant, Variant.ofInstance(val.toString()), Bindings.VARIANT);\r
+                                            System.err.println("Unprocessed cell type " + val.getCellType() + ", SheetName: " + sheetName + ", Row: " + rowN + ", Col:" + i);\r
+                                        }\r
+//                                    System.err.println("Current cell " + sheetName + ":"+ cellName);\r
+                                        Resource style = assignStyles(graph, SR, val, book, existingStyles, existingStyles2, "Style_" + existingStyles.size());\r
+                                        if (style != null)\r
+                                            graph.claim(cell, SR.Cell_HasStyle, style);\r
+                                        \r
+                                    }\r
+                                }\r
+                            }\r
+                            {\r
+                               int[] cw = new int[(columnWidths.size())];\r
+                               int[] rw = new int[(rowHeights.size())];\r
+                               for(int i = 0; i<columnWidths.size();i++){\r
+                                       Integer colWidth = columnWidths.get(i);\r
+                                       if(colWidth==null) cw[i] = 0;\r
+                                       else cw[i] = colWidth;\r
+                               }\r
+                               for(int i = 0; i < rowHeights.size();i++){\r
+                                       Integer rowHeight = rowHeights.get(i);\r
+                                       if(rowHeight==null) rw[i] = 0;\r
+                                       else rw[i] = rowHeight;\r
+                               }\r
+                                Resource newCell = graph.newResource();\r
+                                graph.claim(newCell, L0.InstanceOf, null, SR.Headers);\r
+                                graph.claimLiteral(newCell, L0.HasName, L0.NameOf, L0.String, "Headers", Bindings.STRING);\r
+                                graph.addLiteral(newCell, SR.Headers_columnLabels, SR.Headers_columnLabels_Inverse, L0.StringArray, new String[0], Bindings.STRING_ARRAY);\r
+                                graph.addLiteral(newCell, SR.Headers_columnWidths, SR.Headers_columnWidths_Inverse, L0.IntegerArray, cw, Bindings.INT_ARRAY);\r
+                                graph.addLiteral(newCell, SR.Headers_rowHeights, SR.Headers_rowHeights_Inverse, L0.IntegerArray, rw, Bindings.INT_ARRAY);\r
+                                graph.claim(sheet, L0.ConsistsOf, L0.PartOf, newCell);\r
+                            }\r
+                        }\r
+\r
+                        Layer0X L0X = Layer0X.getInstance(graph);\r
+                        DatabaseIndexing.deleteIndex(graph, L0X.DependenciesRelation, container);\r
+\r
+                        btreeContainer.set(result);\r
+                        bookContainer.set(book);\r
+                    } catch (Exception e) {\r
+                        e.printStackTrace();\r
+                        btreeContainer.add(Collections.emptyList());\r
+                    }\r
+                }\r
+            });\r
+            \r
+            Simantics.getSession().sync(new DelayedWriteRequest() {\r
+                \r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    for (BTree bt : btreeContainer.get())\r
+                        bt.flushCachedBTree(graph);\r
+                }\r
+            });\r
+            \r
+            Simantics.getSession().sync(new WriteRequest() {\r
+                \r
+                @Override\r
+                public void perform(WriteGraph graph) throws DatabaseException {\r
+                    Resource delayedBook = bookContainer.get();\r
+                    XSupport support = graph.getService(XSupport.class);\r
+                    Resource book = support.convertDelayedResourceToResource(delayedBook);\r
+                    SpreadsheetGraphUtils.constructAndInitializeRunVariable(graph, book);\r
+                }\r
+            });\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+        }\r
+        \r
+    }\r
+    \r
+    private static Resource assignStyles(WriteGraph graph, SpreadsheetResource SR, Cell cell, Resource book, Map<Integer, Resource> existingStyles, Map<Integer, SpreadsheetStyle> existingStyles2, String styleName) throws DatabaseException {\r
+        CellStyle cellStyle = cell.getCellStyle();\r
+        if (cellStyle != null) {\r
+            \r
+            SpreadsheetStyle sstyle = existingStyles2.get(cellStyle.hashCode());\r
+            if (sstyle == null) {\r
+                SpreadsheetStyleBuilder builder = SpreadsheetStyle.newInstace();\r
+                foregroundColor(cellStyle, builder);\r
+                backgroundColor(cellStyle, builder);\r
+                alignment(cellStyle, builder);\r
+                borders(cellStyle, builder);\r
+                font(cellStyle, builder);\r
+                dataformat(cellStyle, builder);\r
+                \r
+                builder.name(styleName);\r
+                sstyle = builder.build();\r
+                existingStyles2.put(cellStyle.hashCode(), sstyle);\r
+            }\r
+\r
+            int styleId = sstyle.getStyleId();\r
+            \r
+            Resource style = existingStyles.get(styleId);\r
+            if (style == null) {\r
+                style = SpreadsheetGraphUtils.createStyle(graph, book, sstyle);\r
+                \r
+                existingStyles.put(styleId, style);\r
+            }\r
+            return style;\r
+        } else {\r
+            SpreadsheetStyle empty = SpreadsheetStyle.empty();\r
+            Resource emptyStyle = SpreadsheetGraphUtils.createStyle(graph, book, empty);\r
+            existingStyles.put(empty.getStyleId(), emptyStyle);\r
+        }\r
+        return null;\r
+    }\r
+\r
+    private static void dataformat(CellStyle cellStyle, SpreadsheetStyleBuilder builder) {\r
+        if (cellStyle instanceof XSSFCellStyle) {\r
+            XSSFCellStyle xssfStyle = (XSSFCellStyle) cellStyle;\r
+            String formatString = xssfStyle.getDataFormatString();\r
+            short formatIndex = xssfStyle.getDataFormat();\r
+            \r
+            builder.formatString(formatString);\r
+            builder.formatIndex(formatIndex);\r
+        }\r
+    }\r
+\r
+    private static void borders(CellStyle style, SpreadsheetStyleBuilder builder) {\r
+//        short borderBottom = style.getBorderBottom();\r
+//        System.out.println("BorderBottom : " + borderBottom);\r
+//        \r
+//        short borderTop = style.getBorderTop();\r
+//        System.out.println("BorderTop : " + borderTop);\r
+//        \r
+//        short borderLeft = style.getBorderLeft();\r
+//        System.out.println("BorderLeft : " + borderLeft);\r
+//        \r
+//        short borderRight = style.getBorderRight();\r
+//        System.out.println("BorderRight : " + borderRight);\r
+    }\r
+\r
+    private static void alignment(CellStyle style, SpreadsheetStyleBuilder builder) {\r
+        short alignment = style.getAlignment();\r
+        int horizontal;\r
+        switch (alignment) {\r
+        case CellStyle.ALIGN_CENTER:\r
+            horizontal = 1;\r
+            break;\r
+        case CellStyle.ALIGN_LEFT:\r
+            horizontal = 0;\r
+            break;\r
+        case CellStyle.ALIGN_RIGHT:\r
+            horizontal = 2;\r
+        default:\r
+            horizontal = 0;\r
+            break;\r
+        }\r
+        \r
+        short verticalAlignment = style.getVerticalAlignment();\r
+        int vertical;\r
+        switch (verticalAlignment) {\r
+        case CellStyle.VERTICAL_BOTTOM:\r
+            vertical = 2;\r
+            break;\r
+        case CellStyle.VERTICAL_TOP:\r
+            vertical = 0;\r
+            break;\r
+        case CellStyle.VERTICAL_CENTER:\r
+            vertical = 1;\r
+        default:\r
+            vertical = 2;\r
+            break;\r
+        }\r
+        int align = 0;\r
+        align = (align & 12) + horizontal;\r
+        align = (align & 3) + (vertical << 2);\r
+        builder.align(align);\r
+    }\r
+\r
+    private static void foregroundColor(CellStyle cellStyle, SpreadsheetStyleBuilder builder) {\r
+        if (cellStyle instanceof XSSFCellStyle) {\r
+            XSSFCellStyle xssfStyle = (XSSFCellStyle) cellStyle;\r
+            \r
+            XSSFColor fillColor = xssfStyle.getFillForegroundXSSFColor();\r
+            XSSFColor bColor = xssfStyle.getFillBackgroundXSSFColor();\r
+            XSSFColor colorColor = xssfStyle.getFillForegroundColorColor();\r
+            XSSFColor fillcColor = xssfStyle.getFillForegroundXSSFColor();\r
+//            byte[] fills = fillColor.getRgbWithTint();\r
+            \r
+            XSSFFont xssfFont = xssfStyle.getFont();\r
+            XSSFColor fontColor = xssfFont.getXSSFColor();\r
+            if (fontColor != null) {\r
+                \r
+                byte[] rgb = fontColor.getRgbWithTint();\r
+                \r
+                String ix = fontColor.getARGBHex();\r
+                RGB.Integer s = hex2Rgb(ix);\r
+                builder.foreground(s);\r
+//                if (rgb != null) {\r
+//                    int red = rgb[0] & 0xff;\r
+//                    int green = rgb[1] & 0xff;\r
+//                    int blue = rgb[2] & 0xff;\r
+//                    RGB.Integer color = s;\r
+//                    builder.foreground(color);\r
+//                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private static void font(CellStyle style, SpreadsheetStyleBuilder builder) throws BindingException, ManyObjectsForFunctionalRelationException, ServiceException {\r
+        if (style instanceof XSSFCellStyle) {\r
+            XSSFCellStyle xssfStyle = (XSSFCellStyle) style;\r
+            XSSFFont xssfFont = xssfStyle.getFont();\r
+            String fontStyle = "Normal";\r
+            if (xssfFont.getBold()) {\r
+                fontStyle = "Bold";\r
+                if (xssfFont.getItalic()) {\r
+                    fontStyle += "Italic";\r
+                }\r
+            } else if (xssfFont.getItalic()) {\r
+                fontStyle = "Italic";\r
+            } else if (xssfFont.getUnderline() != 0) {\r
+                // not supported\r
+            } else if (xssfFont.getStrikeout()) {\r
+                // not supported\r
+            }\r
+            Font font = new Font(xssfFont.getFontName(), xssfFont.getFontHeightInPoints(), fontStyle);\r
+            builder.font(font);\r
+        }\r
+    }\r
+\r
+    private static void backgroundColor(CellStyle style, SpreadsheetStyleBuilder builder) {\r
+        if (style instanceof XSSFCellStyle) {\r
+            XSSFCellStyle cellStyle = (XSSFCellStyle) style;\r
+            \r
+            XSSFColor xssfColor = cellStyle.getFillBackgroundColorColor();\r
+            XSSFColor xssfC = cellStyle.getFillForegroundColorColor();\r
+            \r
+            if (xssfC != null) {\r
+                String hex = xssfC.getARGBHex();\r
+                if (hex == null)\r
+                    return;\r
+                RGB.Integer color = hex2Rgb(hex);\r
+                builder.background(color);\r
+            } else if (xssfColor != null) {\r
+                String hex = xssfColor.getARGBHex();\r
+                if (hex == null)\r
+                    return;\r
+                RGB.Integer color = hex2Rgb(hex);\r
+                builder.background(color);\r
+            }\r
+            \r
+//            byte[] rgb = xssfColor.getRgbWithTint();\r
+//            \r
+//            if (rgb != null) {\r
+//                int red = rgb[0] & 0xff;\r
+//                int green = rgb[1] & 0xff;\r
+//                int blue = rgb[2] & 0xff;\r
+//                \r
+//                RGB.Integer color = new RGB.Integer(red, green, blue);\r
+//                builder.background(color);\r
+//            }\r
+        }\r
+    }\r
+    \r
+    public static RGB.Integer hex2Rgb(String colorStr) {\r
+\r
+        String s1 = colorStr.substring( 2, 4 );\r
+        String s2 = colorStr.substring( 4, 6 );\r
+        String s3 = colorStr.substring( 6, 8 );\r
+        return new RGB.Integer(\r
+                Integer.valueOf( s1, 16 ),\r
+                Integer.valueOf( s2, 16 ),\r
+                Integer.valueOf( s3, 16 ) );\r
+    }\r
+\r
+\r
+    public static void main(String[] args) throws Exception {\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("E888"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("E8:E22"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("SUM(E8:E22)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("kääk"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        \r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("WeekStart(SheetNumber(A1),Year)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        \r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("0.001*C41"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("1+0.5"));\r
+            AstValue v = p.arithmetic_expression();\r
+            System.err.println("v="+v);\r
+        }\r
+        \r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("J675+0.5"));\r
+            AstValue v = p.arithmetic_expression();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("SUM(J675+0.5)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        \r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("IF(J711=0,0,J675+0.5+0.05)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("B:B"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("+SUMIF(B:B,E9,C:C)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("A(B())"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("+VLOOKUP(ROUND(C154*100, 0),$M$3:$T$34,C$2,TRUE)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+        \r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("LINEST(N37:N42,M37:M42^{1,2},TRUE,1)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("IF(K16>0,(SUM(K614))/K16,)"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+        }\r
+\r
+        {\r
+            SheetFormulaParser p = new SheetFormulaParser(new StringReader("(-(0.2/2)+SQRT((0.2/2)^2-4*((0.2/2)^2-(3*0.17625)/(PI()*4.7))))"));\r
+            AstValue v = p.relation();\r
+            System.err.println("v="+v);\r
+            SpreadsheetBook book = new SpreadsheetBook();\r
+            Object o = v.accept(new CellValueVisitor(SpreadsheetEvaluationEnvironment.getInstance(book), null));\r
+            System.err.println("o="+o);\r
+        }\r
+        \r
+    }\r
+    \r
+}\r