Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse Edm.Time when excel data is text #573

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/opa5-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
with:
ref: ${{ github.head_ref }}

- run: corepack enable
- run: corepack enable pnpm
- name: use node 20
uses: actions/setup-node@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
fetch-depth: 0
if: ${{ steps.release.outputs.releases_created }}

- run: corepack enable
- run: corepack enable pnpm
- name: use node 20
uses: actions/setup-node@v4
with:
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
run: |
git config --global user.email "[email protected]"
git config --global user.name "npm team"
- run: corepack enable
- run: corepack enable pnpm
- name: use node 20
uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wdi5-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
with:
ref: ${{ github.head_ref }}

- run: corepack enable
- run: corepack enable pnpm
- name: use node 20
uses: actions/setup-node@v4
with:
Expand Down
1 change: 0 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"engines": {
"node": ">=16"
},
"packageManager": "[email protected]",
"scripts": {
"start": "run-p watch:server start:uiv4fe start:uiv2fe start:uiv2freestyle start:uiv2fenondraft start:uiv4fpm",
"start:uiv4fe": "pnpm --filter ordersv4fe start",
Expand Down
Binary file modified examples/test/testFiles/TwoRowsNoErrors.xlsx
Binary file not shown.
74 changes: 62 additions & 12 deletions packages/ui5-cc-spreadsheetimporter/src/controller/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,22 @@ export default class Parser extends ManagedObject {
}
} else if (metadataColumn.type === "Edm.TimeOfDay" || metadataColumn.type === "Edm.Time") {
let date = rawValue;

// Only try to parse as Date if it's not marked as a date in sheet data
if (value.sheetDataType !== "d") {
const parsedDate = new Date(rawValue);
if (isNaN(parsedDate.getTime())) {
this.addMessageToMessages("spreadsheetimporter.invalidDate", util, messageHandler, index, [metadataColumn.label], rawValue);
continue;
}
date = parsedDate;
date = new Date(rawValue);
}
try {
this.checkDate(date, metadataColumn, util, messageHandler, index);
const spreadsheetDate = date.toISOString().substring(11, 19);
payload[columnKey] = spreadsheetDate;
} catch (error) {
this.addMessageToMessages("spreadsheetimporter.errorWhileParsing", util, messageHandler, index, [metadataColumn.label], rawValue);

if (date && !isNaN(date.getTime())) {
// Successfully parsed to Date, format to only time part
const timeFormatted = date.toISOString().substring(11, 19);
payload[columnKey] = timeFormatted;
} else {
// Call the new method to parse time pattern if excel data is text not date
const parsedTime = this.parseTimePattern(rawValue, util, messageHandler, index, metadataColumn);
if (parsedTime) {
payload[columnKey] = parsedTime;
}
}
} else if (
metadataColumn.type === "Edm.UInt8" ||
Expand Down Expand Up @@ -193,4 +195,52 @@ export default class Parser extends ManagedObject {
ui5type: MessageType.Error
});
}

/**
* Parses a time string according to specific patterns and returns the local time as a string.
* This method handles raw time strings and validates them against the expected format.
* The method supports time strings in the format "HH:mm:ss" and "HH:mm:ss.sss", where:
* - HH represents hours (00 to 23),
* - mm represents minutes (00 to 59),
* - ss represents seconds (00 to 59),
* - sss represents milliseconds (000 to 999).
*
* If the time string is valid and the components are within their respective ranges,
* it constructs a Date object and formats the time to respect the local timezone.
* If the time string does not match the expected pattern or components are out of range,
* it logs an appropriate error message.
*
* @param {string} rawValue - The raw time string to be parsed.
* @param {Util} util - Utility class instance for accessing helper functions like i18n.
* @param {MessageHandler} messageHandler - MessageHandler class instance for logging errors.
* @param {number} index - The row index of the data being parsed, used for error reporting.
* @param {Property} metadataColumn - The metadata for the column, including the type label.
* @returns {string|null} - Returns a formatted time string if successful, otherwise null.
*/
static parseTimePattern(rawValue: any, util: Util, messageHandler: MessageHandler, index: number, metadataColumn: Property) {
const timePattern = /^(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?$/;
const match = rawValue.match(timePattern);

if (match) {
const hours = parseInt(match[1], 10);
const minutes = parseInt(match[2], 10);
const seconds = match[3] ? parseInt(match[3], 10) : 0;
const milliseconds = match[4] ? parseInt(match[4], 10) : 0;

// Validate time components
if (hours < 24 && minutes < 60 && seconds < 60) {
// Construct a Date object from time components
let today = new Date();
today.setHours(hours, minutes, seconds, milliseconds);
// Format the time considering the local timezone
const timeFormatted = `${today.getHours().toString().padStart(2, "0")}:${today.getMinutes().toString().padStart(2, "0")}:${today.getSeconds().toString().padStart(2, "0")}`;
return timeFormatted;
} else {
this.addMessageToMessages("spreadsheetimporter.invalidTime", util, messageHandler, index, [metadataColumn.label], rawValue);
}
} else {
this.addMessageToMessages("spreadsheetimporter.invalidTimeFormat", util, messageHandler, index, [metadataColumn.label], rawValue);
}
return null;
}
}
Loading