forked from equinor/witsml-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request equinor#366 from aszalos-roland/#173_Export_log_to…
…_.csv equinor#173 Export log to .csv
- Loading branch information
Showing
5 changed files
with
123 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export interface ExportProperties { | ||
outputMimeType: string; | ||
fileExtension: string; | ||
separator: string; | ||
newLineCharacter: string; | ||
omitSpecialCharactersFromFilename?: boolean; | ||
appendDateTime?: boolean; | ||
} | ||
interface ExportObject { | ||
exportData: (fileName: string, header: string, data: string) => void; | ||
properties: ExportProperties; | ||
} | ||
function omitSpecialCharacters(text: string): string { | ||
return text.replace(/[&/\\#,+()$~%.'":*?<>{}]/g, "_"); | ||
} | ||
function appendDateTime(append: boolean): string { | ||
const now = new Date(); | ||
return append ? `-${now.getFullYear()}-${now.getMonth()}-${now.getDay()}T${now.getHours()}-${now.getMinutes()}-${now.getSeconds()}` : ""; | ||
} | ||
function useExport(props: ExportProperties): ExportObject { | ||
const exportData = (fileName: string, header: string, data: string) => { | ||
const link = document.createElement("a"); | ||
link.href = window.URL.createObjectURL( | ||
new Blob([header, props.newLineCharacter, data], { | ||
type: props.outputMimeType | ||
}) | ||
); | ||
link.download = props.omitSpecialCharactersFromFilename | ||
? `${omitSpecialCharacters(fileName)}${appendDateTime(props.appendDateTime)}${props.fileExtension}` | ||
: `${fileName}${appendDateTime(props.appendDateTime)}${props.fileExtension}`; | ||
document.body.appendChild(link); | ||
link.click(); | ||
//we might not need a timeout for clean up | ||
setTimeout(function () { | ||
window.URL.revokeObjectURL(link.href); | ||
link.remove(); | ||
}, 200); | ||
}; | ||
return { exportData: exportData, properties: props }; | ||
} | ||
|
||
export default useExport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters