0

我已经盯着下面的代码几个小时了,我不知道从哪里开始解决这个问题。在此之前,我相信这更多的是我的 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 代码是否适合他们。如果它不起作用,那也将是有用的信息。

4

1 回答 1

1

关键在于变量“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 确实没有定义。

于 2015-01-08T19:16:00.420 回答