2015-10-16 48 views
2

當我從Excel複製數據並粘貼到Word文檔。他們有「\ t」的每個字&「\ n」之間在該行的末尾,它看起來像這樣:添加管道到表

Letter Number Symbol 
A 1 # 
B 2 ! 
C 3 % 

感謝Joasih你的幫助。這個難題的缺點是,如果沒有數據,它需要有一個空間。

沒有管道:

Letter Number Symbol 
A 1 # 
B  ! 
C 3 % 

隨着管道:

||Letter||Number||Symbol|| 
|A|1|#| 
|B| |!|<-- Needs to have a space if no data 
|C|3|%| 

\t \t var submitBtn = true; 
 

 
\t $("#reset").click(function() { 
 
\t \t $("#withoutPipes").val(""); 
 
\t \t submitBtn = true; 
 
\t }); 
 
\t 
 
\t $("#submit").click(function() { \t \t \t 
 
\t \t if (submitBtn && $("#withoutPipes").val() != "") { 
 
\t \t \t var excelText = $("#withoutPipes").val(); 
 
\t \t \t var excelLines = excelText.replace(/\t\t/g, "\t \t"); 
 
\t \t \t var split = excelLines.replace(/\t\t/g, "\t \t ").split("\n"); 
 
\t \t \t var header = "||" + split[0].replace(/\t/g, "||") + "||"; 
 
\t \t \t var cells = "|" + split.slice(1).join("|\n|").replace(/\t/g, "|") + "|"; 
 
\t \t \t var formattedTable = header + "\n" + cells; 
 
\t \t \t $("#withoutPipes").val(formattedTable); 
 
\t \t \t submitBtn = false; 
 
\t \t } 
 
\t });
#withoutPipes { 
 
    width: 400px; 
 
    height: 200px; 
 
    font-size: 14pt; 
 
    margin-top: 50px; 
 
    padding: 10px 10px 10px 10px; 
 
    vertical-align: top; 
 
} 
 
.textBox { 
 
    margin: 0; 
 
    text-align: center; 
 
} 
 
.buttons { 
 
    margin: 40px 20px 20px 40px; 
 
    font-size: 14pt; 
 
}
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
 
<form id="testForm"> 
 
    <textarea id="withoutPipes" autofocus="on" placeholder="Copy and paste the table here. Just Ctrl-V and submit"></textarea> 
 
</form> 
 

 
<button type="button" class="buttons" id="submit">Format table for JIRA</button>

+0

您可以張貼在的jsfiddle的樣本? – theoutlander

+0

當然,給我一點時間 – Jose

+0

嗨,@theoutlander,希望你能弄明白。提前致謝! – Jose

回答

0

這個怎麼樣? (打開你的瀏覽器的控制檯看到輸出)

excelText = "Letter\tNumber\tSymbol\nA\t1\t#\nB\t\t!\nC\t3\t%" 
 
// Letter \t Number \t Symbol 
 
// A \t 1 \t # 
 
// B \t 2 \t ! 
 
// C \t 3 \t % 
 

 
excelLines = excelText.replace(/\t\t/g, "\t \t").split("\n") 
 
header = "||" + excelLines[0].replace(/\t/g, "||") + "||" 
 
cells = "|" + excelLines.slice(1).join("|\n|").replace(/\t/g, "|") + "|" 
 

 
formattedTable = header + "\n" + cells 
 
console.log(formattedTable) 
 
// ||Letter||Number||Symbol|| 
 
// |A|1|#| 
 
// |B| |!| 
 
// |C|3|%|

幫助來自:
How to replace all occurrences of a string in JavaScript? http://www.w3schools.com/jsref/jsref_slice_array.asp http://www.w3schools.com/jsref/jsref_join.asp

+0

我會試一試 – Jose

+0

嘿喬西亞,它在每條線的開頭都缺少管道。 – Jose

+0

更改爲在每行的開頭添加管道(並且標題爲兩個);再試一次 –