循环数组值并生成不同的列名输出

时间:2016-01-21 21:40:07

标签: javascript arrays loops

我需要你的帮助。

我想知道是否可以修改for循环,循环数组值并根据数组值输出列名,但也只有(1)出现单个项,从而产生不同的列名。这是javascript代码输出到目前为止的图片:

enter image description here

这是所需的输出。正如您所看到的,它在不使用重复的情况下更具视觉吸引力。我将在后面的列名称下计算数字。

enter image description here

以下是相关的Javascript代码:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type="text/css">

</style>

<script type="text/javascript">

function search_array(arr, str){
  var searchExp = new RegExp(str,"gi");
  return (searchExp.test(arr))?true:false;
}

function build_sheet() {

    var Excel = new ActiveXObject("Excel.Application")

    var Book = Excel.Workbooks.Add()

    var Sheet = Book.ActiveSheet

    var temp = "BNI to President","BNI to Director","BNI to Manager","BNA to President","BNA to Director","BNA to Manager"

    var c = 2 /* Start position @ Column 2*/

    for(var i = 0; i < temp.length; i++) {

        if (search_array(temp[i], "BNI to") == true) {
            Sheet.Cells(2,c).Value = "Briefing Notes (Info)"
        }
        if (search_array(temp[i], "BNA to") == true) {
            Sheet.Cells(2,c).Value = "Briefing Notes (Approval)"
        }
        else {
            Sheet.Cells(2,c).Value = temp[i]
        }
        ++c
    }

    Excel.visible = true

    Excel.quit()

    Excel = null

}
</script>

</head>

<body>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标,但一个简单的方法是简单地记录所写的内容,如果已经写好了,就不要再写了:

function build_sheet() {

    var Excel = new ActiveXObject("Excel.Application");
    var Book = Excel.Workbooks.Add();
    var Sheet = Book.ActiveSheet;
    var temp = "BNI to President","BNI to Director","BNI to Manager","BNA to President","BNA to Director","BNA to Manager";
    var c = 2; /* Start position @ Column 2*/

    var written = ['info'=>false, 'approval'=>false];

    for(var i = 0; i < temp.length; i++) {

        if (!written['info'] && search_array(temp[i], "BNI to") == true) {
            Sheet.Cells(2,c).Value = "Briefing Notes (Info)";
            written['info'] = true;
            ++c;
        }
        if (!written['approval'] && search_array(temp[i], "BNA to") == true) {
            Sheet.Cells(2,c).Value = "Briefing Notes (Approval)";
            written['approval'] = true;
            ++c;
        }
        else {
            Sheet.Cells(2,c).Value = temp[i];
            ++c;
        }
    }

    Excel.visible = true;
    Excel.quit();
    Excel = null;
}

答案 1 :(得分:0)

我不确定这是否100%有效,因为我没有excel而且我不知道你如何在Excel电子表格上使用JS,但我会解释我的思路第一

如果您跟踪在电子表格中输入的任何值(在for循环外部定义的单独数组中),则可以检查该数组以查看是否已插入temp[i]。如果没有,那么运行其余的代码,这似乎工作正常。

    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <style type="text/css">
        </style>
        <script type="text/javascript">
          function search_array(arr, str){
            var searchExp = new RegExp(str,"gi");
            return (searchExp.test(arr))?true:false;
          }

          function build_sheet() {
            var Excel = new ActiveXObject("Excel.Application")
            var Book = Excel.Workbooks.Add()
            var Sheet = Book.ActiveSheet
            var temp = ["BNI to President","BNI to Director","BNI to Manager","BNA to President","BNA to Director","BNA to Manager"];
            var c = 2 /* Start position @ Column 2*/
            var arr = []

            for( var i = 0; i < temp.length; i++) {
              if (arr.indexOf(temp[i]) == -1) {
                arr.push(temp[i]);
                if (search_array(temp[i], "BNI to") == true) {
                  Sheet.Cells(2,c).Value = "Briefing Notes (Info)"
                }
                if (search_array(temp[i], "BNA to") == true) {
                  Sheet.Cells(2,c).Value = "Briefing Notes (Approval)"
                }
                else {
                  Sheet.Cells(2,c).Value = temp[i]
                }
              }
              ++c
            }
          Excel.visible = true
          Excel.quit()
          Excel = null
          }
        </script>
      </head>
      <body>
      </body>
    </html>