title: Number table rows in Google Doc using Apps Script
body: <p>I'm working on a script which takes text and places each paragraph in a numbered table cell. I'm running into a problem where each line break is counted as a paragraph by the script, which means my table either has empty cells or is numbered incorrectly.</p>
  
  <p>Here's the working script (minus row numbering):</p>
  
  <pre class="lang-js prettyprint-override"><code>function formatArticle() {
    var doc = DocumentApp.getActiveDocument()
    var body = doc.getBody(); 
  
    // Get the paragraphs
    var paras = body.getParagraphs();
  
    // Add a table to fill in with copied content
    var addTable = body.appendTable();
    for (var i=0;i&lt;paras.length;++i) {
  
      // If the paragraph is text, add a table row and insert the content.
      if(i % 2 == 0) {
        var tr = addTable.appendTableRow();
        var text = paras[i].getText();
  
        // Number the table rows as they're added in a cell to the left.
        for(var j=0;j&lt;2;j++) {
          if(j == 0) {
            var td = tr.appendTableCell(i);
            } else {
              var td = tr.appendTableCell(text);
            }
          }
        }
  
      // Shrink left column.
      addTable.setColumnWidth(0, 65);
  
      // Delete the original text from the document.
      paras[i].removeFromParent();
    }
  }
  </code></pre>
  
  <p><strong><a href="https://docs.google.com/document/d/1g8sCdgY8MPON7_Gh7KW9rS2tFXdBwQrzm3CuH_UARhw/edit?usp=sharing" rel="nofollow">Here's a demo Google Doc</a></strong> so you can see how text formats without setting a new one up yourself if that helps.</p>