2.0.0 • Published 2 years ago

excel-creator-test v2.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 years ago

npm version code style: prettier

excel-creator

NPM Package for creating Excel with subtitles from nested objects.

Prerequisites

This project requires NodeJS (version 8 or later) and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
6.4.1
v8.16.0

First make sure you are able to run python3 (Mac/Linux) or python (Windows) from the terminal. If you are not then you might need to add it to the PATH. If you want to use a version of python not in the PATH you should specify pythonPath.

DockerFile requirements

Install git for the @types/xlsx-populate dependency

RUN apk add --no-cache git

Download python and pip and udpate pip

RUN apk add --no-cache python3 py3-pip && \ pip3 install --no-cache-dir --upgrade pip

Download required libraries

RUN python3 -m pip install --no-cache-dir json_excel_converter && \ python3 -m pip install --no-cache-dir xlsxwriter

Table of contents

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installation

BEFORE YOU INSTALL: please read the prerequisites

To install and set up the library, run:

$ npm install -S excel-creator

Or if you prefer using Yarn:

$ yarn add --dev excel-creator

Usage

createExcel

createExcel(path: string, workSheets: UserData[]): Promise<NodeJS.ReadableStream>
export interface ExcelFileStruct {
  fileName?: string;
  workSheets: UserData[];
}

type VerticalAlignment = 'top' | 'middle' | 'bottom';
type HorizontalAlignment = 'left' | 'center' | 'right';

export interface IExcelOptions {
  verticalAlignment?: VerticalAlignment;
  horizontalAlignment?: HorizontalAlignment;
  boldHeaders?: boolean;
  headersBottomBorder?: boolean;
}

export interface UserData {
  workSheetName: string;
  translation?: { [key: string]: string };
  data?: Record<string, any>;
}

Variables

  • path

Type
string

The path to save the file

  • worksheets
Type
UserData

The array containing the information aboute the sheets

Worksheets properties

workSheetName

TypeDefault value
string'sheet'

The name of the sheet

data

TypeDefault value
Record<string, any>'center'

The actual sheets text. Keys for headers and values for the values. If you want just one value the syntax is:

{ key: 'value' }

If you want just many values the syntax is:

{ key: ['value1','value2'] }

###merges ####horizontal merge: If you want subtitle for specific key the syntax is:

{ mainTitleName: { subTitleName: 'value'} }

You can add how many nested subtitles that you want. For better understanding look at the example.

####vertical merge: In order to achieve vertical merges you have to specify the key word #merge{number}# where the number is the number of cells you want to merge. The next object propery will be the only thing present in the merged cell. For example, if you have an array like so

    {title:['#merge3#', 'a', 'f' ]}

The resulting excel will look as follows: example excel

translation

TypeDefault value
translation: { beni: 'beni2', old: 'veryOld' }/undefinedundefined

Translate any key or value to another text.

options

Contain the next properties:

verticalAlignment

TypeDefault value
'top'/ 'middle' / 'bottom''top'

The vertical alignment of the text

horizontalAlignment

TypeDefault value
'left'/ 'center' / 'right''center'

The horizontal alignment of the text

boldHeaders

TypeDefault value
booleanfalse

Define if the headers are bold

headersBottomBorder

TypeDefault value
booleanfalse

Define if there is a line under the headers headers are bold

  • pythonPath
TypeDefault value
string / nullnull

If you want to use a version of python not in the PATH you should specify pythonPath.

Example:

import { createExcel } from  'excel-creator';
import  path  from  'path';

const  main = async () => {
  await  createExcel(
    path.resolve(__dirname, '../index.xlsx'),

    [
      {
        data: { names: { boys: { young: ['beni', 'itay'], old:  'mati' }, girls: ['a', 'c', 'e'] } },
        workSheetName:  'greatShit',
        translation: { beni:  'beni2', old:  'veryOld' },
        options: { verticalAlignment:  'top', horizontalAlignment:'center', boldHeaders:  true, headersBottomBorder:  true },
      },
    ],
  );
};

  

main();

result:

example excel

Credits

Binyamin Muller, Itay Tovim

Authors

  • Binyamin Muller - Initial work
  • Itay Tovim - Initial work

License

MIT License © Andrea SonnY

2.0.0

2 years ago