Page 1 sur 1

bouton de modification de valeurs de champs

MessagePublié: 27 Sep 2007 à 11:51
par billbock
à mettre ds un bouton de tool bar
Ce code dans un bouton permet de remplacer les agents du type

Field champ := valeur (utile pour modifier les valeurs d'un champ...)

il permet d'attribuer une valeur a un champ de n'importe quel format...

Code : Tout sélectionner
EditLastField := "edfLastField";
EditLastFieldDataType := "edfLastFieldDataType";
EditLastSeparator := "%~%";
ProfileName := "ChadSmiley Tools";
PromptTitle := @DbTitle + " - " + @ViewTitle;
MaxSearchForSelectedDocs := 5520;
MaxUpdatedDocuments := 1000;
ArraySeperator := ";";
NoteEntryLength := 11;
PormptNewLine := @Char(13) + @Char(13);

REM {Data types|@Function execution};
DataTypesCombo := @Explode(
"Integer|@TextToNumber( RawValue )$"+
"Integer Multi Value|@TextToNumber( @Explode( RawValue ; Separator; @True) )$"+
"Date|@ToTime( RawValue )$"+
"Date Multi Value|@ToTime( @Explode( RawValue ; Separator; @True) )$"+
"Text|@Text( RawValue )$"+
"Text Multi Value|@Text( @Explode( RawValue ; Separator; @True) )$"+
"Name|RawValue$"+
"Name Multi Value|RawValue$"+
"Common Name|@Name( [CN]; RawValue )$"+
"CommonName Multi Value|@Name( [CN]; @Explode( RawValue ; \":\"; @True ) )$"+
"Upper Case Text|@UpperCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Lower Case Text|@LowerCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Proper Case Text|@ProperCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Upper Case Text Multi Value|@UpperCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True ))$"+
"Lower Case Text Multi Value|@LowerCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True) )$"+
"Proper Case Text Multi Value|@ProperCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True) )$"+
"Replace Substring|@ReplaceSubstring( @GetField( EditField[ef] ); FromRawValue ; RawValue )$"+
"Replace|@Explode( @Replace( @GetField( EditField[ef] ) ; FromRawValue ; RawValue ) ; Separator ; @True )$"+
"Implode|@Implode( @Text( @GetField( EditField[ef] ) ) ; Separator )$"+
"Explode|@Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True )$"+
"Formula|@Eval( RawValue )$"+
"Abbreviate Name|@Name([Abbreviate]; RawValue )$"+
"Abbreviate Name Multi Value|@Name( [Abbreviate]; @Explode( RawValue ; Separator; @True ) )$"+
"Password Set|@Password( RawValue )$"+
"Password Convert|@Password( @GetField( EditField[ef] ) )$"+
"Remove Field|@DeleteField$"+
"Unique|@Unique(@GetField( EditField[ef]))$"+
"+ Append Values|@If(" +
"                                 @GetField(EditField[ef]) = \"\"; RawValue;" +
"                                 @Contains(DefaultDataType; \"Date\");" +
"                                         @If( @IsError( @ToTime( RawValue ) ) ;" +
"                                                 \"\" ;" +
"                                                 @SetField( EditField[ef] ; @GetField(EditField[ef]) : @TextToTime( @Explode( RawValue ; Separator ) ) ) ) ;" +
"                                 @Contains(DefaultDataType; \"Integer\" );" +
"                                         @If( @IsError( @TextToNumber( @Explode( RawValue ; Separator ) ) ) ;" +
"                                                 \"\" ;" +
"                                                 @SetField( EditField[ef] ; @GetField(EditField[ef]) : @TextToNumber( @Explode( RawValue ; Separator ) ) ) ) ;" +
"                                 @SetField( EditField[ef] ; @GetField(EditField[ef]) : @Explode( RawValue ; Separator ) ) )$"+
"Sort Ascending|@Sort(@GetField(EditField[ef]) ; [Ascending] )$"+
"Sort Descending|@Sort(@GetField(EditField[ef]); [Descending])" ; "$" );

DataTypes := @Word( DataTypesCombo ; "|" ; 1 );
DataTypesAction := @Word( DataTypesCombo ; "|" ; 2 );

REM {Get a listing of all the fields on the current document};
List := @Sort( @DocFields );

REM {Look for last field modified in Profile Doc};
FieldList := @Explode( @GetProfileField( ProfileName ; EditLastField ; @UserName ) ; ArraySeperator ; @True ) ;

REM {Get the list of forms and field that was updated using Edit Document Fields};
FieldListForms := @Word( FieldList ; EditLastSeparator ; 1 );
FieldListField := @Word( FieldList ; EditLastSeparator ; 2 );
FieldListLastIndex := @Member( Form; FieldListForms );
REM {If the FieldListLastIndex is greater than zero then set the last field to the what was in the profile document};
@If( FieldListLastIndex > 0;
        @Do( LastField := FieldListField[ FieldListLastIndex ];
                FieldList := @ReplaceSubstring( FieldList ; FieldList[ FieldListLastIndex ] ; "" ) );
        LastField :="" );

REM {Prompt for which field needs to be updated. Loop until a field is selected or 'Cancel' is selected};
@DoWhile(
        EditField := @Prompt( [OkCancelEditCombo] ; PromptTitle ; "Select the field you wish to alter:" ; LastField ; @Trim( @Unique( List : LastField ) ) );
        EditField = "" );
EditFieldPromptTitle := EditField + " - " + PromptTitle;

REM {This will allow the retrieval of the data type of the field that was last selected. Data is stored like Form+Field%~%DataType.};
FormFieldList := @Explode( @GetProfileField( ProfileName ; EditLastFieldDataType ; @UserName ) ; ArraySeperator ; @True ) ;
FormFieldListFormField := @Word( FormFieldList ; EditLastSeparator ; 1 );
FormFieldListDataType := @Word( FormFieldList ; EditLastSeparator ; 2 );
FormFieldListFormulaCode := @Word( FormFieldList ; EditLastSeparator ; 3 );
FormFieldListIndex := @Member( Form + EditField; FormFieldListFormField );
@If( FormFieldListIndex > 0;
        @Do( DefaultDataType := FormFieldListDataType[ FormFieldListIndex ];
                FormFieldListFormulaCode := FormFieldListFormulaCode[ FormFieldListIndex ];
                FormFieldList := @ReplaceSubstring( FormFieldList ; FormFieldList[ FormFieldListIndex ] ; "" ) );
        DefaultDataType :="" );

REM {If there was no data type used for the field on the form the try to determine the data type};
DefaultDataType :=
        @If( DefaultDataType != "" ;
                DefaultDataType ;
                @If(
                        @IsNumber( @GetField( EditField ) ) ;
                                @If( @Count( @GetField( EditField ) ) > 1 ;
                                        "Integer Multi Value" ;
                                        "Integer" ) ;
                        @IsTime( @GetField( EditField ) ) ;
                                @If( @Count( @GetField( EditField ) ) > 1 ;
                                        "Date Mult iValue" ;
                                        "Date" ) ;
                        @If( @Count( @GetField( EditField ) ) > 1 ;
                                        "Text Multi Value" ;
                                        "Text" )
                        )
        );

REM {If the data type is a type of error then select the data type of text};
DefaultDataType := @IfError( DefaultDataType ; "Text" );

REM {Prompt for which data type you would like the data to be. This needs to be done before value prompt to determine if the Picklist or any prompting needs to be used.};
DataType := @Prompt( [OkCancelList] ; EditFieldPromptTitle; "Please select the correct data type or action for field: " + EditField + "."; DefaultDataType ; DataTypes );

REM {The DataTypeAction will contain the formula that will be executed to retrieve the new value};
DataTypeAction := DataTypesAction[ @Member( DataType ; DataTypes ) ];

REM {If formula was used on this field then use that instead of the fields value. Format the original value as text because the @Prompt command requires text.};
OriginalValue := @If( DataType = "Formula" & DefaultDataType = "Formula" & FormFieldListFormulaCode != "" ;
        FormFieldListFormulaCode ;
        @If( @Contains( DefaultDataType ; MultiValue ) ;
                @Implode( @Text( @GetField( EditField ) ) ; ArraySeperator );
                @Text( @GetField( EditField ) ) )
        );

REM {Prompt for additional fields and determine the string that they are searching for.};
@If( DataType = @Explode( "Replace Substring;Replace" ; ArraySeperator ) ;
        @Do( EditField := @Unique( EditField : @Prompt( [OkCancelListMult] ; PromptTitle ; "Select any addtional fields you wish to alter:" ; EditField ; List ) );
                FromRawValue := @Prompt( [OkCancelEdit] ; EditFieldPromptTitle ; "Please enter the text (Case sensitive) to search for in: " + @Implode( EditField ; ", " ) + "." ; "" ) );
        @Do( EditField := EditField;
                FromRawValue := "" )
        );

Separator := @If( DataType = @Explode( "Implode;Explode" ; ArraySeperator ) ;
        @Prompt( [OkCancelEdit] ; PromptTitle ; "Enter the " + @If( DataType = "Implode" ; "separator" ; "separators" ) + ArraySeperator ; "" );
        ArraySeperator );

REM {Based on what type of data is being entered different prompts will happen if any at all.};
RawValue := @If(
        @Contains( DataType ; "Name Multi Value" ) ; @PickList( [Name] );
        @Contains( DataType ; "Name" ) ; @PickList( [Name] : [Single] );
        DataType = @Explode( "Remove Field;Unique;Sort Ascending;Sort Descending;Implode;Explode;Proper Case Text;Proper Case Text Multi Value;Lower Case Text;Lower Case Text Multi Value;Upper Case Text;Upper Case Text Multi Value;Password Convert" ; ArraySeperator ) ; "" ;
        @Contains( DataType ; "Multi Value" ) ; @Prompt( [OkCancelEdit] ; EditFieldPromptTitle; "Please enter the new desired value for: " + @Implode( EditField ; ", " ) + "." + PormptNewLine + "Seperated with ; for each value." ; OriginalValue ) ;
        @Contains( DataType ; "+ Append Values" ) ; @Prompt( [OkCancelEdit] ; EditFieldPromptTitle; "Please enter values to append: " + @Implode( EditField ; ", " ) + "." + PormptNewLine + "Seperated with ; for each value." ; "" ) ;
        DataType = @Explode("Replace Substring;Replace" ; ArraySeperator ) ; @Prompt( [OkCancelEdit] ; EditFieldPromptTitle ; "Please enter the text to repalce with in: " + EditField + "." ; "" ) ;
        @Prompt( [OkCancelEdit] ; EditFieldPromptTitle ; "Please enter the new desired value for: " + EditField + "." ; OriginalValue )
        );

REM {Store Field in Profile doc};
@SetProfileField( ProfileName ; EditLastField ; @Unique( @Explode( FieldList : ( Form + EditLastSeparator + EditField[1] ) ; ArraySeperator ; @False ) ); @UserName );

REM {Store Data Type of Field in Profile doc};
@SetProfileField( ProfileName ; EditLastFieldDataType ; @Unique( @Explode( FormFieldList : ( Form + EditField[1] + EditLastSeparator + DataType + EditLastSeparator + @If( DataType = "Formula" ; RawValue ; FormFieldListFormulaCode ) ) ; ArraySeperator ; @False ) ) ; @UserName );

REM {If multi docs selected, only process those checked ... an unchecked doc cannot be NavNextSelected};
@Command([NavNextSelected]);
@UpdateFormulaContext;

REM {Store all Note IDs before manipulation in case field modifications cause categorized views or sorted columns to reorganize};
NoteIDList := @Text( @NoteID );
@Command([NavNextSelected]);
@UpdateFormulaContext;

REM {Start Looping Selected documents};
@While( ( @Left( NoteIDList ; NoteEntryLength ) != ( @Text( @NoteID + ArraySeperator ) ) ) & ( @Length( NoteIDList ) < MaxSearchForSelectedDocs ) ;
        NoteIDList := NoteIDList + ArraySeperator + @Text( @NoteID );
        @Command([NavNextSelected]);
        @UpdateFormulaContext
);

NoteIDList := @Unique( @Explode( NoteIDList ; ArraySeperator ; @False ) );
@StatusBar( "Found " + @Text( @Elements( NoteIDList ) ) + " documents." );
NotNoteIDList := "";

REM {Loop through selected docs taking each NoteIDList out of the list as it is processed};
DocUpdateCount := 0;
DocNavigationCount := 0;
@While( DocUpdateCount < @Elements( NoteIDList ) ;
        NoteIDList := @Replace( NoteIDList ; @NoteID ; "" ) ;
        NotNoteIDList := NotNoteIDList : @NoteID;
        @For( ef := 1; ef <= @Elements( EditField ); ef := ef + 1;
                formulaResult := @Eval( DataTypeAction );
remark := " **REM** The values entered above will be applied to all selected doc. If data conversion doesn't work then don't set field.";
                @If( @IsError( formulaResult );
                        @Return( @Prompt( [Ok] ;"Error" ; @Text( FormulaResult ) ) );
                @SetField( EditField[ef] ; formulaResult )
                )
        );
        @If( DocNavigationCount > MaxUpdatedDocuments ;
                 NoteIDList := "";
                @Do(
                        DocUpdateCount := DocUpdateCount + 1;
                        @Command([NavNextSelected]);
                        @UpdateFormulaContext;
remark := " **REM** If we haven't processed all docs yet but the current doc is not in the NoteIDList list, keep looping ... if cnt exceeds MaxUpdatedDocuments assume infinite loop and stop ";
                        @If( DocUpdateCount < @Elements( NoteIDList ) & ( !@Member( @NoteID ; NoteIDList ) ) & ( !@Member( @NoteID ; NotNoteIDList) );
                                @While( (! @Member( @NoteID ; NoteIDList ) & DocNavigationCount < MaxUpdatedDocuments );
                                        @Command([NavNextSelected]);
                                        @UpdateFormulaContext;
                                        DocNavigationCount := DocNavigationCount + 1);
                        "")
                )
        )
);

@If( @Implode( @Unique( @Explode( NoteIDList ; ArraySeperator ; @False ) ) ) != "" ; @StatusBar( "Unable to update the following documents: " + @Implode( @Unique( @Explode( NoteIDList ; ArraySeperator ; @False ) ) ; ", " ) + "." ); "" );
@StatusBar( "Navigated through " + @Text( DocUpdateCount + DocNavigationCount ) + " documents." );
@StatusBar( "Performed '" + DataType + "' for '" + @Implode( EditField ; ", " ) + "' field" + @If( @Elements( EditField ) > 1; "s " ; " " ) + "on " + @Text( DocUpdateCount ) + " document" + @If( DocUpdateCount > 1 ; "s" ; "" ) + "." )

MessagePublié: 27 Sep 2007 à 12:13
par abertisch
Un autre code:

Code : Tout sélectionner
REM {Toolbar Functions 1.3.0 by Chad Schelfhout.};
REM {Visit http://www.chadsmiley.com/ToolbarFunctions for the latest updates};

REM {Constants};
ProfileName := "ChadSmiley Tools";
ToolbarToolsLastSelection := "ToolbarToolsLastSelection";
ToolbarToolsLastProfile := "ToolbarToolsLastProfile";
ToolbarToolsLastEnvironmentVariable := "ToolbarToolsLastEnvironmentVariable";
ToolbarToolsLastFormula := "ToolbarToolsLastFormula";
DefaultSelection := "Undo Replication Conflict";
cSemicolonReplace := "#SC#";

REM {Get last selection for this database};
ToolbarToolsSelection := @GetProfileField( ProfileName ; ToolbarToolsLastSelection ; @UserName ) ;

Option := @Prompt( [OkCancelCombo] ; "Select Option" ; "What would you like to do:" ; @If( ToolbarToolsSelection = "" ; DefaultSelection ; ToolbarToolsSelection ) ; @Explode( "Undo Replication Conflict:Unlock Document:Refresh Selected Documents:Link Message:Workflow Formula Test:Agent Run:Agent Run Current:Note ID:UNID:Parent UNID:Database Replica ID:Database Path:Database Server:Database Server & Path:Edit Profile Document:Get Environment Value:Set Environment Value:Formula Evaluate:Authors:Document Edit:Document Open"  ; ":" ) ) ;

REM {Set last selection for this database};
@SetProfileField( ProfileName ; ToolbarToolsLastSelection ; Option ; @UserName ) ;

@If(

Option = "Refresh Selected Documents" ; @Command([ToolsRefreshSelectedDocs])  ;

Option = "Undo Replication Conflict" ; @Do( @SetField("$REF" ; @DeleteField ) : @SetField( "$Conflict" ; @DeleteField ) );

Option = "Unlock Document" ; @Do(
   @SetField("$Writers" ; @DeleteField ) ;
   @SetField("$PWriters" ; @DeleteField ) ;
   @SetField( "$WritersDate" ; @DeleteField ) ) ;

Option = "Agent Run" ; @Do ( @Command([RunAgent] ; @Prompt( [OkCancelEdit];"Run Agent" ; "Enter agent to run"; "" ) ) ) ;

Option = "Agent Run Current" ; @Do ( @Command([ToolsRunMacro]; $Title) ) ;

Option = "Link Message" ; @Do ( @Command([Compose]; @MailDbName; "Bookmark") ) ;

Option = "Edit Profile Document" ; @Do (
   @SetProfileField( ProfileName ; ToolbarToolsLastProfile ;
      @Trim(
         @Unique(
            @Prompt([OkCancelEditCombo];
               "Open Profile Document";
               "Profile Document Name (There should be a corresponding form or suborm)";
               @Subset( @Explode( @GetProfileField( ProfileName ; ToolbarToolsLastProfile ; @UserName ) ; ";" ) ;1 ) ;
               @Explode( @GetProfileField( ProfileName ; ToolbarToolsLastProfile ; @UserName ) ; ";" ) ) :
            @GetProfileField( ProfileName ; ToolbarToolsLastProfile ; @UserName ) ) );
      @UserName ) ;
   @Command([EditProfile] ;
      @Text( @Subset( @Explode( @GetProfileField( ProfileName ; ToolbarToolsLastProfile ; @UserName ) ; ";" ) ;1 )  ) ;
      @If( @Prompt( [YesNo] ; "Personal Profile" ; "Is '" + @Subset( @Explode( @GetProfileField( ProfileName ; ToolbarToolsLastProfile ; @UserName ) ; ";" ) ;1 ) + "' a personal profile?" ) ; @UserName ; "" ) ) );

Option = "Get Environment Value" ; @Do (
   CurrentEnvVariable := @GetProfileField( ProfileName ; ToolbarToolsLastEnvironmentVariable ; @UserName ) ) :
   @Set( "NewEnvVariable" ; @Prompt([OkCancelEditCombo]; "Get Environment Value"; "Enter the name of the environment variable:"; @Subset( CurrentEnvVariable ; 1 ); CurrentEnvVariable ) ) :
   @SetProfileField( ProfileName ; ToolbarToolsLastEnvironmentVariable ; @Trim( @Unique( NewEnvVariable : CurrentEnvVariable ) ); @UserName ) :
   @Prompt([OkCancelEdit]; "Environment Value"; "The value of the evironment variable '" + NewEnvVariable +  "':"  ;  @Environment( NewEnvVariable ) );

Option = "Set Environment Value" ; @Do (
   CurrentEnvVariable := @GetProfileField( ProfileName ; ToolbarToolsLastEnvironmentVariable ; @UserName ) ) :
   @Set( "NewEnvVariable" ; @Prompt([OkCancelEditCombo]; "Set Environment Value"; "Enter the name of the environment variable:"; @Subset( CurrentEnvVariable ; 1 ); CurrentEnvVariable ) ) :
   @SetProfileField( ProfileName ; ToolbarToolsLastEnvironmentVariable ; @Trim( @Unique( NewEnvVariable : CurrentEnvVariable ) ); @UserName ) :
   @SetEnvironment( NewEnvVariable ; @Prompt([OkCancelEdit]; "Environment Value"; "The new value of the evironment variable '" + NewEnvVariable +  "':"  ;  @Environment( NewEnvVariable ) ) );

Option = "Parent UNID" ; @Do ( @Prompt([OkCancelEdit]; "Parent UNID"; "Parent Unique ID"; @Text($REF)) ) ;

Option = "UNID" ; @Do ( @Prompt([OkCancelEdit]; "UNID"; "Unique ID"; @Text(@DocumentUniqueID)) ) ;

Option = "Database Replica ID" ; @Do ( @Prompt([OkCancelEdit]; "Database Replica ID"; "Database Replica ID"; @Text(@ReplicaID))  ) ;

Option = "Database Path" ; @Do ( @Prompt([OkCancelEdit]; "Database Path"; "Database Path"; @Text(@Subset( @DbName ; -1 ) ) )  ) ;

Option = "Database Server" ; @Do ( @Prompt([OkCancelEdit]; "Database Server"; "Database Server"; @Text(@Subset( @DbName ; 1 ) ) )  ) ;

Option = "Database Server & Path" ; @Do ( @Prompt([OkCancelEdit]; "Database Server & Path"; "Database Server & Path"; @Text( @Implode( @DbName ; "!!" ) ) )  ) ;

Option = "Note ID" ; @Do ( @Prompt([OkCancelEdit]; "Note ID"; "Note ID"; @Text(@NoteID))  ) ;

Option = "Formula Evaluate" ; @Do (
   CurrentForumla := @GetProfileField( ProfileName ; ToolbarToolsLastFormula ; @UserName ) ) :
   @Set( "NewFormula" ;
      @Prompt([OkCancelEditCombo];
         "Formula to Evaluate";
         "Enter the formula to evaluate:";
         @ReplaceSubstring( @Subset( CurrentForumla ; 1 ) ; cSemicolonReplace ; ";" );
         @ReplaceSubstring( CurrentForumla ; cSemicolonReplace ; ";" ) ) ) :
   @SetProfileField( ProfileName ;
      ToolbarToolsLastFormula ;
      @Trim( @Unique(  @ReplaceSubstring( NewFormula ; ";" ; cSemicolonReplace ) : CurrentForumla ) ); @UserName ) :
   @Prompt([OkCancelEdit]; "Formula Evaluated"; "The evaluate result:"  ; @Implode( @Text( @Eval ( NewFormula ) ) ) );

Option = "Workflow Formula Test" ; @Do ( @Command([Compose]; @DbName; "(OS FormulaTest)") ) ;

Option = "Authors" ; @Do ( @Prompt([OkCancelEdit]; "Authors"; "List of people/groups that have author access to document:"; @Implode( @Name( [Abbreviate]; @Author ) ; "," ) ) ) ;

Option = ("Document Open":"Document Edit" ) ;
@If( @Unique(@Text(@DocumentUniqueID)) = @Repeat("0"; 32 ) ; @Prompt( [Ok]; "Unable to Continue"; "A document needs to be selected.") ; @Do(
   host:=@DbLookup("":""; @Subset(@DbName; 1) : "names.nsf"; "($Servers)"; @Subset(@DbName; 1); "SMTPFullHostDomain" ; [FailSilent] );
   host:=@If(@IsError(host) ; "localhost" ; host );
   normalMode:=@DbLookup("":""; @Subset(@DbName; 1) : "names.nsf"; "($Servers)"; @Subset(@DbName; 1); "HTTP_NormalMode" ; [FailSilent] );
   normalMode:=@If(@IsError(normalMode) ; "1" ; normalMode );
   port:=@DbLookup("":""; @Subset(@DbName; 1) : "names.nsf"; "($Servers)"; @Subset(@DbName; 1); "HTTP_Port" ; [FailSilent] );
   port:=@If(@IsError(port) ; "80" ; port );
   sslPort:=@DbLookup("":""; @Subset(@DbName; 1) : "names.nsf"; "($Servers)"; @Subset(@DbName; 1); "HTTP_SSLPort" ; [FailSilent] );
   sslPort:=@If(@IsError(port) ; "443" ; port );

   prefix:=@If(
      normalMode = "1" | normalMode = "2" ; "http";
      "https"
   );

   portSuffix:=@If(
      prefix = "http" & port = 80; "";
      prefix = "https" & sslPort = "443"; "";
      prefix = "http"; ":" + @Text(port);
      prefix = "https"; ":" + @Text(sslPort);
      ""
   );

   @URLOpen(prefix + "://" + host + portSuffix + "/" + @WebDbName + "/0/" + @Text(@DocumentUniqueID) + "?" + @If( Option = "Document Open" ; "OpenDocument" ; "EditDocument" ) ) ) ) ;
"" )

MessagePublié: 16 Oct 2007 à 08:09
par abertisch
Code : Tout sélectionner
REM {Edit Document Fields 6.0.0 by Chad Schelfhout.};
REM {Visit http://www.chadsmiley.com/EditDocumentFields for the latest updates};

REM {Changable constants};
cEnableConfirmation := @True;
cFieldHistoryValues := 10;
cStandardSeparators := ":" : ";" : " ";
cPromptTitle := @DbTitle + " - " + @ViewTitle;

REM {Unchangable constants};
cProfileName := "ChadSmiley Tools";
cEditLastField := "edfLastField";
cEditLastFieldDataType := "edfLastFieldDataType";
cEditLastFieldDataTypeValue := "edfLastFieldDataTypeValue";
cEditLastSeparator := "%~%";
cValueListSeparator := "^";
cFromRawValueSeparator := "%@%";
cSemicolonReplace := "#SC#";
cMaxSearchForSelectedDocs := 5520;
cMaxUpdatedDocuments := 1000;
cArraySeparator := ";";
cNoteEntryLength := 11;
cPromptNewLineOne := @Char(13);
cPromptNewLineTwo := cPromptNewLineOne + cPromptNewLineOne;
cPromptTab := @Char(9);
cCategoryNoteID := "NT00000000";
cTextExtractList := "Text Left":"Text Left Back":"Text Right":"Text Right Back";
cNoPromptList := cTextExtractList:"Remove Field":"Unique":"Sort Ascending":"Sort Descending":"Implode":"Explode":"Proper Case Text":"Proper Case Text Multi Value":"Lower Case Text":"Lower Case Text Multi Value":"Upper Case Text":"Upper Case Text Multi Value":"Password Convert":"Trim":"Trim then Unique";
cErrorCheckCode := "@Implode( @Unique( @Explode( NoteIDList : ErrorNoteID ; cArraySeparator ; @False ) ) )";
cErrorInformation := "\"Error documents: \" + @Implode( @Unique( @Explode( ErrorNoteIDList ; cArraySeparator ; @False ) ) ; \", \" ) + cPromptNewLineOne + \"Not updated documents: \" + @Implode( @Unique( @Explode( ErrorNoteIDList ; cArraySeparator ; @False ) ) ; \", \" )";


REM {Data types|@Function execution};
DataTypesCombo := @Explode(
"Integer|@TextToNumber( RawValue )$"+
"Integer Multi Value|@TextToNumber( @Explode( RawValue ; Separator; @True) )$"+
"Date|@ToTime( RawValue )$"+
"Date Multi Value|@ToTime( @Explode( RawValue ; Separator; @True) )$"+
"Text|@Text( RawValue )$"+
"Text Multi Value|@Text( @Explode( RawValue ; Separator; @True) )$"+
"Text Left|@Left( @Text( @GetField( EditField[ef] ) ) ; ExtractValue )$"+
"Text Left Back|@LeftBack( @Text( @GetField( EditField[ef] ) ) ; ExtractValue )$"+
"Text Right|@Right( @Text( @GetField( EditField[ef] ) ) ; ExtractValue )$"+
"Text Right Back|@RightBack( @Text( @GetField( EditField[ef] ) ) ; ExtractValue )$"+
"Trim|@Trim( @Text( @GetField( EditField[ef] ) ) )$"+
"Trim then Unique|@Unique(@Trim( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Name|RawValue$"+
"Name Multi Value|RawValue$"+
"Common Name|@Name( [CN]; RawValue )$"+
"Common Name Multi Value|@Name( [CN]; @Explode( RawValue ; \":\"; @True ) )$"+
"Upper Case Text|@UpperCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Lower Case Text|@LowerCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Proper Case Text|@ProperCase( @Implode( @Text( @GetField( EditField[ef] ) ) ) )$"+
"Upper Case Text Multi Value|@UpperCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True ))$"+
"Lower Case Text Multi Value|@LowerCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True) )$"+
"Proper Case Text Multi Value|@ProperCase( @Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True) )$"+
"Replace Substring|@ReplaceSubstring( @GetField( EditField[ef] ); FromRawValue ; RawValue )$"+
"Replace|@Explode( @Replace( @GetField( EditField[ef] ) ; FromRawValue ; RawValue ) ; Separator ; @True )$"+
"Implode|@Implode( @Text( @GetField( EditField[ef] ) ) ; Separator )$"+
"Explode|@Explode( @Text( @GetField( EditField[ef] ) ) ; Separator; @True )$"+
"Formula|@Eval( RawValue )$"+
"Abbreviate Name|@Name([Abbreviate]; RawValue )$"+
"Abbreviate Name Multi Value|@Name( [Abbreviate]; @Explode( RawValue ; Separator; @True ) )$"+
"Password Set|@Password( RawValue )$"+
"Password Convert|@Password( @GetField( EditField[ef] ) )$"+
"Remove Field|@DeleteField$"+
"Unique|@Unique(@GetField( EditField[ef]))$"+
"+ Append Values|@If(" +
"     @GetField(EditField[ef]) = \"\"; RawValue;" +
"     @Contains(DefaultDataType; \"Date\");" +
"      @If( @IsError( @ToTime( RawValue ) ) ;" +
"       \"\" ;" +
"       @SetField( EditField[ef] ; @GetField(EditField[ef]) : @TextToTime( @Explode( RawValue ; Separator ) ) ) ) ;" +
"     @Contains(DefaultDataType; \"Integer\" );" +
"      @If( @IsError( @TextToNumber( @Explode( RawValue ; Separator ) ) ) ;" +
"       \"\" ;" +
"       @SetField( EditField[ef] ; @GetField(EditField[ef]) : @TextToNumber( @Explode( RawValue ; Separator ) ) ) ) ;" +
"     @SetField( EditField[ef] ; @GetField(EditField[ef]) : @Explode( RawValue ; Separator ) ) )$"+
"Sort Ascending|@Sort(@GetField(EditField[ef]) ; [Ascending] )$"+
"Sort Descending|@Sort(@GetField(EditField[ef]); [Descending])" ; "$" );

DataTypes := @Word( DataTypesCombo ; "|" ; 1 );
DataTypesAction := @Word( DataTypesCombo ; "|" ; 2 );

REM {Get a listing of all the fields on the current document};
List := @Sort( @DocFields );

REM {Look for last field modified in Profile Doc};
FieldList := @Explode( @GetProfileField( cProfileName ; cEditLastField ; @UserName ) ; cArraySeparator ; @True ) ;

REM {Get the list of forms and field that was updated using Edit Document Fields};
FieldListForms := @Word( FieldList ; cEditLastSeparator ; 1 );
FieldListField := @Word( FieldList ; cEditLastSeparator ; 2 );
FieldListLastIndex := @Member( Form; FieldListForms );
REM {If the FieldListLastIndex is greater than zero then set the last field to the what was in the profile document};
@If( FieldListLastIndex > 0;
 @Do( LastField := FieldListField[ FieldListLastIndex ];
  FieldList := @ReplaceSubstring( FieldList ; FieldList[ FieldListLastIndex ] ; "" ) );
 LastField :="" );

REM {Prompt for which field needs to be updated. Loop until a field is selected or 'Cancel' is selected};
@DoWhile(
 EditField := @Prompt( [OkCancelEditCombo] ; cPromptTitle ; "Select the field you wish to alter or enter a new field to add:" ; LastField ; @Trim( @Unique( List : LastField ) ) );
 EditField = "" );
EditFieldPromptTitle := "Change '" + EditField + "' in " + cPromptTitle;

REM {This will allow the retrieval of the data type of the field that was last selected. Data is stored like Form+Field%~%DataType.};
FormFieldList := @Explode( @GetProfileField( cProfileName ; cEditLastFieldDataType ; @UserName ) ; cArraySeparator ; @True ) ;
FormFieldListFormField := @Word( FormFieldList ; cEditLastSeparator ; 1 );
FormFieldListDataType := @Word( FormFieldList ; cEditLastSeparator ; 2 );
FormFieldListFormulaCode := @ReplaceSubstring( @Word( FormFieldList ; cEditLastSeparator ; 3 ) ; cSemicolonReplace ; ";" );
FormFieldListIndex := @Member( Form + EditField; FormFieldListFormField );
@If( FormFieldListIndex > 0;
 @Do( DefaultDataType := FormFieldListDataType[ FormFieldListIndex ];
  FormFieldListFormulaCode := FormFieldListFormulaCode[ FormFieldListIndex ];
  FormFieldList := @ReplaceSubstring( FormFieldList ; FormFieldList[ FormFieldListIndex ] ; "" ) );
 DefaultDataType :="" );

REM {If there was no data type used for the field on the form the try to determine the data type};
DefaultDataType :=
 @If( DefaultDataType != "" ;
  DefaultDataType ;
  @If(
   @IsNumber( @GetField( EditField ) ) ;
    @If( @Count( @GetField( EditField ) ) > 1 ;
     "Integer Multi Value" ;
     "Integer" ) ;
   @IsTime( @GetField( EditField ) ) ;
    @If( @Count( @GetField( EditField ) ) > 1 ;
     "Date Mult iValue" ;
     "Date" ) ;
   @If( @Count( @GetField( EditField ) ) > 1 ;
     "Text Multi Value" ;
     "Text" )
   )
 );

REM {If the data type is a type of error then select the data type of text};
DefaultDataType := @IfError( DefaultDataType ; "Text" );

REM {Prompt for which data type you would like the data to be. This needs to be done before value prompt to determine if the Picklist or any prompting needs to be used.};
DataType := @Prompt( [OkCancelList] ; EditFieldPromptTitle; "Please select the correct data type or action for field: " + EditField + "."; DefaultDataType ; DataTypes );

REM {The DataTypeAction will contain the formula that will be executed to retrieve the new value};
DataTypeAction := DataTypesAction[ @Member( DataType ; DataTypes ) ];

REM {If formula was used on this field then use that instead of the fields value. Format the original value as text because the @Prompt command requires text.};
OriginalValue := @If( DataType = "Formula" & DefaultDataType = "Formula" & FormFieldListFormulaCode != "" ;
 FormFieldListFormulaCode ;
 @If( @Contains( DefaultDataType ; MultiValue ) ;
  @Implode( @Text( @GetField( EditField ) ) ; cArraySeparator );
  @Text( @GetField( EditField ) ) )
 );

REM {This will allow the retrieval of history of values of the field. Data is stored like Form+Field+DataType%~%ValueList.};
FormFieldListDataTypeValues := @Explode( @GetProfileField( cProfileName ; cEditLastFieldDataTypeValue ; @UserName ) ; cArraySeparator ; @True ) ;
FormFieldListFormFieldDataType := @Word( FormFieldListDataTypeValues ; cEditLastSeparator ; 1 ) ;
FormFieldListValuesLists := @Word( FormFieldListDataTypeValues ; cEditLastSeparator ; 2 ) ;
FormFieldListDTIndex := @Member( Form + EditField + DataType; FormFieldListFormFieldDataType );
@If( FormFieldListDTIndex > 0;
 @Do( FormFieldListValuesList := FormFieldListDataTypeValues[ FormFieldListDTIndex ];
  FormFieldListValuesList := @ReplaceSubstring( @Trim( @Explode( FormFieldListValuesLists[ FormFieldListDTIndex ] ; cValueListSeparator ; @False ) ) ; cSemicolonReplace; ";" );
  FormFieldListDataTypeValues := @ReplaceSubstring( FormFieldListDataTypeValues ; FormFieldListDataTypeValues[ FormFieldListDTIndex ] ; "" ) );
 FormFieldListValuesList :="" );

REM {Prompt for additional fields and determine the string that they are searching for.};
@If( DataType = ("Replace Substring":"Replace" ) ;
 @Do( EditField := @Unique( EditField : @Prompt( [OkCancelListMult] ; cPromptTitle ; "Select any addtional fields you wish to alter:" ; EditField ; List ) );
  FromRawValue := @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle ; "Enter or select the text (case sensitive) to search for in: " + @Implode( EditField ; ", " ) + "." ; "" ; @Unique( @Word( FormFieldListValuesList ; cFromRawValueSeparator ; 2 ) ) ) );
 @Do( EditField := EditField;
  FromRawValue := "" )
 );

REM { With the Edit combo there will be a list of standard seperators to choose from.};
Separator := @If( DataType = ("Implode":"Explode" ) ;
 @Prompt( [OkCancelEditCombo] ; cPromptTitle ; "Enter or select the " + @If( DataType = "Implode" ; "separator" ; "separators" ) + ":"  ; "" ; @Unique( @If( FormFieldListValuesList = "" ; cStandardSeparators  ;  FormFieldListValuesList : cStandardSeparators ) ) );
 cArraySeparator );

REM {Determine the string to search for};
ExtractValue := @If( DataType = cTextExtractList ;
 @Prompt( [OkCancelEditCombo] ; cPromptTitle ; "Enter or select the search string or string length:" ; @Subset( FormFieldListValuesList ; 1 ) ; @Unique( FormFieldListValuesList ) );
 "" );

REM {Based on what type of data is being entered different prompts will happen if any at all.};
RawValue := @If(
 @Contains( DataType ; "Name Multi Value" ) ; @PickList( [Name] );
 @Contains( DataType ; "Name" ) ; @PickList( [Name] : [Single] );
 DataType = ( cNoPromptList ) ; "" ;
 @Contains( DataType ; "Multi Value" ) ; @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle; "Enter or select the new desired value for: " + @Implode( EditField ; ", " ) + "." + cPromptNewLineTwo + "Seperated with ; for each value." ; OriginalValue ; @Unique( OriginalValue : FormFieldListValuesList ) ) ;
 @Contains( DataType ; "+ Append Values" ) ; @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle; "Enter or select values to append: " + @Implode( EditField ; ", " ) + "." + cPromptNewLineTwo + "Seperated with ; for each value." ; "" ; @Unique( FormFieldListValuesList ) ) ;
 DataType = ("Replace Substring":"Replace" ) ; @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle ; "Enter or select the text to repalce with in: " + EditField + "." ; "" ; @Unique( @Word( FormFieldListValuesList ; cFromRawValueSeparator ; 1 ) ) ) ;
 DataType = "Formula" ; @Do( @DoWhile(
  OriginalValue := @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle ; "Enter or select the new desired formula for: " + EditField + "." ; OriginalValue ; @Unique( OriginalValue : FormFieldListValuesList ) );
  tempReturnCheck := @CheckFormulaSyntax( OriginalValue );
  @If( tempReturnCheck != "1"; @Prompt( [Ok] ; "Invalid Formula - " + EditFieldPromptTitle ;
   "Invalid Formula entered: " +
   cPromptNewLineTwo + cPromptTab + "Error: " + cPromptTab + cPromptTab + @Text( tempReturnCheck ) +
   cPromptNewLineOne + cPromptTab + "Formula: " + cPromptTab +  cPromptTab + OriginalValue ) ; "" );
 tempReturnCheck != "1" );
 OriginalValue );
 @Prompt( [OkCancelEditCombo] ; EditFieldPromptTitle ; "Enter or select the new desired value for: " + EditField + "." ; OriginalValue  ; @Unique( OriginalValue : FormFieldListValuesList ) )
 );

