Share via


Ewa.BrowserUdfs Object

Applies to: apps for SharePoint | Excel Services | SharePoint Server 2013

Represents the collection of browser user-defined functions (UDFs).

Ewa.BrowserUdfs

Remarks

A user-defined function (UDF) is a function that you can create yourself and then add to the list of available functions in Microsoft ExcelMicrosoft Excel when Excel does not provide the kind of function that you want right out of the box.

ECMAScript (JavaScript, JScript) UDFs are UDFs that run in the browser on a web page that has an embedded Excel workbook. You use the ECMAScript (JavaScript, JScript) UDF inside the embedded workbook. As long as you are working with the workbook in the browser, you can use the ECMAScript (JavaScript, JScript) UDF exactly like you use the built-in Excel functions. When the web page is closed, the ECMAScript (JavaScript, JScript) UDF is no longer available.

Example

The following code example shows how to add a browser UDF to the page. The UDF takes a quantity of items and a price, calculates the cost, and then applies a 6% discount to the cost if the quantity is 100 items or more. The code example assumes that you are working with an Excel Web Access Web Part on SharePoint Server 2013.

<script type="text/javascript">
var ewa = null;

// Add event handler for onload event.
if (window.attachEvent) {
    window.attachEvent("onload", ewaOnPageLoad);
}
else {
    window.addEventListener("DOMContentLoaded", ewaOnPageLoad, false);
}

// Add event handler for applicationReady event.

function ewaOnPageLoad() {
    Ewa.EwaControl.add_applicationReady(onApplicationReady);
}


function onApplicationReady(result) {
    ewa = Ewa.EwaControl.getInstances().getItem(0);
    var udfs = ewa.getBrowserUdfs();

    // Add the browser UDF, "DISCOUNT" to the page
    udfs.add("DISCOUNT", DISCOUNT, "Gives company discounted price.", false, false);
}

// UDF that returns a discount (6%) for orders with 100 or more items; 
// otherwise it returns 0.

function DISCOUNT(quantity, price) {
    var theDiscount = 0;
    var discountCost = 0;
    var initialAmount = 0;

    if (quantity >= 100) {
        initialAmount = quantity * price;
        // Apply a 6% discount
        theDiscount = initialAmount * 0.06;
        discountCost = initialAmount - theDiscount;
    }
    else {
        discountCost = initialAmount;
    }    
    return discountCost;
}
</script>

See also

Concepts

Ewa namespace