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

cube-derive-util

v0.0.2

Published

Util facilitate to export data as file

Downloads

4

Readme

文件下载功能组件 cube-derive-util

cube-derive-util将需要下载的数据转成相应文件格式的Buffer数据,并且携带header。通过浏览器解析返回的Buffer数据,完成文件下载。

目录

安装

  • 进入自己的项目的 server 目录,使用 yarn add cube-derive-util 安装服务端依赖。

参数

downloadFile(
  reply,
  {
    dataBody,
    header,
    type,
    options
  });

| 参数项 | 解释 | 默认值 | | ----- | ------ | ------ | | reply | routes 中 handler 的 reply | | | dataBody | 需要导出的数据 | 无 | | header | 指定导出文件的文件头 | 无 | | type | 指定导出文件类型 | csv | | options | 配置文件名,目前只有excel可配置行高列宽 | | options.wsrows | 设置每一行的高度,单位为 px | 10像素 | | options.wscols | 设置每一列的宽度,单位为 px | 100像素 | | options.fileName | 设置导出文件的文件名 | knownsec |

注意事项

  • header 中的数据项 text 中英文都可以,只需要满足 headerdataBody 中的 key 对应即可。

  • headerdataBody存在关联,必须满足一定的数据格式,dataBody数组中的每一个对象的keyheader中的元素,如下:

header: [
  {
    key: 'name'
    text: '姓名'
  },
  {
    key: 'age'
    text: '年龄'
  },
  {
    key: 'gender'
    text: '性别'
  },
  {
    key: 'enjoy'
    text: '爱好'
  },
]
dataBody: [
  {
    'name': 'zhangxueyou',
    'age': '18',
    'gender': 'male',
    'enjoy': 'basketball'
  },
  {
    name: 'liudehua',
    age: '18',
    gender: 'female',
    enjoy: 'baseball'
  },
  ......
]

实例

  • 首先,这个方法直接在你的controller中 import ,传入相应参数,即可想用文件下载的功能。

  • 引入方法

    import downloadFile from 'cube-derive-util';
  • 使用

    // 在某一个 controller 中,将查询到的数据导出为文件
    export const outuputCountries = async(request, reply) => {
        
      const { type } = request.query; // type='xlsx'
      // 假如现在我们已经通过一些查询接口得到类似于上面的数据
      downloadFile(reply, {
        dataBody,
        header,
        type,
        options: {
          fileName: '全球国家名字',
        },
      });
    };
  • 结果

    你将会下载一个 全球国家名字.xlsx 文件,文件内容如下:

| 姓名 | 年龄 | 性别 | 爱好 | | ----- | ------ | ------ | ------ | | zhangxueyou | 18 | male | basketball | | liudehua | 18 | female | baseball |

支持功能

  • 目前只可以支持将数据导出成csvxslx文件格式。

贡献者

首席维护

核心开发