REM {Store Field in Profile doc};
@SetProfileField( cProfileName ; cEditLastField ; @Unique( @Trim( FieldList : ( Form + cEditLastSeparator + EditField[1] ) ) ); @UserName );

REM {Store Data Type of Field in Profile doc};
@SetProfileField( cProfileName ; cEditLastFieldDataType ;
 @Unique( @Trim( FormFieldList : ( Form + EditField[1] + cEditLastSeparator + DataType + cEditLastSeparator +
  @ReplaceSubstring(
   @If( DataType = "Formula" ; RawValue ; FormFieldListFormulaCode ) ;
   ";" ; cSemicolonReplace ) ) ) ) ;
 @UserName );

REM {Store Data Value of Field in Profile doc};
@SetProfileField( cProfileName ; cEditLastFieldDataTypeValue ;
 @Unique( @Trim( FormFieldListDataTypeValues : ( Form + EditField[1] + DataType + cEditLastSeparator +
  @Implode(
   @Subset(
    @Unique(
    @ReplaceSubstring(
     @If( DataType = ("Implode":"Explode" ) ; Separator ;
      DataType = cTextExtractList ; ExtractValue ;
      DataType = ( "Replace Substring":"Replace" ) ; RawValue + cFromRawValueSeparator + FromRawValue ;
      RawValue ) : FormFieldListValuesList  ;
     ";" ; cSemicolonReplace ) ) ;
    cFieldHistoryValues );
   cValueListSeparator ) ) ) ) ;
 @UserName );

