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

js-xlsx-util

v1.0.0

Published

A simple utility collection for js-xlsx.

Downloads

8

Readme

js-xlsx-util

A simple utility collection for js-xlsx.

Installation

With npm:

npm install js-xlsx-util --save

Usage

var _ = require('js-xlsx-util');

Methods

Parse Methods

xlsx文件进行读取和解析的方法

  • _.readFile(xlsxPath, noCache):读取文件,自动执行_.formatCell方法,对workbook进行格式化。默认进行缓存,第二个参数设置为true的话表示不进行缓存

    当文件过大的时候,每次多去都会比较耗时,如果源文件不会经常变动的话,建议使用缓存。

  • _.formatCell(workbookOrWorksheet):对workbookworksheet进行格式化,格式化之后的单元格对象会新增col列和row行属性,同时也会增加top(gap),right(gap),bottom(gap),left(gap)四个方法用于获取间隔gap的单元格,默认是相邻的。

    cellA5.top() === cellA4
    cellA5.bottom(2) === cellA7
    cellC3.left() === cellB3
    cellD4.right(4) === cellI4.left()
  • _.getPrevCol(colOrCell):获取某一列的上一列序号,也可以传入一个格式化之后的单元格

    _.getPrevCol('DF') // DE
    var formatCell = {
    	t:'n',
    	v:1234,
    	col:'SA' // 格式化之后才有这个属性
    }
    _.getPrevCol(formatCell) // 'QZ'
  • _.getNextCol(colOrCell):获取某一列的下一列序号,用法同_.getPrevCol(colOrCell)

  • _.each(worksheet, iterator):遍历sheet中的所有单元格,迭代器的参数是单元格位置,属性和sheet

    _.each(worksheet,function(key,value,sheet){
    	// key = 'SA12'
    	// value = {t:'n',v:123 ... }
    });
  • _.filter(worksheet, iterator):遍历sheet并进行过滤,根据iterator函数的执行结果确定是否符合条件,返回符合条件的单元格的数组

    var result = _.filter(worksheet,function(key,value,sheet){
    	return value.t === 'n'; // 过滤所有的数字单元格
    });
    // result [{
    	t:'n',
    	...
    },{
    	t:'n',
    	...
    }]
  • _.formatKey(key):对单元格的位置(键)进行格式化,返回一个包含行和列的对象

    _.formatKey('BS23') // {row:23,col:'BS'}

Writing Methods

输出文件的时候相关工具方法

  • _.writeFile(filepath, workbook):输出文件,和xlsx.writeFile类似,参数位置不太一样。

  • _.buildRef(worksheet):为sheet生成!ref属性值,因为在使用xlsx输出文件的时候如果没有该属性是无法生成表格的。如果已经有了!ref属性,则不再生成

  • _.cell(value):根据value的类型生成单元格对象

    _.cell(234)    // {t:'n', v: 234}
    _.cell('str')  // {t:'s', v: 'str'} 
  • _.addRow(worksheet, rowObj):为sheet中添加一行,会自动根据!ref(如果没有的话使用_.buildRef()生成)自动在最后一行之后追加新的一行,可以指定列及其对应的值

    var worksheet = {
    	A1:...
    	B1:...
    };
    _.addRow(worksheet, {
    	A:'str',
    	B:123,
    	C:{t:'n',v:890}
    });
    console.log(worksheet); 
    /*
    {
    	A1:...
    	B1:...
    	A2:{t:'s',v:'str'},
    	B2:{t:'n',v:123},
    	C2:{t:'n',v:890}
    }
    */
  • _.addWorkSheet(workbook, sheetName, worksheet):将sheet添加到workbook

    var workbook = {};
    _.addWorkSheet(workbook,'Sheet1', worksheet);
    console.log(workbook);
    /*
    {
    	Sheets:{
    		'Sheet1':worksheet
    	},
    	SheetNames:['Sheet1']
    }
    */

Others

其他方法

  • _.isEqual(valA,valB):判断两个值是否一样,经过转化字符串,然后去除首尾空格,因为有些单元格中首尾是有空格的。
  • _.isUndefined(value):判断一个值是否是undefined
  • _.isDefined(value):判断一个值是否是定义的

License

MIT