复制并粘贴Microsoft Javascript示例以检查单元格中的值,但无法正常工作

Copy and pasted Microsoft example of Javascript to check value in cell, but fails to work

本文关键字:常工作 工作 Microsoft Javascript 单元格 检查 复制      更新时间:2023-09-26

我已经盯着下面的代码几个小时了,我不确定从哪里开始解决这个问题。 事先,我相信这更像是一个JavaScript问题,而不是一个Microsoft的Web API问题,因为我实际上是在复制和粘贴他们的代码。

我正在尝试使用Microsoft Excel的Web API在我的网页上嵌入Excel工作表(工作正常)。更具体地说,我试图在单元格上突出显示时,它在警报 javascript 框中显示所选单元格的值。

这是他们的工作示例,其中包含我正在尝试执行的操作的代码 http://www.excelmashup.com/APIBrowser#example105

只需将右下角的选项卡从"输出"更改为"HTML",即可看到与下面相同的代码:

<html>
<head>
  <script type="text/javascript" src="http://r.office.microsoft.com/r/rlidExcelWLJS?v=1&kip=1"></script>
  <script type="text/javascript">
    // run the Excel load handler on page load
    if (window.attachEvent) {
      window.attachEvent("onload", loadEwaOnPageLoad);
    } else {
      window.addEventListener("DOMContentLoaded", loadEwaOnPageLoad, false);
    }
    function loadEwaOnPageLoad() {
      var fileToken = "SDBBABB911BCD68292!110/-4923638281765748078/t=0&s=0&v=!ALTlXd5D3qSGJKU";
      var props = {
              uiOptions: {
                    showGridlines: false,
          selectedCell: "'Sheet1'!C9",
                    showRowColumnHeaders: false,
                    showParametersTaskPane: false
              },
              interactivityOptions: {
                    allowTypingAndFormulaEntry: false,
                    allowParameterModification: false,
                    allowSorting: false,
                    allowFiltering: false,
                    allowPivotTableInteractivity: false
              }
      };
      Ewa.EwaControl.loadEwaAsync(fileToken, "myExcelDiv", props, onEwaLoaded);     
    }
    function onEwaLoaded() {
        document.getElementById("loadingdiv").style.display = "none";
    }
    // This sample gets the value in the highlighted cell. 
// Try clicking on different cells then running the sample.
function execute()
{
    // Get unformatted range values (getValuesAsync(1,...) where 1 = Ewa.ValuesFormat.Formatted)
    ewa.getActiveWorkbook().getActiveCell().getValuesAsync(1,getRangeValues,null);
}     
function getRangeValues(asyncResult)
{
    // Get the value from asyncResult if the asynchronous operation was successful.
    if (asyncResult.getCode() == 0)
    {
        // Get the value in active cell (located at row 0, column 0 of the
        // range which consists of a single cell (the "active cell")).
        alert("Result: " + asyncResult.getReturnValue()[0][0]);
    }
    else 
    {
          alert("Operation failed with error message " + asyncResult.getDescription() + ".");
    }    
}
    </script>
  </head>
  <body>
    <input type="button" onclick="execute();">Execute Sample</input> <<<<<Here is the problem
    <div id="myExcelDiv" style="width: 402px; height: 346px"></div>     
  </body>
</html>

当我将上述内容更改为 onclick="alert('hello')" 时,它工作正常,但当我使用 execute() 时它不会提醒单元格的值; 也许有人可以将代码复制并粘贴到.html文件中,看看这是否只是我的问题,以及Microsoft代码是否适合他们。 如果它不起作用,那也是有用的信息。

键位于变量 "ewa" 上。通常,当页面加载时,loadEwaOnPageLoad()内部有以下行:Ewa.EwaControl.loadEwaAsync(fileToken, "myExcelDiv", props, onEwaLoaded); 因此,我们需要在onEwaLoaded函数中抓取一个Ewa对象。我在这样的地方看到了它(ewa 在其他地方声明):

function onEwaLoaded(asyncResult) {
        /*
         * Add code here to interact with the embedded Excel web app.
         * Find out more at http://msdn.microsoft.com/en-US/library/hh315812.aspx.
         */
        if (asyncResult.getSucceeded()) {
            // Use the AsyncResult.getEwaControl() method to get a reference to the EwaControl object
            ewa = asyncResult.getEwaControl();
          //...

获得 ewa 对象后,其余的就没问题了。没有它,那么变量ewa确实没有定义。

Excel 交互式视图功能已被禁用。但是,我刚刚修复了代码的某些部分以使其正常工作。我已将所有详细信息放在带有星号的评论中

<html>
<head>
    <script type="text/javascript" src="http://r.office.microsoft.com/r/rlidExcelWLJS?v=1&kip=1"></script>
    <script type="text/javascript">
        var ewa = null;//*******************ADDING THIS IS BETTER**************************
        // run the Excel load handler on page load
        if (window.attachEvent) {
            window.attachEvent("onload", loadEwaOnPageLoad);
        } else {
            window.addEventListener("DOMContentLoaded", loadEwaOnPageLoad, false);
        }
        function loadEwaOnPageLoad() {
            var fileToken = "SDBBABB911BCD68292!110/-4923638281765748078/t=0&s=0&v=!ALTlXd5D3qSGJKU";
            var props = {
                uiOptions: {
                    showGridlines: false,
                    selectedCell: "'Sheet1'!C9",
                    showRowColumnHeaders: false,
                    showParametersTaskPane: false
                },
                interactivityOptions: {
                    allowTypingAndFormulaEntry: false,
                    allowParameterModification: false,
                    allowSorting: false,
                    allowFiltering: false,
                    allowPivotTableInteractivity: false
                }
            };
            Ewa.EwaControl.loadEwaAsync(fileToken, "myExcelDiv", props, onEwaLoaded);
        }
        function onEwaLoaded(asyncResult) { //*******************PASS asyncResult PARAMETER**************************
            //*******************THERE IS NO SUCH THING CALLED "loadingdiv"**************************
            //document.getElementById("loadingdiv").style.display = "none";
            //*******************ADD THIS IF-ELSE STATMENT**************************
            if (asyncResult.getSucceeded()) {
                // Use the AsyncResult.getEwaControl() method to get a reference to the EwaControl object
                alert("Async operation Succeeded!");
                ewa = asyncResult.getEwaControl();
            }
            else {
                alert("Async operation failed!");
            }
        }
        // This sample gets the value in the highlighted cell. 
        // Try clicking on different cells then running the sample.
        function execute() {
            // Get unformatted range values (getValuesAsync(1,...) where 1 = Ewa.ValuesFormat.Formatted)
            ewa.getActiveWorkbook().getActiveCell().getValuesAsync(1, getRangeValues, null);
        }
        function getRangeValues(asyncResult) {
            // Get the value from asyncResult if the asynchronous operation was successful.
            if (asyncResult.getCode() == 0) {
                // Get the value in active cell (located at row 0, column 0 of the
                // range which consists of a single cell (the "active cell")).
                alert("Result: " + asyncResult.getReturnValue()[0][0]);
            }
            else {
                alert("Operation failed with error message " + asyncResult.getDescription() + ".");
            }
        }
    </script>
</head>
<body>
    <!-- *******************THE SIMICOLON AFTER execute() IS REMOVED*********************** -->
    <input type="button" onclick="execute()" value="Execute Sample"></input>
    <!-- *******************STYLE IS IMPROVED*********************** -->
    <div id="myExcelDiv" style="width: 100%; height: 1000px"> </div>
</body>
</html>