1

私は誰かが私をここで私の悲惨さから解放してくれることを望んでいます。JavaScriptでLotusScript関数を書き直しましたが、正常に機能していません。私はもうこのことを見ることができません。私は誰かが私がこれを理解するのを手伝ってくれることを望んでいます。

この関数は、グループの名前とアドレス帳のdbオブジェクトを取ります。ネストされたグループを含む、そのグループのすべてのメンバーをフェッチして返します。Lotusscriptバージョンではうまく機能しますが、Javascriptバージョンでは機能しません。これは、再帰関数と配列へのインデックスと関係があります。

2つのグループを作成する場合。最初のグループに10個の名前を入れ、2番目のグループに5個の名前を入れます。注:2番目のグループに先頭に#を付けて、最初のグループの先頭に並べ替えます。

グループ-A1グループ-Ablabla2 bla3 bla4 bla5 bla6 bla7 bla8 bla9 bla10

1グループ-その他1その他2その他3その他4その他5

Javascriptバージョンでは、関数グループAを渡すと、10個の名前が返されます。2番目のグループから5つ、最初のグループから2番目の5つ。

これがロータススクリプトバージョンのコードです

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として扱うことができるため、次の2つの方法のいずれかを呼び出すことができます。

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

注:JavaScript配列の代わりにJava TreeSetクラスを使用しているため、次の利点があります。

  1. 自動的にソートされます。これは望ましくないかもしれませんが、このタイプの操作では、多くの場合便利です。
  2. 重複は自動的に無視されるため、同じメンバーが複数のサブグループに存在する場合でも、各名前のインスタンスを1つだけ取得する必要があります。
  3. 構文的には、配列よりも操作がはるかに簡単です。

楽しみ。:)

于 2013-02-27T01:27:06.963 に答える