1

我希望有人能把我从这里的痛苦中解救出来。我用 JavaScript 重写了一个 LotusScript 函数,但它没有按应有的方式工作。我不能再看这个东西了。我希望有人能帮我解决这个问题。

该函数采用组名和地址簿数据库对象。它获取并返回该组的所有成员,包括嵌套组。它在 Lotusscript 版本中运行良好,但在 Javascript 版本中却不行。这与递归函数和数组的索引有关。

如果您创建两个组。将 10 个姓名放入第一组,将 5 个姓名放入第二组。请注意,使用前导 # 命名第二组,使其排在第一组的顶部

A组 1A组 bla bla2 bla3 bla4 bla5 bla6 bla7 bla8 bla9 bla10

1组-A 其他1 其他2 其他3 其他4 其他5

在 Javascript 版本中,当您传递函数 group-A 时,它会返回 10 个名称。第二组五个,第一组第二个五个。

这是lotusscript版本的代码

Function LibGetGroupMembers( groupName As String, addressBook As NotesDatabase ) As Variant

'  This Function takes a passed in group name and gets all the members of that group.
'  Not a big whoop until you consider nested group names.  This is a recursive routine.
'
 '   Inputs  :  Name of the group to fetch the members of.
'
'  Uses     :  Global objects
 '
 '  Outputs : Returns an array of members
 '
 '  History:
 '  02.19.2001  - Steven Rieger           :  Created Function.

Dim groupView As NotesView
Dim groupMainDoc As NotesDocument
Dim groupMembers As Variant
Dim nestedGroupMembers As Variant
Dim arrayOfMembers() As String
Dim mainIndex As Integer
Dim nestedIndex As Integer
Dim resultIndex As Integer


On Error Goto ErrorHandling

gLibObjectName = CStr( GetThreadInfo(1) ) & " - " & gLibDatabaseNamePath
gLibErrorMessage = "Begin " & gLibObjectName
Call LibLogErrorPushCallingPgm ( gLibErrorMessage  )
Print( gLibErrorMessage )

gLibAdditionalErrorMessage = "Fetching Group View From NAB"
Set groupView = addressBook.GetView( "($VIMGroups)" )
If( groupView Is Nothing ) Then
    Call LibLogError( "Error: Un-able to get view from address book", gLibErrorMessage )
    Exit Function
End If

gLibAdditionalErrorMessage = "Fetching Group Main Document " & groupName    
Set groupMainDoc = groupView.GetDocumentByKey( groupName, True )

resultIndex = 0
Redim arrayOfMembers( 0 )

'  Did we find a matching group document?
If Not( groupMainDoc Is Nothing ) Then
    If Not( Iselement ( gListOfGroupNames( Lcase( groupName ) ) ) ) Then
'           Print( "Processing Group: " & groupName )
        gListOfGroupNames( Lcase( groupName ) ) = " "
        groupMembers=groupMainDoc.GetItemValue( "Members" )
        For mainIndex = Lbound( groupMembers ) To Ubound( groupMembers)
            nestedGroupMembers= LibGetGroupMembers( groupMembers( mainIndex ), addressBook )
            If( nestedGroupMembers(0) = "" ) Then
                If( groupMembers( mainIndex ) <> "" ) Then
                    Redim Preserve arrayOfMembers( Ubound( arrayOfMembers ) + 1) 
                    arrayOfMembers( resultIndex ) = groupMembers( mainIndex )
                    resultIndex = resultIndex + 1
                End If
            Else
                Redim Preserve arrayOfMembers( Ubound( nestedGroupMembers ) + resultIndex ) 
                For nestedIndex = Lbound( nestedGroupMembers ) To Ubound( nestedGroupMembers )
                    arrayOfMembers( resultIndex ) = nestedGroupMembers( nestedIndex )
                    resultIndex = resultIndex + 1
                Next
            End If
        Next
        If( arrayOfMembers( Ubound( arrayOfMembers ) ) = "" And Ubound( arrayOfMembers ) > 0 ) Then
            Redim Preserve arrayOfMembers( Ubound( arrayOfMembers ) - 1 ) 
        End If
    Else
        groupName = "*** Circular Group: " & groupName & " ***"
    End If
End If

LibGetGroupMembers = arrayOfMembers

Goto Bye

ErrorHandling :
Call LibLogError( "Error: " & Err( ) & ": " & Error( ) & Chr( 13 ) & Chr( 13 ) & "Occured in Module " & CStr( GetThreadInfo(1) ) & " at Line Number: " & Erl(), CStr( GetThreadInfo(1) & " - " & gLibDatabaseNamePath ) )
Resume Bye     

Bye: 

gLibAdditionalErrorMessage = "End " & CStr( GetThreadInfo(1) & " - " & gLibDatabaseNamePath ) 
' This will pop the last entry of the stack!
Call libLogErrorPopCallingPgm()
Print( gLibAdditionalErrorMessage )

End Function

这是 JAVASCRIPT 函数的代码

