【问题标题】:excel javascript API array handlingexcel javascript API 数组处理
【发布时间】:2017-01-22 22:34:35
【问题描述】:

我正在使用 javascript API 为 excel 2016 开发插件。我可以成功地将范围放入数组并获取要显示在 console.log 中的值。我还能够使用 JSON.stringify();

将值放入 JSON 数组

我需要操作数组来删除空值(“”)。 这可以通过使用常规的 javascript 数组方法来完成吗?

我想我可以使用与 var shWk 类似的方法将结果显示回不同的工作表中

以下是我正在尝试做的一些 sn-ps:


 (function () {
        "use strict";

        // The initialize function must be run each time a new page is loaded
        Office.initialize = function (reason) {
            $(document).ready(function () {
                app.initialize();
                //document.getElementById("date").innerHTML = Date("MAR 30 2017");
                $('#deleteTab').click(deleteTab);
                $('#preview').click(preview);
                $('#publish').click(publish);

            });
        };

        function preview() {
            Excel.run(function(ctx) {
              //getting the colname from a date range in B2
            var colName = ctx.workbook.worksheets.getItem('preview').getRange("B2");
            colName.load('values');
            return ctx.sync().then(function() {
              //converting colname value to string for column name
                var wkN = (colName.values).toString();
                // displaying on the task pane
                document.getElementById("tst").innerText = wkN;
                // testing to confirm i got the correct colname
                var shWk = ctx.workbook.worksheets.getItem('preview').getRange("B3");
                shWk.values = colName.values;
                //building the column connection by setting the table name located on a different worksheet 
                var tblName = 'PILOT_ZMRP1';
                var tblWK = ctx.workbook.tables.getItem(tblName).columns.getItem(wkN);         
                //loading up tblWK
                tblWK.load('values');
                return ctx.sync().then(function(){
                //this is where my question is:
                    var arry = tblWK.values;
                    for (var i=0; i < tblWK.length; i++){
                        if (tblWK.values !== ""){
                        arry.values[i][0]) = tblWK.values[i][0]
                        };
                    };
                    console.log(arry.length); //returns 185
                    console.log (arry.values);//returns undefined
                    tblWK.values = arry;
                    var tblWeek = tblWK.values;
                    console.log(tblWeek.length);//returns 185
                    console.log(tblWK.values);//returns [object Array]  [Array[1],Array[2]
               })              
            });
}).catch(function (error) {
	console.log(error);
    console.log("debug info: " + JSON.stringify(error.debugInfo));
});
}

我错过了什么?您能否指出一些在 office.js 的特定上下文中处理 javascript 数组的资源?

【问题讨论】:

  • 您是想用空值替换空字符串值(为什么?),还是“删除”对您有其他意义?你所追求的更宏观的场景是什么?
  • 我正在寻找删除数组中返回的空值。有些单元格包含一个值,有些单元格包含“”。这些值对于每一列都是不同的。我正在尝试实现的是一个仅包含真值的数组,而不是包含“”和诸如“text”或16(数字)之类的值的数组。
  • 您的代码中似乎有错误。如果所有项目都是“”,那么您看到的也是我期望的结果。如果有一个项目不是“”,我预计会出错。如果您没有看到错误,请在这两行之间添加“use strict”,编译器应该强制它:return ctx.sync().then(function(){ //这是我的问题所在:

标签: javascript excel office-js


【解决方案1】:

我要感谢大家花时间研究这个问题。这是我在 Stack Overflow 上发布的第二个问题。我看到这个问题没有写得尽可能清楚。我试图实现的是过滤掉具有“”的一维数组中的值。填充数组的数据来自单独工作表中的列,其中包含空值(因此为“”)和数值。下面的代码解决了我的问题。

//using .filter()
        var itm = tblWK.values;
        function filt(itm){
            return itm != "";
        }
        var arry = [];
        var sht = [];
        var j=0;
        var s=0;
        arry.values = tblWK.values.filter(filt);
//then to  build the display range to show the values:

 for (var i=0; i < itm.length-1; i++) {
        if (tblWK.values[i][0]){
        var arry;    //tblWK.values.splice(i,0); -splice did not work, maybe my syntax was wrong?
        console.log("this printed: "+tblWK.values[i][0]);
        var cl = ('D'+i);          //building the range for display
        j++;                        //increasing the range
s=1;//setting the beignning range
        var cll = cl.toString();//getRange() must be a string
        console.log(cll);//testing the output
        }       
}
//using the variable from the for loop
      var cl = ('D'+s+':D'+j);
      var cll = cl.toString();
      console.log(cll);//testing the build string
      sht = ctx.workbook.worksheets.getItem('Preview').getRange(cll);
      sht.values = arry.values; //displays on the preview tab
      console.log (arry.values); //testing the output

通过询问 office.js 支持哪些 vanilla javascript 函数,这个问题可能更容易说出来。通过阅读 Micheal Zlatkovsky 的 Building Office Add-ins using Office.js 和阅读 MDN 文档以及发布的建议答案 here,我找到了很多帮助。

问候, J

【讨论】:

    【解决方案2】:

    我不确定此检查的目的是什么:tblWK.values !== ""。 .values 是一个二维数组,永远不会是“”。

    对于 Excel,值“”表示单元格为空。换句话说,如果你想清除一个单元格,你分配给“”。 null 赋值导致无操作。

    【讨论】:

      【解决方案3】:

      您可以使用 for each 从包含 null 的数组中获取值,并且可以将 null 值推送到另一个数组中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-07
        • 2017-09-26
        • 2021-10-31
        • 2018-06-28
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 2019-03-01
        相关资源
        最近更新 更多