REM {If multi docs selected, only process those checked ... an unchecked doc cannot be NavNextSelected};
@Command([NavNextSelected]);
@UpdateFormulaContext;

REM {Store all Note IDs before manipulation in case field modifications cause categorized views or sorted columns to reorganize};
NoteIDList := @Text( @NoteID );
ErrorNoteIDList := "";
@Command([NavNextSelected]);
@UpdateFormulaContext;

REM {Start Looping Selected documents to gather all the documents that need to be updated.};
@While( ( @Left( NoteIDList ; cNoteEntryLength ) != ( @Text( @NoteID + cArraySeparator ) ) ) & ( @Length( NoteIDList ) < cMaxSearchForSelectedDocs ) ;
 NoteIDList := NoteIDList + cArraySeparator + @Text( @NoteID );
 NoteIDList := @ReplaceSubstring( NoteIDList ; cCategoryNoteID + cArraySeparator ; "" );
 @Command([NavNextSelected]);
 @UpdateFormulaContext
);
REM {Remove all category Note IDs};
NoteIDList := @ReplaceSubstring( NoteIDList ; cCategoryNoteID ; "" );
REM {Remove all duplicate Note IDs};
NoteIDList := @Unique( @Explode( NoteIDList ; cArraySeparator ; @False ) );
@StatusBar( "Found " + @Text( @Elements( NoteIDList ) ) + " documents." );
NotNoteIDList := "";

