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

@johnbiiibs/cds-excel-uploader

v1.1.3

Published

A CAP Plugin for Excel Uploads

Downloads

35

Readme

@johnbiiibs/cds-excel-uploader

This is a personal project, hence there is no offical support. Feel free to use it though, and open issues and I will try my best to fix any. Cheers!

This package aims to provide utility middlewares / handlers to simplify the process of handling Excel (XLSX) uploads in the SAP Cloud Application Programming Model (CAP). It takes in an Excel file then parses it into a JSON Object.

This package is built on top of SheetJS.

Install

npm i @johnbiiibs/cds-excel-uploader

Usage

The package comes with two (2) middleware utility functions: one as a CAP CDS Handler, and another as an Express Middleware.

CAP Handler

This method is inspired by the following blog on community.sap.com. Please check out the original blog: Upload data from excel in CAP (Node.js)

Start by projecting the ExcelUpload entity from the ExcelUploadService. This entity is a Singleton entity (@odata.singleton).

// service.cds
using { ExcelUploadService } from '@johnbiiibs/cds-excel-uploader';

service MyService {
    entity ExcelUpload as projection on ExcelUploadService.ExcelUpload;
}

Then get the parseExcel utility function from the CDS Middleware, and attach it to the entity.

// service.js

const { cds: middleware } = require("@johnbiiibs/cds-excel-uploader");
const { parseExcel } = middleware;

module.exports = async srv => {
    const { ExcelUpload } = srv.entities;

    // handler for the excel file
    srv.on("PUT", ExcelUpload, parseExcel());

    // handler for the parsed excel data
    srv.on("PUT", ExcelUpload, async (req, next) => {
        // 'req.data.fileData' contains the excel data
    });
};

Sample UI5 file uploader for this implementation.

// FileUploader.fragment.xml (UI5)

<u:FileUploader
    id="uploader"
    name="myFileUpload"
    uploadUrl="/srv-api/excel-upload-srv/ExcelUpload/file"
    width="100%"
    tooltip="Upload your file to the local server"
    sendXHR="true"
    useMultipart="false"
    uploadStart="handleUploadStart"
    uploadComplete="handleUploadComplete"
    httpRequestMethod="Put"
/>

Express Middleware

This middleware focuses integration through the server.js file as an Express.js Middleware.

This method has a dependency on the express-fileupload package.

// server.js
const fileUpload = require("express-fileupload");
const { express: middleware } = require("@johnbiiibs/cds-excel-uploader");
const { parseExcel } = middleware;

cds.on("bootstrap", (app) => {
    app.post("/upload", fileUpload(), parseExcel(), (req, res, next) => {
        // 'req.fileData' contains the excel data
        res.json(req.fileData);
    });
});

Sample UI5 file uploader for this implementation.

// FileUploader.fragment.xml (UI5)

<u:FileUploader
    id="uploader"
    name="myFileUpload"
    uploadUrl="/upload"
    width="100%"
    tooltip="Upload your file to the local server"
    sendXHR="true"
    fileType="xls,xlsx"
    uploadComplete="handleUploadComplete"
/>

Parsing Options

As mentioned, this package is heavily dependent on SheetJS, particularly the XLSX.utils.sheet_to_json utility function.

As such, its options argument can also be applied to the parseExcel method. By default, the package uses this configuration:

const options = {
    cellText: true,
    cellDates: true,
    dateNF: 'dd"."mm"."yyyy',
    rawNumbers: false,
    defval: ""
}

Sample CAP Implementation

// Same code as the sample above...

// Sample options.
// This would overwrite the default.
const options = {
    cellDates: false,
    rawNumbers: true,
    defval: "X"
}

// handler for the excel file
srv.on("PUT", ExcelUpload, parseExcel(options));

Sample Express Implementation

// Same code as the sample above...

// Sample options.
// This would overwrite the default.
const options = {
    cellDates: false,
    rawNumbers: true,
    defval: "X"
}

cds.on("bootstrap", (app) => {
    app.post("/upload", fileUpload(), parseExcel(options), (req, res, next) => {
        // 'req.fileData' contains the excel data
        res.json(req.fileData);
    });
});

Support

Please use the GitHub bug tracking system to post questions or to report bugs.