skip to Main Content

We prepared following script to download data into CSV file:

// Variable to store the final csv data
    var csv = [];
    // Get each row data
    var rows = document.querySelectorAll("table tr");
    // Get each column data
    for(var i = 1; i < rows.length; i++){
            // Stores each csv row data
            var row = [], cols = rows[i].querySelectorAll("td,th");
            for(var j = 0; j < cols.length; j++){
                // Get the text data of each cell
                // of a row and push it to csvrow
                row.push('"' + cols[j].innerText.trim() + '"');                
            }
            // Combine each column value with comma
            csv.push(row.join(",") + "n");
    }

The problem is that we have a comma at the beginning of each line. Could you please advise how can we fix our code to remove this comma? Thank you! Sample output:

"Host","Version","Country","Brand","Store"
,"test1","5.2","MX","brand","1234"
,"test2","5.2","CA","brand","4321"
,"test3","5.2","US","brand","4444"

Desired output:

"Host","Version","Country","Brand","Store"
"test1","5.2","MX","brand","1234"
"test2","5.2","CA","brand","4321"
"test3","5.2","US","brand","4444"

3

Answers


  1. The commas you see are probably the commas of the list.

    you can use this to get it with no commas

    let csvRawData = csv.join('');
    console.log(csvRawData);
    
    Login or Signup to reply.
  2. The issue you’re facing is caused by the fact that you’re adding a comma before each column value in the row.push statement.
    To fix this, you can modify the code to handle the first column differently, making sure that a comma is not added before the first value in each row.

    Try use

    row.push(j === 0 ? '"' + cols[j].innerText.trim() + '"' : ',"' + cols[j].innerText.trim() + '"')
    

    Instead of

    row.push('"' + cols[j].innerText.trim() + '"')
    
    Login or Signup to reply.
  3. Your push is adding the comma

    This is a lot simpler

    const csv = [...document.querySelectorAll("table tr")]
      .map(row => [...row.querySelectorAll("td,th")]
        .map(cell => `"${cell.textContent.trim().replace(/"/g,'""')}"`)
        .join(',')) // cells
      .join('n');   // rows
    console.log(csv);
    <table>
      <thead>
        <tr>
          <th>Host</th>
          <th>Version</th>
          <th>Country</th>
          <th>Brand</th>
          <th>Store</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>test1</td>
          <td>5.2</td>
          <td>MX</td>
          <td>brand "X"</td>
          <td>1234</td>
        </tr>
        <tr>
          <td>test2</td>
          <td>5.2</td>
          <td>CA</td>
          <td>brand</td>
          <td>4321</td>
        </tr>
        <tr>
          <td>test3</td>
          <td>5.2</td>
          <td>US</td>
          <td>brand</td>
          <td>4444</td>
        </tr>
      </tbody>
      <table>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search