REM {Determine if the document should be updated.};
tmpPrompt := @Implode( "The following information will be used to update the " + @Text( @Elements( NoteIDList ) ) + " document" + @If( @Elements( NoteIDList ) > 1 ; "s" ; "" ) + "." + cPromptTab + cPromptTab +
 cPromptNewLineTwo + cPromptTab + "Field:" + cPromptTab + cPromptTab + EditField +
 cPromptNewLineOne + cPromptTab + "Data type/action: " + cPromptTab + DataType +
 cPromptNewLineOne + cPromptTab +
  @If(  DataType = ("Implode":"Explode" ) ; "Separator:  " + cPromptTab + Separator ;
    DataType = ("Text Left":"Text Left Back":"Text Right":"Text Right Back" ) ; "Search string:  " + cPromptTab + ExtractValue ;
    DataType = ("Replace Substring":"Replace" ) ; "Search string:  " + cPromptTab + FromRawValue + cPromptNewLineOne + cPromptTab + "Replace string:  " + cPromptTab + RawValue ;
    DataType = cNoPromptList;
    "" ;
    "Value:  " + cPromptTab + cPromptTab + @Text( RawValue ) ) +
 cPromptNewLineTwo + "Would you like to continue?" );
@If( cEnableConfirmation ;
 @Do(
   @StatusBar( @Explode( tmpPrompt ; cPromptNewLineOne ; @True ) );
  @If( @Prompt( [YesNo]; EditFieldPromptTitle ;
   tmpPrompt );
   "" ; @Return ( "" ) ) );
 "" );

