npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-data-to-csv

v1.0.10

Published

Ngx Data To CSV is an Angular library that provides a service to transform data into CSV format. It offers a set of methods for converting data (in the form of an object or a JSON string) into CSV format, extracting values from nested objects, extracting

Downloads

93

Readme

Ngx Data To CSV Documentation

Ngx Data To CSV is an Angular library that provides a service to transform data into CSV format. It offers a set of methods for converting data (in the form of an object or a JSON string) into CSV format, extracting values from nested objects, extracting unique keys from an array of objects, parsing a JSON string into an object, formatting cell values for inclusion in a CSV file, and initiating the download of a CSV file.

The library is designed to handle various complexities that arise when dealing with data structures, including nested objects and arrays. It automates the process of generating CSV files, allowing developers to focus on their data processing logic rather than worrying about the intricacies of CSV formatting.

Installation

To install the Ngx Data To CSV package, use the following command in your console:

ng add ngx-data-to-csv

or

npm install ngx-data-to-csv

How to use

To use this service, you need to import and inject it into your Angular component.

  1. Import NgxDataToCsvService into your component:
import { NgxDataToCsvService } from 'ngx-data-to-csv';
  1. Inject the service into the constructor of your component:
constructor(private csvService: NgxDataToCsvService) {}

or

csvService: NgxDataToCsvService = inject(NgxDataToCsvService);
  1. Use the toCsv(data, filename, config?) method to transform your data into CSV. This method takes three arguments:

    • data: the data you want to transform into CSV.
    • filename: the name of the CSV file to be created.
    • config?: an optional configuration object.

Here's an example of how to use the service in a component:

import { Component } from '@angular/core';
import { NgxDataToCsvService } from 'path/to/ngx-data-to-csv';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  data = [
    { lastName: 'Smith', firstName: 'John', age: 30 },
    { lastName: 'Doe', firstName: 'Jane', age: 25 },
  ];

  constructor(private csvService: NgxDataToCsvService) {}

  downloadCsv() {
    this.csvService.toCsv(this.data, 'myData');
  }
}

In this example, we have an array of data data that we want to transform into CSV. We injected NgxDataToCsvService into the constructor of our component, and we created a downloadCsv method that calls csvService.toCsv with our data and the desired filename.

Configuration

The optional configuration object that you can pass to the toCsv method can contain the following keys:

  • filename: the name of the CSV file. By default, it is 'csv.csv'.
  • fieldSeparator: the field separator used in the CSV. By default, it is ','.
  • showTitle: a boolean that determines whether the title should be displayed at the top of the CSV. By default, it is false.
  • title: the title of the CSV. By default, it is 'CSV'.
  • useByteOrderMark: a boolean that determines whether a byte order mark should be used. By default, it is true.
  • noDownload: a boolean that determines whether the CSV should be downloaded or simply returned as a string. By default, it is false.

Here's an example of how to use it with configuration:

this.csvService.toCsv(this.data, 'myData', { showTitle: true, title: 'My Data', fieldSeparator: ';' });

In this example, the generated CSV file will have 'My Data' as the title and the fields will be separated by ';'.