2016-01-21 74 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