CAP CDS Plugin Integration¶
The UI5 Spreadsheet Importer provides integration with SAP CAP (Cloud Application Programming Model) applications through its CDS plugin. This plugin allows for efficient handling of large Excel files by uploading the entire file directly to the CAP backend for processing, rather than extracting and sending data via standard UI5 APIs.
Overview¶
The CDS plugin offers the following benefits:
- Server-side processing: The entire Excel file is sent to the backend, where it's processed efficiently
- Performance optimization: Significantly improves performance for large Excel files
- Reduced network traffic: Only sends the Excel file once instead of multiple OData calls
- Seamless integration: Works with both UI5 Spreadsheet Importer components and direct file uploads
The source code is available here:
https://github.com/spreadsheetimporter/cds-spreadsheetimporter-plugin
Installation¶
To use the CDS plugin with your CAP application:
Backend Implementation¶
1. Add as dependency in package.json¶
Make sure the plugin is added to your CAP project's package.json:
{
"dependencies": {
"cds-spreadsheetimporter-plugin": "^1.0.3",
"@sap/cds": "^8.x",
// other dependencies
}
}
2. Import the plugin in your CAP service¶
Add the plugin to your service CDS file:
using { sap.capire.orders as my } from '../db/schema';
using from 'cds-spreadsheetimporter-plugin';
service OrdersService {
entity Orders as projection on my.Orders;
...
3. How the plugin works¶
The plugin adds a new service endpoint to your CAP application:
This endpoint:
- Accepts Excel file uploads via PUT request
- Processes the file server-side using the XLSX library
- Maps spreadsheet columns to entity properties
- Validates the data against the CDS entity model
- Inserts the processed data into the database
The CDS definition for the service is:
service ImporterService {
entity Spreadsheet {
key entity: String;
content: LargeBinary @Core.MediaType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
}
}
Integration Methods¶
There are two ways to use the CDS plugin: via the UI5 Spreadsheet Importer component or with a direct file upload.
Method 1: Using UI5 Spreadsheet Importer Component¶
Warning
This feature is available since version 2.1.0.
The simplest approach is to use the UI5 Spreadsheet Importer with the directUploadConfig
option:
<core:ComponentContainer
id="spreadsheetToCAP"
width="100%"
usage="spreadsheetImporter"
propagateModel="true"
async="true"
settings="{
directUploadConfig:{
enabled: true,
localhostSupport: true,
localhostPort: 4004,
useCdsPlugin: true
},
componentContainerData:{
buttonText:'Excel Upload with CDS Plugin',
buttonId:'uploadButton'
}
}"
/>
Method 2: Direct File Upload¶
You can also use a standard UI5 FileUploader to send the file directly to the CAP endpoint:
<u:FileUploader
id="fileUploader"
name="myFileUpload"
uploadUrl="/odata/v4/importer/Spreadsheet(entity='OrdersService.Orders')/content"
tooltip="Upload your file to the local server"
httpRequestMethod="Put"
sendXHR="true"
uploadOnChange="true"
style="Emphasized"
fileType="xlsx"
placeholder="Choose a file for Upload...">
</u:FileUploader>
Configuration Options¶
DirectUploadConfig Options¶
When using the UI5 Spreadsheet Importer component, the following directUploadConfig
options are available:
Option | Type | Default | Description |
---|---|---|---|
enabled | Boolean | false | Enables direct upload to the CAP backend |
useCdsPlugin | Boolean | false | Indicates that the CAP backend uses the CDS plugin |
localhostSupport | Boolean | false | Enables support for localhost development |
localhostPort | Number | 4004 | Port for localhost development |
prependPath | String | null | Custom path to prepend to the upload URL |
entity | String | Auto-detected | The CDS entity name for processing the upload |
collection | String | Auto-detected | The collection path for the entity |
service | String | Auto-detected | The service name |
Backend Processing Options¶
When calling the endpoint directly, you can include additional parameters:
Parameter | Description |
---|---|
entity | Required. CDS entity name to import data into |
Complete Example¶
Here's a complete example using UI5 Spreadsheet Importer with CDS plugin integration:
<core:ComponentContainer
id="spreadsheetToCAP"
width="100%"
usage="spreadsheetImporter"
propagateModel="true"
async="true"
settings="{
directUploadConfig:{
enabled: true,
useCdsPlugin: true,
localhostSupport: true,
localhostPort: 4004
},
componentContainerData:{
buttonText:'Excel Upload with CDS Plugin',
buttonId:'uploadButton'
}
}"
/>
Backend Implementation Details¶
The plugin's backend implementation:
- Receives the Excel file as a stream through the OData service
- Processes the file using the XLSX library
- Converts spreadsheet data to properly typed entity data
- Handles associations and relationships based on the CDS model
- Performs batch insertion into the database
The core processing logic: - Extracts sheet data using XLSX.read()
- Processes data from all sheets or a specific sheet - Maps column headers to CDS entity properties - Validates data types against CDS model - Inserts the processed data into the database