2016-01-21 49 views
0

我需要您的帮助。循环访问数组值并生成不同的列名称输出

我想知道是否可以修改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> 

回答

0

我不知道这是否会100%的工作,因为我没有Excel,我不知道你是怎么才可以用JS在E上xcel电子表格,但我会先解释我的思路。

如果您在单独的数组中定义了您在电子表格中输入的任何值(在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> 
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; 
} 
+0

'++ c' will be conditional i believe – charlietfl