REM {Loop through selected docs taking each NoteIDList out of the list as it is processed};
DocUpdateCount := 0;
DocNavigationCount := 0;
@While( DocUpdateCount < @Elements( NoteIDList ) ;

 @If( @TextToNumber( @Text( @DocumentUniqueID ) ) != 0 ;
  @Do(
   NoteIDList := @Replace( NoteIDList ; @NoteID ; "" ) ;
   NotNoteIDList := NotNoteIDList : @NoteID;
   @For( ef := 1; ef <= @Elements( EditField ); ef := ef + 1;
    formulaResult := @Eval( DataTypeAction );
remark := " **REM** The values entered above will be applied to all selected doc. If data conversion doesn't work then don't set field.";
    @If( @IsError( formulaResult );
     @Do(
      tmpPrompt := "Error with NoteID of " + @NoteID + ". Continue?" + cPromptTab +
      cPromptNewLineOne + cPromptTab + "Error: " + cPromptTab + cPromptTab + @Text( FormulaResult ) +
      cPromptNewLineOne + cPromptTab + "Formula: " + cPromptTab +  cPromptTab + DataTypeAction +
      cPromptNewLineOne + cPromptTab + @If( @Contains( DataTypeAction ; "EditField[ef]" ) ; "EditField[ef]:" ; "Field:" + cPromptTab ) + cPromptTab + EditField[ef] +
      cPromptNewLineOne + cPromptTab + "Data type/action: " + cPromptTab + DataType +
      cPromptNewLineOne + cPromptTab +
      @If(  DataType = ("Replace Substring":"Replace" ) ; "FromRawValue:  " + cPromptTab + @Text( FromRawValue )+ cPromptNewLineOne + cPromptTab + "RawValue:  " + cPromptTab + @Text( RawValue ) ;
        DataType = cNoPromptList;
        "" ;
        "RawValue:  " + cPromptTab + @Text( RawValue ) );
     @StatusBar( @Explode( tmpPrompt ; cPromptNewLineOne ; @True ) );
     @If( @Prompt( [YesNo] ;"Error - " + EditFieldPromptTitle ; tmpPrompt ) ;
       ErrorNoteIDList := ErrorNoteIDList+ cArraySeparator + @Text( @NoteID );
      @Return( @If( @Eval( cErrorCheckCode ) != "" ;
       @StatusBar( @Explode( @Eval( cErrorInformation ) ; cPromptNewLineOne ; @True ) ):
       @Prompt( [Ok] ; "Unable to Update - " + EditFieldPromptTitle ; @Eval( cErrorInformation ) );
      "" ) ) ) );
    @SetField( EditField[ef] ; formulaResult )
    )
   );
   @If( DocNavigationCount > cMaxUpdatedDocuments ;
     NoteIDList := "";
    @Do(
     DocUpdateCount := DocUpdateCount + 1;
     @Command([NavNextSelected]);
     @UpdateFormulaContext;
remark := " **REM** If we haven't processed all docs yet but the current doc is not in the NoteIDList list, keep looping ... if cnt exceeds MaxUpdatedDocuments assume infinite loop and stop ";
     @If( DocUpdateCount < @Elements( NoteIDList ) & ( !@Member( @NoteID ; NoteIDList ) ) & ( !@Member( @NoteID ; NotNoteIDList) );
      @While( (! @Member( @NoteID ; NoteIDList ) & DocNavigationCount < cMaxUpdatedDocuments );
       @Command([NavNextSelected]);
       @UpdateFormulaContext;
       DocNavigationCount := DocNavigationCount + 1);
     "")
    )
   )
  );
 @Do( @Command([NavNextSelected]);
  @UpdateFormulaContext )
 )
);

@If( @Implode( @Unique( @Explode( NoteIDList : ErrorNoteIDList ; cArraySeparator ; @False ) ) ) != "" ;
 @StatusBar( @Explode( @Eval( cErrorInformation ) ; cPromptNewLineOne ; @True ) ):
 @Prompt( [Ok] ; "Unable to Update - " + EditFieldPromptTitle ; @Eval( cErrorInformation ) );
 "" );

@StatusBar( "Navigated through " + @Text( DocUpdateCount + DocNavigationCount ) + " documents." );
@StatusBar( "Performed '" + DataType + "' for '" + @Implode( EditField ; ", " ) + "' field" + @If( @Elements( EditField ) > 1; "s " ; " " ) + "on " + @Text( DocUpdateCount ) + " document" + @If( DocUpdateCount > 1 ; "s" ; "" ) + "." )


Toujours le même auteur mais autre fonction. :wink:

Il faut mettre le code dans un bouton dans la toolbar.

Ce code permet de mettre un jour des champs dans plusieurs documents en même temps.

La liste de ce que peut faire ce code est longue.

ATTENTION quand même à son utilisation, il n'est pas rare de faire planter Notes. :wink: