简体   繁体   English

excel javascript API数组处理

[英]excel javascript API array handling

I'm working on an add-in for excel 2016 using the javascript API. 我正在使用javascript API开发Excel 2016的外接程序。 I can successfully get the range into an array and get the values to show in console.log. 我可以成功地将范围放入数组中,并获取要在console.log中显示的值。 I've also been able to get the values into a JSON array using JSON.stringify(); 我还能够使用JSON.stringify()将值放入JSON数组中;

I need to manipulate the array to remove the empty values (""). 我需要操纵数组以删除空值(“”)。 Can this be accomplished by using regular javascript array methods? 可以使用常规的javascript数组方法来完成此操作吗?

I'm thinking I can display the results back into a different worksheet using a similar approach like i did with var shWk 我想我可以像使用var shWk一样使用类似的方法将结果显示回不同的工作表中

Here are some snippets of what I'm trying to do: 以下是我要执行的操作的摘要:


  (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)); }); } 

What am I missing? 我想念什么? Can you point me to some resources for javascript array handling in the specific context of office.js? 您能指出我一些在office.js特定上下文中用于JavaScript数组处理的资源吗?

I want to thank everyone for the time spent looking at this question. 我要感谢大家花在研究这个问题上的时间。 This is my second question ever posted on Stack Overflow. 这是我有史以来在堆栈溢出上发布的第二个问题。 I see that the question was not written as clear as it could've been. 我看到这个问题的写作不够清晰。 What i was trying to achieve was filtering out the values in a 1D array that had "". 我想要达到的目的是过滤出具有“”的一维数组中的值。 The data populating the array was from a column in a separate worksheet that had empty values (hence the "") and numeric values in it. 填充数组的数据来自单独工作表中的一列,该表中具有空值(因此为“”)和数值。 the code below resolved my issue. 下面的代码解决了我的问题。

 //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 

The question was probably easier said by asking what vanilla javascript functions does office.js support. 通过询问office.js支持哪些普通的javascript函数,可以很容易地说出这个问题。 I found a lot help reading Building Office Add-ins using Office.js by Micheal Zlatkovsky and by reading the MDN documentation as well as the suggested answer posted here . 通过阅读Micheal Zlatkovsky的Office.js和阅读MDN文档以及在此处发布的建议答案,我发现在阅读中使用Office.js构建Office加载项有很大帮助。

Regards, J 问候,J

I'm not sure what this check is trying to achieve: tblWK.values !== "" . 我不确定此检查要达到什么目的: tblWK.values !== "" .values is a 2D array and won't ever be "". .values是一个2D数组,永远不会是“”。

For Excel, the value "" means that the cell is empty. 对于Excel,值“”表示该单元格为空。 In other words, if you want to clear a cell, you assign to "". 换句话说,如果要清除单元格,则将其分配给“”。 null value assignment results in no-op. null值分配导致无操作。

您可以通过对每个数组使用来从包含null的数组中获取值,并且可以将null值压入另一个数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM