Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

html table to csv

// Quick and simple export target #table_id into a csv
function download_table_as_csv(table_id, separator = ',') {
    // Select rows from table_id
    var rows = document.querySelectorAll('table#' + table_id + ' tr');
    // Construct csv
    var csv = [];
    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll('td, th');
        for (var j = 0; j < cols.length; j++) {
            // Clean innertext to remove multiple spaces and jumpline (break csv)
            var data = cols[j].innerText.replace(/(
|
|
)/gm, '').replace(/(ss)/gm, ' ')
            // Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv)
            data = data.replace(/"/g, '""');
            // Push escaped string
            row.push('"' + data + '"');
        }
        csv.push(row.join(separator));
    }
    var csv_string = csv.join('
');
    // Download it
    var filename = 'export_' + table_id + '_' + new Date().toLocaleDateString() + '.csv';
    var link = document.createElement('a');
    link.style.display = 'none';
    link.setAttribute('target', '_blank');
    link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string));
    link.setAttribute('download', filename);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript loop types 
Typescript :: typescript object type 
Typescript :: loop two lists python 
Typescript :: components of cucumber bdd framework 
Typescript :: Lists - Learn C# 
Typescript :: typescript get the promise return type 
Typescript :: typescript array of string array 
Typescript :: angular build router-outlet not working 
Typescript :: conda tsinghua 
Typescript :: stop camera if it hits edge of room gml 
Typescript :: ignore hosts option in network proxy in ubuntu 16.04 
Typescript :: java check if element exists in array 
Typescript :: how to check if a value exists in unorderedmaps 
Typescript :: java delete contents of file 
Typescript :: How to pass optional parameters while omitting some other optional parameters? 
Typescript :: i comparer for lists c# 
Typescript :: ts Strategy pattern 
Typescript :: rest parameters in typescript 
Typescript :: key with variable name in typescript 
Typescript :: select2 .select2-results .select2-highlighted 
Typescript :: conditional type typescript 
Typescript :: angular material chips autocomplete example 
Typescript :: typeorm transaction example 
Typescript :: datasets in python github 
Typescript :: typescript to c# converter 
Typescript :: tsc : File C:UsersajayAppDataRoaming pm sc.ps1 cannot be loaded because running scripts is disabled on this system. 
Typescript :: idle angular 15 menute 
Typescript :: elastice search requirements in ubunt 
Typescript :: typescript event emitter 
Typescript :: scss all elements inside 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =