2

When I copy data from Excel and paste onto a word document. They have "\t" in between each word & "\n" at the end of the line, it looks like this:

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

Thanks Joasih for all your help. the missing piece of this puzzle is that if there is no data, it needs to have a space.

Without pipes:

Letter  Number  Symbol
A   1   #
B       !
C   3   %

With pipes:

||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>
Jose
  • 370
  • 1
  • 4
  • 14

1 Answers1

0

How about this? (open your browser's console to see the output)

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|%|

Help from:
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

Community
  • 1
  • 1
Josiah Krutz
  • 957
  • 6
  • 16
  • Hey Josiah, it was missing the pipes at the beginning of each line. – Jose Oct 16 '15 at 03:34
  • Changed to add pipes at the beginning of each line (and two for the header); try it again – Josiah Krutz Oct 16 '15 at 03:40
  • Hi @Josiah, I updated the question in hopes that you can solve the last piece of the puzzle. You're 2 for 2! – Jose Oct 16 '15 at 04:06
  • I think I fixed it. \t\t now gets chagned to \t \t. Let me know if this works for you! – Josiah Krutz Oct 16 '15 at 04:15
  • Somehow it's getting hung up. I also tried to add "var noSpace = cells.replace(/||/g,"| |")", but this didn't work either. I updated the question to reflect your latest suggestion – Jose Oct 16 '15 at 04:33
  • Try taking out the extra "$("#submit").click(function () { " in your Code Snippet – Josiah Krutz Oct 16 '15 at 04:38
  • unfortunately I wasn't able to run the code. I updated the snippet above. When I press on the button nothing happens. – Jose Oct 16 '15 at 04:39
  • I believe there is a syntax error in your Javascript. See previous comment /\ – Josiah Krutz Oct 16 '15 at 04:40
  • I removed it, but nothing happened. Will create a function with your code to see what happens – Jose Oct 16 '15 at 04:40
  • Yes, I think there is another issue... sorry about that; I'm looking into it. – Josiah Krutz Oct 16 '15 at 04:42
  • Found it: Take out the '.split("\n")' at the end of 'var excelText = $("#withoutPipes").val().split("\n");' – Josiah Krutz Oct 16 '15 at 04:45
  • tried with a function, but it fails as well. Any clues? – Jose Oct 16 '15 at 04:49
  • Praise the Lord! You may want to update your question with the working example javascript. Thanks! – Josiah Krutz Oct 16 '15 at 04:54
  • Done. BTW, I had to add the following to ensure that all the pipes had spaces in between ---var excelLines = excelText.replace(/\t\t/g, "\t \t");---before you then I added yours --- var split = excelLines.replace(/\t\t/g, "\t \t ").split("\n");--- btw, I'm not claiming that I came up with this. @Josiah was the one that solved this problem. Many thanks! – Jose Oct 18 '15 at 07:28