Skip to content

Commit

Permalink
Add GetCollectionType to Edit-In Excel API (microsoft#550)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->

The issue
We have received this issue:
microsoft/ALAppExtensions#21940 where the
programmer wants to move all of the filters from one field to another.
The only thing missing for this capability is that we should be able to
dynamically detect if the filter is a AND or OR expression, this is not
possible in the current implementation.

Expected behavior
We should expose the "Edit in Excel Filter Collection Type" property on
the filter through the API, such that clients of the API can dynamically
detect what collection type the filter has.

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#498510](https://dynamicssmb2.visualstudio.com/Dynamics%20SMB/_workitems/edit/498510/)

---------

Co-authored-by: Matti Nielsen <[email protected]>
  • Loading branch information
DenLilleMand and DenLilleMand authored Apr 18, 2024
1 parent 79ee4d0 commit 302e4bf
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if not CLEAN25
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Expand All @@ -12,12 +13,18 @@ interface "Edit in Excel Field Filter"
{
Access = Public;

ObsoleteState = Pending;
ObsoleteTag = '25.0';
ObsoleteReason = 'Replaced by "Edit in Excel Field Filter v2" with support for getting the collection type of the filter.';

/// <summary>
/// Add a filter value
/// </summary>
/// <param name="EditInExcelFilterType">The filter type, such as Equal, Greater than.</param>
/// <param name="FilterValue">The value which the field should be Equal to, Greater than etc.</param>
#pragma warning disable AL0432
procedure AddFilterValue(EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text): Interface "Edit in Excel Field Filter"
#pragma warning restore AL0432

/// <summary>
/// Get a specific filter
Expand All @@ -39,3 +46,4 @@ interface "Edit in Excel Field Filter"
/// <returns>The number of filters</returns>
procedure Count(): Integer
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Integration.Excel;

/// <summary>
/// This codeunit provides an interface to create a filter for a specific field for Edit in Excel.
/// </summary>
interface "Edit in Excel Field Filter v2"
{
Access = Public;

/// <summary>
/// Add a filter value
/// </summary>
/// <param name="EditInExcelFilterType">The filter type, such as Equal, Greater than.</param>
/// <param name="FilterValue">The value which the field should be Equal to, Greater than etc.</param>
procedure AddFilterValueV2(EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text): Interface "Edit in Excel Field Filter v2"

/// <summary>
/// Get a specific filter
/// </summary>
/// <param name="Index">The index of the filter.</param>
/// <param name="EditInExcelFilterType">The filter type, such as Equal, Greater than.</param>
/// <param name="FilterValue">The value which the field should be Equal to, Greater than etc.</param>
procedure Get(Index: Integer; var EditinExcelFilterType: Enum "Edit in Excel Filter Type"; var FilterValue: Text)

/// <summary>
/// Get the filter collection type
/// </summary>
/// <returns>The collection type of the filter</returns>
procedure GetCollectionType(): Enum "Edit in Excel Filter Collection Type"

/// <summary>
/// Remove a specific filter
/// </summary>
/// <param name="Index">The index of the filter.</param>
procedure Remove(Index: Integer)

/// <summary>
/// Counts the number of filters
/// </summary>
/// <returns>The number of filters</returns>
procedure Count(): Integer
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,60 @@ codeunit 1490 "Edit in Excel Filters"
var
EditinExcelFiltersImpl: Codeunit "Edit in Excel Filters Impl.";


#if not CLEAN25
/// <summary>
/// Add the specified field using 'and' collection type.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
[Obsolete('Use AddFieldV2 instead.', '25.0')]
#pragma warning disable AL0432
procedure AddField(ODataFieldName: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Interface "Edit in Excel Field Filter"
#pragma warning restore AL0432
begin
exit(EditinExcelFiltersImpl.AddField(ODataFieldName, "Edit in Excel Filter Collection Type"::"and", EditInExcelEdmType));
end;
#endif

/// <summary>
/// Add the specified field using 'and' collection type.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
procedure AddFieldV2(ODataFieldName: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Interface "Edit in Excel Field Filter v2"
begin
exit(EditinExcelFiltersImpl.AddField(ODataFieldName, "Edit in Excel Filter Collection Type"::"and", EditInExcelEdmType));
end;

#if not CLEAN25
/// <summary>
/// Add the specified field with a specified collection type.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <param name="EditInExcelFilterOperatorType">Specifies whether filters for this field have 'and' or 'or', such as Field1 = a|b|c (or operator) or Field1 = &lt;a&amp;>b&gt;amp;>c (and operator). Both operators for the same field is currently not supported.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
[Obsolete('Use AddFieldV2 instead.', '25.0')]
#pragma warning disable AL0432
procedure AddField(ODataFieldName: Text; EditInExcelFilterOperatorType: Enum "Edit in Excel Filter Collection Type"; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Interface "Edit in Excel Field Filter"
#pragma warning restore AL0432
begin
exit(EditinExcelFiltersImpl.AddField(ODataFieldName, EditInExcelFilterOperatorType, EditInExcelEdmType));
end;
#endif

/// <summary>
/// Add the specified field with a specified collection type.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <param name="EditInExcelFilterOperatorType">Specifies whether filters for this field have 'and' or 'or', such as Field1 = a|b|c (or operator) or Field1 = &lt;a&amp;>b&gt;amp;>c (and operator). Both operators for the same field is currently not supported.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
procedure AddFieldV2(ODataFieldName: Text; EditInExcelFilterOperatorType: Enum "Edit in Excel Filter Collection Type"; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Interface "Edit in Excel Field Filter V2"
begin
exit(EditinExcelFiltersImpl.AddField(ODataFieldName, EditInExcelFilterOperatorType, EditInExcelEdmType));
end;

#if not CLEAN25
/// <summary>
/// Add the specified field with an initial value using 'and' collection type.
/// This is mainly intended for fields that only filter on a single value.
Expand All @@ -48,17 +81,47 @@ codeunit 1490 "Edit in Excel Filters"
/// <param name="EditInExcelFilterType">The filter type, such as Equal, Greater than etc.</param>
/// <param name="FilterValue">The value which the field should be Equal to, Greater than etc.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
[Obsolete('Use AddFieldV2 instead.', '25.0')]
procedure AddField(ODataFieldName: Text; EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type")
begin
EditinExcelFiltersImpl.AddField(ODataFieldName, EditInExcelFilterType, FilterValue, EditInExcelEdmType);
end;
#endif

/// <summary>
/// Add the specified field with an initial value using 'and' collection type.
/// This is mainly intended for fields that only filter on a single value.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <param name="EditInExcelFilterType">The filter type, such as Equal, Greater than etc.</param>
/// <param name="FilterValue">The value which the field should be Equal to, Greater than etc.</param>
/// <param name="EditInExcelEdmType">The Edm type of the OData field as specified in the $metadata document.</param>
procedure AddFieldV2(ODataFieldName: Text; EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type")
begin
EditinExcelFiltersImpl.AddField(ODataFieldName, EditInExcelFilterType, FilterValue, EditInExcelEdmType);
end;

#if not CLEAN25
/// <summary>
/// Get the field filters for the specified field.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <returns></returns>
[Obsolete('Use GetV2 instead.', '25.0')]
#pragma warning disable AL0432
procedure Get(ODataFieldName: Text): Interface "Edit in Excel Field Filter"
#pragma warning restore AL0432
begin
exit(EditinExcelFiltersImpl.Get(ODataFieldName));
end;
#endif

/// <summary>
/// Get the field filters for the specified field.
/// </summary>
/// <param name="ODataFieldName">The OData name of the field referenced.</param>
/// <returns></returns>
procedure GetV2(ODataFieldName: Text): Interface "Edit in Excel Field Filter V2"
begin
exit(EditinExcelFiltersImpl.Get(ODataFieldName));
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ codeunit 1491 "Edit in Excel Filters Impl."
FilterContainsMultipleOperatorsTxt: Label 'The page filter contains multiple operators, the latter was removed.', Locked = true;
FieldPayloadEdmTypeTok: Label 'fieldPayload.%1.edmType', Locked = true;

procedure AddField(ODataFieldName: Text; EditinExcelFilterCollectionType: Enum "Edit in Excel Filter Collection Type"; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Interface "Edit in Excel Field Filter"
procedure AddField(ODataFieldName: Text; EditinExcelFilterCollectionType: Enum "Edit in Excel Filter Collection Type"; EditInExcelEdmType: Enum "Edit in Excel Edm Type"): Codeunit "Edit in Excel Fld Filter Impl."
begin
TryAdd(ODataFieldName, EditinExcelFilterCollectionType, Format(EditInExcelEdmType));
exit(Get(ODataFieldName));
end;

procedure AddField(ODataFieldName: Text; EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type") EditinExcelFieldFilter: Interface "Edit in Excel Field Filter"
procedure AddField(ODataFieldName: Text; EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text; EditInExcelEdmType: Enum "Edit in Excel Edm Type") EditinExcelFieldFilter: Codeunit "Edit in Excel Fld Filter Impl."
begin
EditinExcelFieldFilter := AddField(ODataFieldName, "Edit in Excel Filter Collection Type"::"and", EditInExcelEdmType);
Get(ODataFieldName).AddFilterValue(EditInExcelFilterType, FilterValue);
Get(ODataFieldName).AddFilterValueV2(EditInExcelFilterType, FilterValue);
end;

procedure Get(ODataFieldName: Text): Interface "Edit in Excel Field Filter"
procedure Get(ODataFieldName: Text): Codeunit "Edit in Excel Fld Filter Impl."
var
EditinExcelFldFilterImpl: Codeunit "Edit in Excel Fld Filter Impl.";
FilterCollectionNode: DotNet FilterCollectionNode;
Expand Down Expand Up @@ -137,7 +137,7 @@ codeunit 1491 "Edit in Excel Filters Impl."
Session.LogMessage('0000I3X', FilterContainsMultipleOperatorsTxt, Verbosity::Warning, DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, 'Category', EditInExcelTelemetryCategoryTxt);
exit; // OData does not support filtering on a field with both 'and' and 'or' hence if we see both, ignore the second type
end;
Get(ODataFieldName).AddFilterValue(EditinExcelFilterType, FilterValue);
Get(ODataFieldName).AddFilterValueV2(EditinExcelFilterType, FilterValue);
end;
end;
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,45 @@ using System;
/// <summary>
/// This codeunit provides an interface to running Edit in Excel for a specific page.
/// </summary>
codeunit 1492 "Edit in Excel Fld Filter Impl." implements "Edit in Excel Field Filter"

#if not CLEAN25
#pragma warning disable AL0432
codeunit 1492 "Edit in Excel Fld Filter Impl." implements "Edit in Excel Field Filter", "Edit in Excel Field Filter v2"
#pragma warning restore AL0432
#else
codeunit 1492 "Edit in Excel Fld Filter Impl." implements "Edit in Excel Field Filter v2"
#endif
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

var
EditInExcelFieldFilter: Interface "Edit in Excel Field Filter";
EditInExcelFieldFilter: Codeunit "Edit in Excel Fld Filter Impl.";
FilterCollectionNode: DotNet FilterCollectionNode;
ODataFieldName: Text;
EdmType: Text;

internal procedure Initialize(NewODataFieldName: Text; NewEdmType: Text; NewFilterCollectionNode: DotNet FilterCollectionNode; NewEditInExcelFieldFilter: Interface "Edit in Excel Field Filter")
internal procedure Initialize(NewODataFieldName: Text; NewEdmType: Text; NewFilterCollectionNode: DotNet FilterCollectionNode; NewEditInExcelFieldFilter: Codeunit "Edit in Excel Fld Filter Impl.")
begin
FilterCollectionNode := NewFilterCollectionNode;
EditInExcelFieldFilter := NewEditInExcelFieldFilter;
ODataFieldName := NewODataFieldName;
EdmType := NewEdmType;
end;

#if not CLEAN25
#pragma warning disable AL0432
[Obsolete('Use AddFilterValueV2 instead, returns interface "Edit in Excel Field Filter" instead which supports getting filter collection type', '25.0')]
procedure AddFilterValue(EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text): Interface "Edit in Excel Field Filter"
#pragma warning restore AL0432
begin
AddFieldFilterValue(EditInExcelFilterType, FilterValue);
exit(EditInExcelFieldFilter); // Reference back to self to allow builder pattern
end;
#endif

procedure AddFilterValueV2(EditInExcelFilterType: Enum "Edit in Excel Filter Type"; FilterValue: Text): Interface "Edit in Excel Field Filter V2"
begin
AddFieldFilterValue(EditInExcelFilterType, FilterValue);
exit(EditInExcelFieldFilter); // Reference back to self to allow builder pattern
Expand All @@ -52,6 +70,16 @@ codeunit 1492 "Edit in Excel Fld Filter Impl." implements "Edit in Excel Field F
FilterCollectionNode.Collection.RemoveAt(Index);
end;

procedure GetCollectionType(): Enum "Edit in Excel Filter Collection Type"
var
TempString: DotNet String;
begin
TempString := FilterCollectionNode.Operator();
if TempString = 'or' then
exit("Edit in Excel Filter Collection Type"::"or");
exit("Edit in Excel Filter Collection Type"::"and")
end;

procedure Count(): Integer
begin
exit(FilterCollectionNode.Collection.Count());
Expand All @@ -75,4 +103,4 @@ codeunit 1492 "Edit in Excel Fld Filter Impl." implements "Edit in Excel Field F

FilterCollectionNode.Collection.Add(FilterBinaryNode);
end;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ codeunit 132526 "Edit in Excel Filters Test"
var
LibraryAssert: Codeunit "Library Assert";

[Test]
procedure TestEditInExcelFieldFilterGetCollectionTypeReturnsCorrectAndOperator()
var
EditinExcelFilters: Codeunit "Edit in Excel Filters";
EditInExcelFilter: Interface "Edit in Excel Field Filter v2";
FieldName: Text;
EditInExcelFilterCollectionType: Enum "Edit in Excel Filter Collection Type";
begin
FieldName := 'No_';

EditinExcelFilters.AddFieldV2(FieldName, "Edit in Excel Filter Collection Type"::"and", Enum::"Edit in Excel Edm Type"::"Edm.String");
EditinExcelFilters.GetV2(FieldName).AddFilterValueV2(Enum::"Edit in Excel Filter Type"::Equal, '10000');
EditinExcelFilters.GetV2(FieldName).AddFilterValueV2(Enum::"Edit in Excel Filter Type"::Equal, '10001');

EditInExcelFilter := EditinExcelFilters.GetV2(FieldName);
EditInExcelFilterCollectionType := EditInExcelFilter.GetCollectionType();
LibraryAssert.AreEqual("Edit in Excel Filter Collection Type"::"and", EditInExcelFilterCollectionType, 'Field filter created with an AND operator should have a collection type of AND.')
end;

[Test]
procedure TestEditInExcelFieldFilterGetCollectionTypeReturnsCorrectOrOperator()
var
EditinExcelFilters: Codeunit "Edit in Excel Filters";
EditInExcelFilter: Interface "Edit in Excel Field Filter v2";
FieldName: Text;
EditInExcelFilterCollectionType: Enum "Edit in Excel Filter Collection Type";
begin
FieldName := 'No_';

EditinExcelFilters.AddFieldV2(FieldName, "Edit in Excel Filter Collection Type"::"or", Enum::"Edit in Excel Edm Type"::"Edm.String");
EditinExcelFilters.GetV2(FieldName).AddFilterValueV2(Enum::"Edit in Excel Filter Type"::Equal, '10000');
EditinExcelFilters.GetV2(FieldName).AddFilterValueV2(Enum::"Edit in Excel Filter Type"::Equal, '10001');

EditInExcelFilter := EditinExcelFilters.GetV2(FieldName);
EditInExcelFilterCollectionType := EditInExcelFilter.GetCollectionType();
LibraryAssert.AreEqual("Edit in Excel Filter Collection Type"::"or", EditInExcelFilterCollectionType, 'Field filter created with an OR operator should have a collection type of OR.')
end;

[Test]
procedure TestEditInExcelStructuredFiltersDateTime()
var
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ codeunit 132525 "Edit in Excel Test"
TenantWebService.SetRange("Object ID", Page::"Edit in Excel List");
TenantWebService.DeleteAll();

EditinExcelFilters.AddField('Id', Enum::"Edit in Excel Filter Type"::Equal, 'test', Enum::"Edit in Excel Edm Type"::"Edm.String");
EditinExcelFilters.AddFieldV2('Id', Enum::"Edit in Excel Filter Type"::Equal, 'test', Enum::"Edit in Excel Edm Type"::"Edm.String");

EditInExcel.EditPageInExcel(CopyStr(EditInExcelList.Caption, 1, 240), Page::"Edit in Excel List", EditinExcelFilters);

Expand Down

0 comments on commit 302e4bf

Please sign in to comment.