当我从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|%|
var submitBtn = true;
$("#reset").click(function () {
$("#withoutPipes").val("");
submitBtn = true;
});
$("#submit").click(function () {
if (submitBtn && $("#withoutPipes").val() != "") {
var excelText = $("#withoutPipes").val();
var excelLines = excelText.replace(/\t\t/g, "\t \t");
var split = excelLines.replace(/\t\t/g, "\t \t ").split("\n");
var header = "||" + split[0].replace(/\t/g, "||") + "||";
var cells = "|" + split.slice(1).join("|\n|").replace(/\t/g, "|") + "|";
var formattedTable = header + "\n" + cells;
$("#withoutPipes").val(formattedTable);
submitBtn = false;
}
});
#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 :(得分:0)
这个怎么样? (打开浏览器的控制台以查看输出)
excelText = "Letter\tNumber\tSymbol\nA\t1\t#\nB\t\t!\nC\t3\t%"
// Letter Number Symbol
// A 1 #
// B 2 !
// C 3 %
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|%|
&#13;
帮助来自:
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