Status

Blog::Calendar

« September 2010
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today
XML

Blog::Navigation

Blog::Editing

Bookmarks::Blogroll

Blog::Referers

Today''s Page Hits: 435

Other sites

Google Analytics

Powered by Roller Weblogger.
All | JBoss&Seam | JSF | ZK | Music | General | Java
« Some new features of... | Main | A Way to set ZUL... »
20080623 Monday June 23, 2008
How to plug custom formula in to ZK Spreadsheet.

In ZK Spreadsheet, it provides a convenient way to plug any custom formula into it - use tag library. Only few steps need to do to add a custom formula into ZK Spreadsheet. In following example, I add a simple formula "ECHO" which concatenate all value into a string

1.Write a tag lib functions class file, in those functions you must declare two arguments (Object [] and XelContext) just like following example

public class CustomFunction {

    public static Object echo(java.lang.Object[] args, org.zkoss.xel.XelContext ctx){
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        if(args!=null){
            for(int i=0;i<args.length;i++){
                if(i>0) sb.append(", ");
                if(args[i] instanceof RangeRef){
                    RangeRef ref = (RangeRef)args[i];
                    for(Iterator iter = ref.getCells().iterator();iter.hasNext();){
                        Cell cell = (Cell)iter.next();
                        sb.append(cell.getText());
                        if(iter.hasNext()) sb.append(", ");
                    }
                }else if(args[i]!=null){
                    sb.append(args[i].toString());
                }
            }
        }
        sb.append("]");
        return sb.toString();
    }
}

2.Write a tld file(customfunction.tld in this example) in WEB-INF, and point to the function you want to add.

<taglib>
    <uri>http://my.custom.function</uri>
    <description>Custom Function</description>
    <function>
        <name>ECHO</name>
        <function-class>
            my.CustomFunction
        </function-class>
        <function-signature>
            java.lang.Object echo(java.lang.Object[] args,
            org.zkoss.xel.XelContext ctx)
        </function-signature>
        <description></description>
    </function>
</taglib>

3.Add tab-lib directive in zul file and leave the prefix attribute a empty string.

<?taglib uri="/WEB-INF/customfunction.tld" prefix=""?>
<zk>
        <spreadsheet id="ss1" url="/WEB-INF/empty.xls" maxrows="500" maxcolumns="80" width="90%" height="300px"/>
</zk>

4.Use custom function in ZK Spreadsheet, following is the live demo

Trackback URL: http://www.javaworld.com.tw/roller/atticcat/entry/how_to_plug_custom_formula
Comments:

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed
Copyright (C) 2003, 閣樓貓的五四三 (About Cat)