19 KiB
title | pagination_prev | pagination_next |
---|---|---|
Oracle Cloud Services | demos/local/index | demos/extensions/index |
import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';
Oracle Cloud Services (OCI) is a cloud services platform which includes traditional virtual machine support, "Serverless Functions" and cloud storage.
SheetJS is a JavaScript library for reading and writing data from spreadsheets.
This demo demonstrates how to integrate SheetJS with Oracle Visual Builder Studio (VBS)
1
to build a web application that handles Excel files. You'll learn to create an application
that can import spreadsheet data, store it in Business Objects
2, display it in a table, and
export it back to Excel format.
:::caution pass
Oracle Cloud Services iterates quickly and there is no guarantee that the referenced services will be available in the future.
:::
:::note Tested Deployments
This demo was last tested on 2025-02-04.
:::
Visual Applications
Oracle Cloud Services offers Visual Builder Studio a browser-based development environment for creating web applications with drag-and-drop components.
Installation
SheetJS libraries conform to the Oracle Cloud Visual Builder Studio (VBS) ECMAScript3 requirements. SheetJS libraries can be loaded in Oracle VBS site at different points in the app lifecycle.
Configure in app-flow.json
The app-flow.json
method is recommended as it aligns better with Visual Builder Studio's
module management system.
- Click 📄
Source
from the left sidebar - Open
webApps/sheetjs_demo_app/app-flow.json
- Add the following configuration after line 5:
{\ "requirejs": { "paths": { "xlsx": "https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js" } }
}
HTML
Oracle Cloud VBS is typically loaded in SCRIPT
tag in webApps/sheetjs_demo_app/index.html
. Similarly,
SheetJS Standalone scripts
can be loaded with a SCRIPT
tag in the same HTML page:
{`\
`}
This will expose the XLSX
global object, which includes the functions listed
in the "API Reference" section of the documentation.
Internal State
The various SheetJS APIs work with various data shapes. The preferred state depends on the application.
Business Objects
Business Objects
2 in Oracle VBS provides a server-side model implementation for persistent data storage.
Think of them as a combination of a data model and database table that handles both data storage and
UI binding.
Business Object Definition
The example presidents sheet has one header row with "Name" and "Index" columns. In Oracle VBS, we model this as a Business Object with corresponding fields:
Spreadsheet | Business Object Definition |
---|---|
|
Here is how the data is represented when queried from the Business Object via REST call:
{
"items": [
{ "name": "Bill Clinton", "index1": 42 },
{ "name": "GeorgeW Bush", "index1": 43 },
{ "name": "Barack Obama", "index1": 44 },
{ "name": "Donald Trump", "index1": 45 },
{ "name": "Joseph Biden", "index1": 46 }
]
}
Updating Business Object
Starting from a spreadsheet file, the SheetJS read
method parses the data into a workbook object. After
selecting a worksheet, sheet_to_json
generates row objects that can be created as Business Object
records using the auto-generated REST API.
Here is a sample flow diagram and method for downloading a workbook, generating rows from the first worksheet, and updating a Business Object
2:
flowchart LR
xlsx[(Remote\nFile)]
bo1[(Data\nArrayBuffer)]
wb(SheetJS\nWorkbook)
ws(SheetJS\nWorksheet)
json(JSON\nData)
bo2[(Business\nObject)]
xlsx --> |fetch\n\n| bo1
bo1 --> |XLSX.read\n\n| wb
wb --> |wb.Sheets\nselect| ws
ws --> |sheet_to_json\n\n| json
json --> |REST API\ncreate| bo2
linkStyle 1,2,3 color:blue,stroke:blue;
{` define(['vb/action/actionChain', 'vb/action/actions', 'xlsx'], (ActionChain, Actions, XLSX) => { class ButtonActionChain extends ActionChain { async run(context) { // fetch XLSX file const data = await (await fetch("https://docs.sheetjs.com/pres.xlsx")).arrayBuffer(); const wb = XLSX.read(data); // get the first worksheet const ws = wb.Sheets[wb.SheetNames[0]]; // convert to json const jsonData = XLSX.utils.sheet_to_json(ws);
// process each row and create business object records
for (const row of jsonData) {
const presData = {
name: row.Name,
index1: Number(row.Index)
};
// update business object with new record
await Actions.callRest(context, {
endpoint: 'businessObjects/create_Pres',
body: presData
});
}
}
}
return ButtonActionChain;
}); `}
Rendering Data
In Oracle VBS, the table component oj-table
supports direct binding to Business Objects for data display.
The view structure defines both the data source binding and column layout:
The following example uses the Table
4 component to display data.
{`\
Exporting Data
The export functionality converts Business Object data into an Excel spreadsheet. The process involves retrieving data through REST API call, transforming it into a format suitable for SheetJS, and generating an XLSX file. Here's the complete flow:
flowchart LR
bo[(Business\nObject)]
rest(REST API\nCall)
json(JSON\nArray)
ws(SheetJS\nWorksheet)
wb(SheetJS\nWorkbook)
xlsx[(XLSX\nFile)]
bo --> |getall_Pres\nendpoint| rest
rest --> |transform\nresponse| json
json --> |json_to_sheet\n\n| ws
ws --> |book_new\n\n| wb
wb --> |writeFileXLSX\n\n| xlsx
linkStyle 2,3,4 color:blue,stroke:blue;
{` define(['vb/action/actionChain', 'vb/action/actions', 'xlsx'], (ActionChain, Actions, XLSX) => { class ButtonActionChain1 extends ActionChain { async run(context) { // get pres data const presDataResponse = await Actions.callRest(context, { endpoint: 'businessObjects/getall_Pres', parameters: { fields: 'name,index1' } });
// transform to simple array
const simplifiedData = presDataResponse.body.items.map(item => ({
Name: item.name,
Index: item.index1
}));
// generate workbook
const ws = XLSX.utils.json_to_sheet(simplifiedData);
const wb = XLSX.utils.book_new(ws, "Presidents");
// export to XLSX which triggers a download
XLSX.writeFileXLSX(wb, "SheetJSOracleVisualBuilderAoO.xlsx");
}
}
return ButtonActionChain1;
}); `}
:::note pass
Visual Builder Studio provides a visual development environment and handles the build and deployment process automatically.
:::
Importing Data
The import functionality enables data transfer from an Excel spreadsheet into your Oracle VBS application. The process begins by fetching the XLSX file, parsing it using SheetJS, and then creating corresponding Business Object records.
define([
'vb/action/actionChain',
'vb/action/actions',
'vb/action/actionUtils',
'xlsx'
], (
ActionChain,
Actions,
ActionUtils,
XLSX
) => {
'use strict';
class ButtonActionChain extends ActionChain {
/**
* @param {Object} context
*/
async run(context) {
const { $page, $flow, $application, $constants, $variables } = context;
// fetch XLSX file
const url = "https://docs.sheetjs.com/pres.xlsx";
const data = await (await fetch(url)).arrayBuffer();
const wb = XLSX.read(data);
// get the first worksheet
const ws = wb.Sheets[wb.SheetNames[0]];
// convert to json
const jsonData = XLSX.utils.sheet_to_json(ws);
// process each row and create business object records
for (const row of jsonData) {
const presData = {
name: row.Name,
index1: Number(row.Index)
};
// create a business object record
await Actions.callRest(context, {
endpoint: 'businessObjects/create_Pres',
body: presData
});
}
}
}
return ButtonActionChain;
});
Exporting Data
The export functionality allows you to convert Business Object data into Excel spreadsheets. This process involves retrieving data from the Business Object using REST API calls, transforming it into a format suitable for SheetJS, and generating an XLSX file.
define([
'vb/action/actionChain',
'vb/action/actions',
'vb/action/actionUtils',
'xlsx'
], (
ActionChain,
Actions,
ActionUtils,
XLSX
) => {
'use strict';
class ButtonActionChain1 extends ActionChain {
/**
* @param {Object} context
*/
async run(context) {
const { $page, $flow, $application, $constants, $variables } = context;
// get pres data
const presDataResponse = await Actions.callRest(context, {
endpoint: 'businessObjects/getall_Pres',
parameters: {
fields: 'name,index1'
}
});
// transform to simple array
const simplifiedData = presDataResponse.body.items.map(item => ({
Name: item.name,
Index: item.index1
}));
// generate workbook
const ws = XLSX.utils.json_to_sheet(simplifiedData);
const wb = XLSX.utils.book_new(ws, "Presidents");
/* export to XLSX */
XLSX.writeFileXLSX(wb, "SheetJSOracleVisualBuilderAoO.xlsx");
}
}
return ButtonActionChain1;
});
Oracle Visual Builder Studio Web Applications Demo
:::note pass
At the time of writing, Oracle Cloud offers a 30-day free trial that includes Visual Builder Studio access.
:::
- If you do not have an account create a new Oracle Cloud free tier account5.
Visual Builder Setup
-
Sign in to the Oracle Cloud Management Console with your created account.
-
Type "Visual Builder Studio" in the top search box and click Visual Builder Studio (under services).
- Before creating Visual Builder Studio Instances we need to create an instance. Click
Visual Builder
from the left sidebar and click Create an instance.
- Give it a name:
sheetjs-demo-vb-instance
- Select network access:
Default (No access rules.)
- Nodes:
1
- Now click
Visual Builder Studio
from the left sidebar and clickCreate Visual Builder Studio
and give it instance name:sheetjs-vbs-instance
and click next and set select compartment toyourusername (root)
. Lastly, clickCreate Visual Builder Studio
(this will take 5-15 minutes to fully setup).
- Click link name
sheetjs-vbs-instance
- Click
Service Console
create a new project and fill it with the following inputs.
- Project Details:
sheetjs-demo-project
- Project Template:
Empty Project
- Project Properties:
Markdown
- Click Finish
- Select
Project Home
from the left sidebar and create an environment.
- Environment Name:
sheetjs-demo-env
- Click Create
- Click
Add Instance
-
In Environments select the Service Instances tab and search for
Instance URL
if not shown click details to expand Visual Buildersheetjs-demo-vb-instance
and openInstance URL
This will open Visual Builder Studio. -
Click
New Application
and set
- Application Display Name:
sheetjs-vbs-demo
- Application Template:
Empty Application
- Click Finish
- Create a new Business Object by clicking Business Object card in the Welcome tab or from the sidebar as shown
- Name:
Pres
- Display:
President
- Click
Pres
Business Object and select Fields tab. Then, create a new field by clicking the+Fields
button and select first drop-downField
.
-
Label1:
Name
-
Type1: Click
A
(String
) -
Label2:
Index
-
Type2: Click
#
(Number
)
- Click 🖥️ Web Applications from the left sidebar and create a new Web Application by clicking
+Web Application
You should have the following fields.
- Application Name:
sheetjs_demo_app
- Navigation Style:
None
- Click Create
- Adding SheetJS Module to the App Click 📄
Source
from the left sidebar and paste the following onwebApps/sheetjs_demo_app/app-flow.json
{\ "requirejs": { "paths": { "xlsx": "https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js" } }
}
- Let's create the UI for our application we will be creating a table and two buttons one for import XLSX and one for export XLSX.
- From the left sidebar click the 🖥️
Web Applications
and selectmain-start
- Select Page Designer and drag and drop two
Button
components and oneTable
Component to the canvas
- Creating our button event handler
- Now select the left button in the canvas and on the right side on
General
tab set ID toimport-xslx
and label toImport XLSX
. - Now select the
Events
tab and click+ Event Listener
drop down button and selectOn 'ojAction'
- Now it should auto-select
Action Chains
if not click from top tab option. - Switch from
Design
toCode
from top right
-
Replace
Action Chains > ButtonActionChain > Code
with the importing data snippet in the "Importing Data" example code. -
Now repeat step 14 for the second button
- Now select the first button in the canvas and on the right side on
General
tab set ID toexport-xslx
and label toExport XLSX
. - Now select the
Events
tab and click+ Event Listener
drop down button and selectOn 'ojAction'
- Now it should auto-select
Action Chains
if not click from top tab option. - Switch from
Design
toCode
from top right
-
Replace
Action Chains > ButtonActionChain1 > Code
with the exporting data snippet in the "Exporting Data" example code. -
Connect the Business Object
Pres
to the table.
- Now select
Page Designer
and in the canvas select the table component from the right sidebar selectQuick Start
and thenAdd Data
- This opens up a modal on
Choose the source of your data
selectPres
Business Object and click next.
- On Bind Data step 2 from the left sidebar named
Endpoint Structure
check boxname
andindex1
then click next and lastly click finish.
- Testing the Application
- a) Launch the Preview:
- Look for the Play (▶️) icon in the top-right corner of the Visual Builder Studio
- Click it to open your application in a new browser tab
- b) Test the Import Function:
- In the preview tab, click the "Import XLSX" button
- Navigate back to Visual Builder Studio
- Select the
Pres
business object - Click the "Data" tab
- Verify that new records have been created with the president names and index
- Return to your application preview tab
- Refresh the page
- You should now see the table populated with the imported president data
- c) Test the Export Function:
- Click the "Export XLSX" button
- This will trigger an automatic download of a file named "SheetJSOracleVisualBuilderAoO.xlsx"
- Open the downloaded file using a spreadsheet editor.
- Verify that the exported data matches what was displayed in your application's table
-
See Oracle VBS working with Business Objects for more detail. ↩︎
-
See "JavaScript Support in Oracle VBS" for more details on custom JavaScript functionality and module handling. ↩︎
-
See Oracle VBS Table Component for more details. ↩︎
-
Registering for a free account on the Oracle Cloud Free Tier requires a valid phone number and a valid credit card. ↩︎