【问题标题】:content apps for office: how to set range and read from it?办公内容应用程序:如何设置范围并从中读取?
【发布时间】:2014-10-22 09:14:29
【问题描述】:

我正在尝试为办公开发一些简单的内容应用程序。

我想设置一些范围,然后从中读取。用户必须填写几个框,点击一些按钮数据后应该分析。在 VBA 或 VSTO 中这将非常简单,但我必须将其作为办公应用程序来完成。这个 office javascript API 对我来说很不自然。

一些简短的场景:

  1. 用户在 excel 中选择了某个范围,点击了某个按钮,选定的范围被设置为某个公共变量
  2. 用户点击按钮,函数运行,它从几个范围加载和分析数据。

有人可以帮忙吗?

在 VBA 中:

sub somesub

dim rngSomeRange as range
dim rngSomeRange2 as range
dim rngCell as range
dim colValues as new collection
dim colValues2 as new collection

set rngSomeRange =range("someRange")

for each  rngCell in rngSomeRange

msgbox rngcell.value
colValues.add rngcell.value

next rngCell

for each  rngCell in rngSomeRange2

msgbox rngcell.value
colValues2.add rngcell.value

next rngCell

procAnalyze(colValues, colValues2)

end sub

【问题讨论】:

    标签: excel apps-for-office


    【解决方案1】:

    首先你需要创建一个绑定,以便以后可以引用它:

    var range = "A5:C5";
    var id = "numbers";
    
    Office.context.document.bindings.addFromNamedItemAsync(range, "matrix", { id: id });
    

    然后,您可以稍后引用此绑定来设置数据:

    var data = [['one', 'two', 'three']]; // 2 dimensional array (matrix)
    Office.select("bindings#" + id).setDataAsync(data, { coercionType: "matrix" });
    

    请注意addFromNamedItemAsyncsetDataAsync 都是异步方法,因此您应该提供回调方法:

    1. 检查结果状态
    2. 确保在写入之前创建绑定

    这是一个完整的例子:

    Office.context.document.bindings.addFromNamedItemAsync(range, "matrix", { id: id },
        function (asyncResult) {
            if (asyncResult.status == "failed") {
                // Error
            } else {
                var data = [['one', 'two', 'three']]; // 2 dimensional array (matrix)
                Office.select("bindings#" + id).setDataAsync(data, { coercionType: "matrix" },
                    function (asyncResult) {
                        if (asyncResult.status == "failed") {
                            // Error
                        } else {
                            // Success: 'one' is in A5, 'two' is in B5 and 'three' is in C5
                        }
                    });
            }
        });
    

    【讨论】:

      【解决方案2】:

      首先选择一些单元格,然后按下链接到此功能的按钮,它将绑定到这些单元格:

      function bindData() { //A3
          Office.context.document.bindings.addFromSelectionAsync("matrix", { id: 'myBindingXXX' },
              function (asyncResult) {
      
                  //NOW DO OUTPUT OR ERROR
                  if (asyncResult.status === "failed") {
                      writeToPage('Error bindData: ' + asyncResult.error.message, 3);
                  } else {
                      writeToPage('Added binding with type: ' + asyncResult.value.type + ' and id: ' +
                          asyncResult.value.id, 1);
                  }
              });
      }
      

      现在创建另一个函数来读取绑定,您可以根据需要在其中添加自己的分析。

      function readBoundData() { //A4 note how id is used here in the binding
          Office.select("bindings#myBindingXXX").getDataAsync({ coercionType: "matrix" },
              function (asyncResult) {
      
                  //NOW DO OUTPUT OR ERROR
                  if (asyncResult.status === "failed") {
                      writeToPage('Error readBoundData: ' + asyncResult.error.message, 3);
                  } else {
                      writeToPage('Selected data: ' + asyncResult.value , 1);
                  }
              });
      }
      

      请注意,一旦绑定了单元格,您就不必选择它们来读取它们。

      欲了解更多信息,请参阅http://microsoft-office-add-ins.com/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多