From 63370f70d0f3b92f4dc5861ca83c7bd836f64115 Mon Sep 17 00:00:00 2001 From: Luuk Date: Fri, 2 Dec 2022 12:12:01 +0100 Subject: [PATCH] #LuukBusschers --- SRC/Codeunits/OVWExportToExcel.Codeunit.al | 73 ++++++++ SRC/Pages/OVWObjectViewer.Page.al | 27 +++ .../OVWExcelBuffer.TableExtension.al | 164 ++++++++++++++++++ Translations/PCO Object Viewer.g.xlf | 15 ++ 4 files changed, 279 insertions(+) create mode 100644 SRC/Codeunits/OVWExportToExcel.Codeunit.al create mode 100644 SRC/TableExtensions/OVWExcelBuffer.TableExtension.al diff --git a/SRC/Codeunits/OVWExportToExcel.Codeunit.al b/SRC/Codeunits/OVWExportToExcel.Codeunit.al new file mode 100644 index 0000000..3177e3e --- /dev/null +++ b/SRC/Codeunits/OVWExportToExcel.Codeunit.al @@ -0,0 +1,73 @@ +codeunit 50138 "OVW Export to Excel" +{ + trigger OnRun() + begin + + end; + + procedure Rec2Excel(TableID: Integer) + var + RecRef: RecordRef; + begin + RecRef.Open(TableID); + if not RecRef.ReadPermission then + exit; + + RowNo := 1; + + if RecRef.FindSet() then begin + repeat + Ref2Excel(RecRef, TableID); + until RecRef.Next() = 0; + end; + TempExcelBuffer.CreateExcelBook(RecRef.Name); //change into table name? + end; + + local procedure Ref2Excel(var Ref: RecordRef; TableID: Integer) + var + int: Integer; + begin + ColumnNo := 1; + RowNo += 1; + + for int := 1 to Ref.FieldCount() do begin + fRef2Excel(Ref, int, TableID); + end; + end; + + local procedure fRef2Excel(var Ref: RecordRef; var i: Integer; TableID: Integer) + var + FRef: FieldRef; + begin + FRef := Ref.FieldIndex(i); + + if not IsFieldEnabled(FRef, TableID) then + exit; + + case FRef.Class of + FRef.Class::Normal: + begin + TempExcelBuffer.AddCell(RowNo, ColumnNo, FRef.Value, FRef.Caption, 1); + end; + + FRef.Class::FlowField: + begin + FRef.CalcField(); + TempExcelBuffer.AddCell(RowNo, ColumnNo, FRef.Value, FRef.Caption, 1); + end; + end; + end; + + local procedure IsFieldEnabled(FRef: FieldRef; TableID: Integer): Boolean + var + FieldRecord: Record Field; + begin + if FieldRecord.Get(FRef.Record().Number, FRef.Number) then + exit(FieldRecord.Enabled); + end; + + var + TempExcelBuffer: Record "Excel Buffer" temporary; + ColumnNo: Integer; + RowNo: Integer; +} \ No newline at end of file diff --git a/SRC/Pages/OVWObjectViewer.Page.al b/SRC/Pages/OVWObjectViewer.Page.al index 8354d86..99ece43 100644 --- a/SRC/Pages/OVWObjectViewer.Page.al +++ b/SRC/Pages/OVWObjectViewer.Page.al @@ -80,6 +80,22 @@ page 50130 "OVW Object Viewer" RunApplicationObject(); end; } + + action(ExportToExcel) + { + ApplicationArea = All; + Caption = 'Export To Excel'; + Image = Excel; + Promoted = true; + PromotedCategory = Process; + PromotedIsBig = true; + PromotedOnly = true; + + trigger OnAction(); + begin + DoExportToExcel(); + end; + } } area(Navigation) { @@ -192,4 +208,15 @@ page 50130 "OVW Object Viewer" exit; exit(OVWRecordCountMgt.GetRecordCount(ForObjectID)); end; + + local procedure DoExportToExcel() + var + OVWExporttoExcel: Codeunit "OVW Export to Excel"; + NoTableErr: Label 'Export is only possible for object type Table'; + begin + if Rec."Object Type" <> Rec."Object Type"::Table then + Error(NoTableErr); + + OVWExporttoExcel.Rec2Excel(Rec."Object ID"); + end; } \ No newline at end of file diff --git a/SRC/TableExtensions/OVWExcelBuffer.TableExtension.al b/SRC/TableExtensions/OVWExcelBuffer.TableExtension.al new file mode 100644 index 0000000..95dd97e --- /dev/null +++ b/SRC/TableExtensions/OVWExcelBuffer.TableExtension.al @@ -0,0 +1,164 @@ +tableextension 50131 "OVW Excel Buffer" extends "Excel Buffer" +{ + fields + { + field(50131; "OVW Value is BLOB"; Boolean) + { + DataClassification = SystemMetadata; + } + } + + procedure AddCell(pintRow: Integer; var pintColumn: Integer; pvarValue: Variant; ptxtHeading: Text[250]; pintColumnIncrement: Integer) + begin + AddCellWithFormatting(pintRow, pintColumn, pvarValue, ptxtHeading, false, false, false, 2, pintColumnIncrement); //34764+- + end; + + procedure AddCellWithFormatting(RowNo: Integer; var ColumnNo: Integer; CellValue: Variant; CellHeading: Text[250]; IsBold: Boolean; IsItalic: Boolean; IsUnderlined: Boolean; NumberOfDecimals: Integer; ColumnIncrementValue: Integer) + var + DecimalNotation: Text; + begin + Init(); + Validate("Row No.", RowNo); + Validate("Column No.", ColumnNo); + TestField("Row No."); + TestField("Column No."); + Formula := ''; + Bold := IsBold; + Italic := IsItalic; + Underline := IsUnderlined; + + DecimalNotation := '#,##0'; + if NumberOfDecimals > 0 then begin + DecimalNotation += '.'; + DecimalNotation := PadStr(DecimalNotation, StrLen(DecimalNotation) + NumberOfDecimals, '0'); + end; + + case true of + CellValue.IsInteger, + CellValue.IsDecimal, + CellValue.IsDuration, + CellValue.IsBigInteger: + begin + NumberFormat := '0'; + "Cell Type" := "Cell Type"::Number; + if CellValue.IsDecimal then + NumberFormat := DecimalNotation; + end; + + CellValue.IsDate: + begin + "Cell Type" := "Cell Type"::Date; + end; + + CellValue.IsTime: + begin + "Cell Type" := "Cell Type"::Time; + end; + + CellValue.IsDateTime: + begin + "Cell Type" := "Cell Type"::Date; + end; + + else begin + "Cell Type" := "Cell Type"::Text; + if StrLen(Format(CellValue)) > MaxStrLen("Cell Value as Text") then begin + "OVW Value is BLOB" := true; + ConvertStringToBlob(Format(CellValue), TempBlob); + TempBlob.FromRecord(Rec, FieldNo("Cell Value as Blob")); + end; + end; + end; + if not "OVW Value is BLOB" then + "Cell Value as Text" := Format(CellValue); + if not Insert() then + Modify(); + AddHeading(ColumnNo, CellHeading); + ColumnNo += ColumnIncrementValue; + end; + + [Obsolete('This procedure is no longer used and will be deprecated. This warning will become an error in the future.', '19.1.20220408')] + procedure AddFormula(pintRow: Integer; var pintColumn: Integer; ptxtFormula: Text[250]; ptxtHeading: Text[250]; pintColumnIncrement: Integer) + begin + Init(); + Validate("Row No.", pintRow); + Validate("Column No.", pintColumn); + "Cell Value as Text" := ''; + Formula := ptxtFormula; + if not Insert() then + Modify(); + AddHeading(pintColumn, ptxtHeading); + pintColumn += pintColumnIncrement; + end; + + procedure AddHeading(pintColumn: Integer; ptxtHeading: Text[250]) + begin + AddHeadingAtRow(1, pintColumn, ptxtHeading); + end; + + procedure AddHeadingAtRow(RowNo: Integer; var ColumnNo: Integer; ColumnHeading: Text[250]) + begin + IF ColumnHeading = '' THEN + exit; + if RowNo <> 1 then + ColumnNo += 1; + if Get(RowNo, ColumnNo) then + exit; + Init(); + Validate("Row No.", RowNo); + Validate("Column No.", ColumnNo); + "Cell Value as Text" := ColumnHeading; + Bold := true; + "Cell Type" := "Cell Type"::Text; + Insert(); + end; + + procedure CreateExcelBook(SheetName: Text) + begin + CreateFile(SheetName); + end; + + procedure CreateFile(SheetName: Text) + begin + CreateNewBook(SheetName); + WriteSheet(CopyStr(SheetName, 1, 80), CompanyName, UserId); + CloseBook(); + OpenExcel(); + end; + + [Obsolete('This procedure is no longer used and will be deprecated. This warning will become an error in the future.', '19.1.20220408')] + procedure OnlyCreateFile(SheetName: Text; BookCreated: boolean) + var + FileName: text; + begin + ExcelBookCreated := BookCreated; + NewSheetName := SheetName; + if not BookCreated then + CreateNewBook(SheetName); + + WriteSheet(CopyStr(SheetName, 1, 80), CompanyName, UserId); + if ExcelBookCreated then + SelectOrAddSheet(NewSheetName); + end; + + procedure CreateFileWithoutOpening(ptxtSheetName: Text) + begin + CreateNewBook(ptxtSheetName); + WriteSheet(CopyStr(ptxtSheetName, 1, 80), CompanyName, UserId); + CloseBook(); + end; + + procedure ConvertStringToBlob(Input: Text; var TempBlob: Codeunit "Temp Blob"): Integer + var + WriteStream: OutStream; + begin + TempBlob.CreateOutStream(WriteStream, TextEncoding::UTF8); + WriteStream.WriteText(Input); + end; + + var + TempBlob: Codeunit "Temp Blob"; + + ExcelBookCreated: Boolean; + NewSheetName: text[20]; +} \ No newline at end of file diff --git a/Translations/PCO Object Viewer.g.xlf b/Translations/PCO Object Viewer.g.xlf index a94fe07..92712f2 100644 --- a/Translations/PCO Object Viewer.g.xlf +++ b/Translations/PCO Object Viewer.g.xlf @@ -333,6 +333,11 @@ Page OVW Object Viewer - Property Caption + + Export is only possible for object type Table + + Page OVW Object Viewer - Method DoExportToExcel - NamedType NoTableErr + Insufficient read permissions. @@ -413,6 +418,11 @@ Page OVW Object Viewer - Action ShowTables - Property Caption + + Export To Excel + + Page OVW Object Viewer - Action ExportToExcel - Property Caption + Run Object @@ -478,6 +488,11 @@ XmlPort OVW Object Data I/O - Property Caption + + OVW Value is BLOB + + TableExtension OVW Excel Buffer - Field OVW Value is BLOB - Property Caption + Generated