Saturday, May 16, 2009

Excel Addin : calling service

Given a simple asmx service, here is how to call it in an Excel AddIn


(...)
using ExcelAddIn.ServiceReference1;

namespace ExcelAddIn
{
public partial class ThisAddIn
{

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

public void Hello()
{
using (Service1SoapClient svc = new Service1SoapClient())
{
Excel.Range rgStart;
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
rgStart = ws.Application.ActiveCell;
rgStart.Value2 = svc.HelloWorld();
}
}


#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}



In RibbonBar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;

namespace ExcelAddIn
{
public partial class Ribbon1 : OfficeRibbon
{
public Ribbon1()
{
InitializeComponent();
}

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{

}

private void button1_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.Hello();
}
}
}

Friday, May 15, 2009

JQuery : Templating using Pure.js

I have tried to use 'chain.js' but it was too slow so I've adopted Pure.js, which is very fast.
I also use the nice tips of John which allows to hide
html parts.
Here is a sample on how I use Pure with JQuery

Here is the link for the lib:
http://beebole.com/pure
Thanks Mic Cvilic



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="../Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
    <script src="../Scripts/pure.js" type="text/javascript"></script>
    <script src="../Scripts/Services.js" type="text/javascript"></script>  

</head>
<body>
    <button onclick="getProducts();">GetProducts</button>        
    <br />
    <div id="productsList"></div>
    <br />
    <button onclick="decS();">prev 10</button><button onclick="incS();">next 10</button>
    

      
   <!-- Scripts -->
  
  
    <script type="text/html" id="productsListTemplate">
            <table id="products" border="1"; width="400">
            <thead>
                <tr>
                    <th style="width:100;">ProductID</th>
                    <th>ProductName</th>
                </tr>
            </thead>
            <tbody id="tableBody">
            </tbody>
        </table>
    </script>
    <script type="text/html" id="tableBodyTemplate">
       <tr class="context">
         <td class="ProductID"></td>
         <td class="ProductName"></td>
       </tr>
    </script>    

    <script type="text/javascript" charset="utf-8">
        var s = 0;
        var t = 10;
        var uriProducts = "ClientBin/DataService.axd/InfoCentreV01-Web-ProductService/GetProducts?";
        var tableBodyTemplate = $("#tableBodyTemplate").html();
        var prdtTemplate = $("#productsListTemplate").html();
        $("#productsList").html(prdtTemplate);


        function incS() {
            s = s + 10;
            getProducts()
        }

        function decS() {
            if (s > 1) {
                s = s - 10;
            }
            getProducts();
            
        } 
        
        function getProducts() {
            var uri = uriProducts + "s=" + s + "&t=" + 10;
            $.getJSON(uri, function(products) {
                var context = products.Results;
                $("#tableBody").html('');
                $("#tableBody").html(tableBodyTemplate);
                $('#products').autoRender(context);
                
            });
        }            
    </script>  

</body>
</html>