Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
#LuukBusschers
Browse files Browse the repository at this point in the history
  • Loading branch information
Busschers committed Dec 2, 2022
1 parent da5cbe3 commit 63370f7
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 0 deletions.
73 changes: 73 additions & 0 deletions SRC/Codeunits/OVWExportToExcel.Codeunit.al
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 27 additions & 0 deletions SRC/Pages/OVWObjectViewer.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
164 changes: 164 additions & 0 deletions SRC/TableExtensions/OVWExcelBuffer.TableExtension.al
Original file line number Diff line number Diff line change
@@ -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];
}
15 changes: 15 additions & 0 deletions Translations/PCO Object Viewer.g.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">Page OVW Object Viewer - Property Caption</note>
</trans-unit>
<trans-unit id="Page 2389208843 - Method 3021710581 - NamedType 3370973526" size-unit="char" translate="yes" xml:space="preserve">
<source>Export is only possible for object type Table</source>
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">Page OVW Object Viewer - Method DoExportToExcel - NamedType NoTableErr</note>
</trans-unit>
<trans-unit id="Page 2389208843 - Method 613431046 - NamedType 4093306170" size-unit="char" translate="yes" xml:space="preserve">
<source>Insufficient read permissions.</source>
<note from="Developer" annotates="general" priority="2"></note>
Expand Down Expand Up @@ -413,6 +418,11 @@
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">Page OVW Object Viewer - Action ShowTables - Property Caption</note>
</trans-unit>
<trans-unit id="Page 2389208843 - Action 2851454604 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
<source>Export To Excel</source>
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">Page OVW Object Viewer - Action ExportToExcel - Property Caption</note>
</trans-unit>
<trans-unit id="Page 2389208843 - Action 1570166248 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
<source>Run Object</source>
<note from="Developer" annotates="general" priority="2"></note>
Expand Down Expand Up @@ -478,6 +488,11 @@
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">XmlPort OVW Object Data I/O - Property Caption</note>
</trans-unit>
<trans-unit id="TableExtension 1327980363 - Field 4271261664 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve" al-object-target="Table 2254553953">
<source>OVW Value is BLOB</source>
<note from="Developer" annotates="general" priority="2"></note>
<note from="Xliff Generator" annotates="general" priority="3">TableExtension OVW Excel Buffer - Field OVW Value is BLOB - Property Caption</note>
</trans-unit>
<trans-unit id="Enum 2127429110 - EnumValue 2402474825 - Property 2879900210" size-unit="char" translate="yes" xml:space="preserve">
<source>Generated</source>
<note from="Developer" annotates="general" priority="2"></note>
Expand Down

0 comments on commit 63370f7

Please sign in to comment.