function jsLibGetGroupMembers( groupName:string, addressBook:NotesDatabase )
{
//  This Function takes a passed in group name and gets all the members of that group.
//  Not a big whoop until you consider nested group names.  This is a recursive routine.
//
//   Inputs  :  Name of the group to fetch the members of.
//
//  Uses     :  Global objects
//
//  Outputs : Returns an array of members
//
//  History:
//  02.19.2001  - Steven Rieger           :  Created Function.

var groupView:NotesView;
var groupMainDoc:NotesDocument;
var groupMembers:java.util.Vector;
var nestedGroupMembers = new Array();
var arrayOfMembers = new Array();
var mainIndex:Integer;
var nestedIndex:Integer;
var resultIndex:Integer;

print( "Begin jsLibGetGroupMembers - Passed in Name: " + groupName  );
groupView = addressBook.getView( "($VIMGroups)" )
if( groupView == null ) 
{
    print( "group document not found!" );
    return nestedGroupMembers;
}

//  gLibAdditionalErrorMessage = "Fetching Group Main Document " & groupName    
groupMainDoc = groupView.getDocumentByKey( groupName, true )

resultIndex = 0

//  Did we find a matching group document?
if( groupMainDoc != null ) 
{   //  is the group name already in the global list of groups?
    if( !(groupName.toLowerCase() in gListOfGroupNames ) )  
    {
        //  Add the group name to the globla list of groups to prevent processing duplicate groups
        gListOfGroupNames[ groupName.toLowerCase() ] = " "
        groupMembers = groupMainDoc.getItemValue( "Members" );
        for( groupMemberIndex = 0; groupMemberIndex < groupMembers.length; groupMemberIndex++ )
        {
            //  Fetch any nested group members if the current member is a group name
            nestedGroupMembers = jsLibGetGroupMembers( groupMembers[groupMemberIndex], addressBook );
            if ( typeof nestedGroupMembers[0] === 'undefined' || nestedGroupMembers[0] === null )
            {
                //  If no group was found and we have an actual member name add it to the list.
                if( groupMembers[groupMemberIndex].length > 0 ) 
                {
                    print( "adding user to array: " + groupMembers[groupMemberIndex] + " resultIndex = " + resultIndex );
                    arrayOfMembers[resultIndex] = groupMembers[groupMemberIndex];
                    resultIndex += 1;
                }
                else
                {
                    print( "No User to Add!");
                }
            }
            else
            {
                //  If this was a nested group we found add the members of that group to the list
                for( nestedGroupMemberIndex = 0; nestedGroupMemberIndex < nestedGroupMembers.length; nestedGroupMemberIndex++ )
                {
                    print( "adding nested user to array: " + nestedGroupMembers[nestedGroupMemberIndex] + " resultIndex = " + resultIndex );
                    arrayOfMembers[ resultIndex ] = nestedGroupMembers[nestedGroupMemberIndex];
                    resultIndex += 1;
                }
            } 
        }
    }
    else
        print( "Duplicate Group!");
}
else
{
    print( "no group doc found!" );
}
print( "exiting jsLibGetGroupMembers: " + "\n" + arrayOfMembers );
return arrayOfMembers;

}
4

1 回答 1

8

鉴于我最近的博客文章,这个答案无疑会显得很讽刺,但这里有一个 SSJS 实现,它将返回组层次结构中所有成员的排序列表:

var AtFunctions = (function() {

    function getGroupNames (groupRecord, serverName) {
        var result = new java.util.TreeSet();
        if (groupRecord) {
            for (var eachMember in groupRecord.getItemValue("Members")) {
                var nestedGroupRecord = getGroupRecord(eachMember, serverName);
                if (nestedGroupRecord) {
                    result.addAll(getGroupNames(nestedGroupRecord, serverName));
                    nestedGroupRecord.recycle();
                } else {
                    result.add(eachMember);
                }
            }
        }
        return result;
    }

    function getGroupRecord (groupName, serverName) {
        var result = null;
        try {
            serverName = (serverName || session.getServerName());
            var NAB = session.getDatabase(serverName, "names.nsf");
            var groupView = NAB.getView("($VIMGroups)");
            result = groupView.getDocumentByKey(groupName, true);
        } catch (e) {
            print("Error getting group record: " + e.toString());
        }
        return result;
    }

    var API = {
        expandNameList: function(groupName, serverName) {
            var result = new java.util.TreeSet();
            if (groupName) {
                var groupRecord = getGroupRecord(groupName, serverName);
                if (groupRecord) {
                    result.addAll(getGroupNames(groupRecord, serverName));
                    groupRecord.recycle();
                }
            }
            return result;
        }
    };

    return API;

})();

var @ExpandNameList = AtFunctions.expandNameList;

如果需要,最后的变量别名将允许您将其视为自定义@Function,因此您可以通过以下两种方式之一调用它:

var groupMembers = @ExpandNameList("All Employees");
var groupMembers = AtFunctions.expandNameList("All Employees");
// in either syntax, you can optionally pass a server name as a second argument

注意:它使用 Java TreeSet 类而不是 JavaScript 数组,它具有以下优点:

  1. 它是自动排序的。你可能不想要这个,但对于这种类型的操作,它通常很有用。
  2. 它会自动忽略重复项,因此即使同一成员存在于多个子组中,您也应该只获得每个名称的一个实例。
  3. 从语法上讲,它比数组更容易操作。

享受。:)

于 2013-02-27T01:27:06.963 回答