1.0.1 • Published 4 years ago

exceljs-formatter v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Exceljs Formatter

npm (scoped)

excel-formatter

excel-formatter writes json object to excel file.

Usage

    var headers = ["name", "description", "heading"];
    var sample_json = [
      {
        "name": "Saurabh Pandey",
        "addr": [
          { "city": "560103", "area": "Bellundur", "pincode": "Bangalore", "state": "Karnataka" }
        ],
        "phone_no": [
          { "phone": "8090951024", "country_code": "+91" }
        ]
      },
      {
        "name": "Saurabh Pandey",
        "addr": [
          { "city": "560103", "area": "Bellundur", "pincode": "Bangalore", "state": "Karnataka" }
        ],
        "phone_no": [
          { "phone": "8009994420", "country_code": "+91" }
        ],
      }
    ];
    
    var _headerStyle = {
      fill: {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFFFF00' },
        bgColor: { argb: 'FF0000FF' }
      },
      font: { name: 'Calibri', family: 4, size: 12, bold: true },
      border: { top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' } }
    };
  
    var _cell_style = {
      alignment: { vertical: 'middle', horizontal: 'center', wrapText: true },
      border: { top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' } }
    };
    var config = {
      headers: headers, //(optional)
      headerStyle: _headerStyle, //(optional)
      subHeaderStyle: _headerStyle, //(optional)
      cellStyle: _cell_style, //(optional)
      maxCellLen: 30 //(optional)

    }
    console.log(excelGen.export_json(
        sample_json, "test", config
      ) 
    );

Styles

Fonts

// for the wannabe graphic designers out there
{ 
    font:{
        name: 'Comic Sans MS',
        family: 4,
        size: 16,
        underline: true,
        bold: true
    }
}
// Cells that share similar fonts may reference the same font object after
// the workbook is read from file or stream
Font PropertyDescriptionExample Value(s)
nameFont name.'Arial', 'Calibri', etc.
familyFont family for fallback. An integer value.1 - Serif, 2 - Sans Serif, 3 - Mono, Others - unknown
schemeFont scheme.'minor', 'major', 'none'
charsetFont charset. An integer value.1, 2, etc.
sizeFont size. An integer value.9, 10, 12, 16, etc.
colorColour description, an object containing an ARGB value.{ argb: 'FFFF0000'}
boldFont weighttrue, false
italicFont slopetrue, false
underlineFont underline styletrue, false, 'none', 'single', 'double', 'singleAccounting', 'doubleAccounting'
strikeFont strikethroughtrue, false
outlineFont outlinetrue, false
vertAlignVertical align'superscript', 'subscript'

Alignment

// set cell alignment to top-left, middle-center, bottom-right
{ alignment: { vertical: 'top', horizontal: 'left' } };

**Valid Alignment Property Values**

| horizontal       | vertical    | wrapText | shrinkToFit | indent  | readingOrder | textRotation |
| ---------------- | ----------- | -------- | ----------- | ------- | ------------ | ------------ |
| left             | top         | true     | true        | integer | rtl          | 0 to 90      |
| center           | middle      | false    | false       |         | ltr          | -1 to -90    |
| right            | bottom      |          |             |         |              | vertical     |
| fill             | distributed |          |             |         |              |              |
| justify          | justify     |          |             |         |              |              |
| centerContinuous |             |          |             |         |              |              |
| distributed      |             |          |             |         |              |              |

Borders

// set single thin border around A1
{ 
    border : {
        top: {style:'thin'},
        left: {style:'thin'},
        bottom: {style:'thin'},
        right: {style:'thin'}
    } 
}

Valid Border Styles

  • thin
  • dotted
  • dashDot
  • hair
  • dashDotDot
  • slantDashDot
  • mediumDashed
  • mediumDashDotDot
  • mediumDashDot
  • medium
  • double
  • thick

Fills

// fill A1 with red darkVertical stripes
{ 
    fill: {
        type: 'pattern',
        pattern:'darkVertical',
        fgColor:{argb:'FFFF0000'
    }
}

Pattern Fills

PropertyRequiredDescription
typeYValue: 'pattern'Specifies this fill uses patterns
patternYSpecifies type of pattern (see Valid Pattern Types below)
fgColorNSpecifies the pattern foreground color. Default is black.
bgColorNSpecifies the pattern background color. Default is white.

Valid Pattern Types

  • none
  • solid
  • darkGray
  • mediumGray
  • lightGray
  • gray125
  • gray0625
  • darkHorizontal
  • darkVertical
  • darkDown
  • darkUp
  • darkGrid
  • darkTrellis
  • lightHorizontal
  • lightVertical
  • lightDown
  • lightUp
  • lightGrid
  • lightTrellis

Gradient Fills

PropertyRequiredDescription
typeYValue: 'gradient'Specifies this fill uses gradients
gradientYSpecifies gradient type. One of 'angle', 'path'
degreeangleFor 'angle' gradient, specifies the direction of the gradient. 0 is from the left to the right. Values from 1 - 359 rotates the direction clockwise
centerpathFor 'path' gradient. Specifies the relative coordinates for the start of the path. 'left' and 'top' values range from 0 to 1
stopsYSpecifies the gradient colour sequence. Is an array of objects containing position and color starting with position 0 and ending with position 1. Intermediary positions may be used to specify other colours on the path.
1.0.1

4 years ago

1.0.0

4 years ago