日期:2014-05-16  浏览次数:20406 次

SharePoint 2013 Excel service 常用JavaScript类和方法
<div id="logPane"></div>
<script type="text/javascript">
    // Excel service Javascript OM http://msdn.microsoft.com/en-us/library/ee589018.aspx
    var ewa = null;

    // 注册onload事件
    if (window.attachEvent) {
        window.attachEvent("onload", ewaOnPageLoad);
    }
    else {
        window.addEventListener("DOMContentLoaded", ewaOnPageLoad);
    }

    // 处理onload事件
    function ewaOnPageLoad() {

        // 注册applicationReady事件
        if (typeof (Ewa) != "undefined") {
            Ewa.EwaControl.add_applicationReady(ewaApplicationReady);
        }
        else {
            alert("Error - the EWA is not loaded!");
        }
    }

    // 处理 applicationReady事件
    function ewaApplicationReady() {
        try
        {
            ewa = Ewa.EwaControl.getInstances().getItem(0);

            // 注册选择区域发生变化的事件
            ewa.add_activeSelectionChanged(selectionChanged);

            // 取Range
            // Row, Column, RowCount, ColumnCount
            var range = ewa.getActiveWorkbook().getRange("Personal Monthly Budget", 23, 1, 9, 4);

            // 让Range变成Active 状态, 会让Range滑动到WebPart的可视区域 
            range.activateAsync(0, 0, rangeActived, range)
            logMessage("ewaApplicationReady");

            // 取得Workbook的路径
            logMessage("active workbook path:" + ewa.getActiveWorkbook().getWorkbookPath());
        }
        catch (err) {
            alert(err);
        }
    }

    function rangeActived(asyncResult) {
        logMessage("Range now is actived" + asyncResult.getSucceeded());
        var range = asyncResult.getUserContext();
        printRange(range);

        alert("Range is now actived, click OK to show the value");

        // Ewa.Range.getValuesAsync(format, callback, userContext);
        // 取 Range里面的值
        range.getValuesAsync(0, gotValus, range);

    }

    // 处理getValuesAsync
    function gotValus(asyncResult) {
        if (asyncResult.getCode() == 0) {
            var range = asyncResult.getUserContext();
            var values = asyncResult.getReturnValue();
            logMessage("Values for range:");
            for (var i = 0; i < values.length; i++) {
                for (var j = 0; j < values[i].length; j++) {
                    logMessage(values[i][j]);
                    values[i][j] += "updated";
                }
            }

            // 设置Range里面的值
            // Ewa.Range.setValuesAsync(values, callback, userContext);
            range.setValuesAsync(values, setRangeValues, range);
        }
        else {
            alert("Operation failed with error message " + asyncResult.getDescription() + ".");
        }
    }

    // 处理setValuesAsync
    function setRangeValues(asyncResult) {
        logMessage("Set values completed: " + asyncResult.getSucceeded());
    }

    // 处理选择区域发生变化的事件
    function selectionChanged(rangeArgs) {
        logMessage("selectionChanged--->");
        var range = rangeArgs.getRange();
        var value = rangeArgs.getFormattedValues();
        logMessage("          value:" + value)
        printRange(range);
    }

    function printRange(range) {
        var sheetName = range.getSheet().getName();

        // 取Range的列号
        var col = range.getColumn();

        // 取Range的行号
        var row = range.getRow();

        // var value = rangeArgs.getFormattedValues();
        // 取Range的地址
        var address = range.getAddressA1();

        // 取Range的列数
        var colCount = range.getColumnCount();

        // 取Range的行数
        var rowCount = range.getRowCount();
     
        logMessage("          sheetName:" + sheetName);
        logMess