SharePoint 2013 Excelサービス共通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);
        logMessage("          column:" + col);
        logMessage("          row:" + row);        
        logMessage("          address:" + address);
        logMessage("          colCount:" + colCount);
        logMessage("          rowCount:" + rowCount)
    }

    function logMessage(log) {
        var logDiv = document.getElementById("logPane");
        logDiv.innerHTML += "<br/>" + log;;
    }


    // var value = Ewa.NamedItem.getRefersToRangeAsync(callback, userContext);

    // var value = Ewa.NamedItem.getNamedItemType();
    
    // var value = Ewa.NamedItem.getName();
    
    // Ewa.NamedItem.activateAsync(callback, userContext);

    // var value = Ewa.NamedItemCollection.getCount();
    
    // var value = Ewa.NamedItemCollection.getItem(index);
    
    // var value = Ewa.NamedItemCollection.getItemByName(name);

    // Ewa.NamedItemType 


</script>