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

时间:2013-09-12 21:12:24

标签: javascript html

我一直在盯着下面的代码几个小时,我不知道从哪里开始如何解决这个问题。事先,我认为这更像是一个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代码是否适用于他们。如果它不起作用,那也是有用的信息。

1 个答案:

答案 0 :(得分:0)

关键是变量&#34; ewa&#34;。通常当页面加载时,在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确实没有定义。