1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Data ;
4
+ using System . Linq ;
5
+ using System . Windows . Forms ;
6
+ using System . Xml . Serialization ;
7
+ using taskt . UI . CustomControls ;
8
+ using taskt . UI . Forms ;
9
+
10
+ namespace taskt . Core . Automation . Commands
11
+ {
12
+ [ Serializable ]
13
+ [ Attributes . ClassAttributes . Group ( "Excel Commands" ) ]
14
+ [ Attributes . ClassAttributes . Description ( "This command opens an Excel Workbook." ) ]
15
+ [ Attributes . ClassAttributes . UsesDescription ( "Use this command when you want to open an existing Excel Workbook." ) ]
16
+ [ Attributes . ClassAttributes . ImplementationDescription ( "This command implements Excel Interop to achieve automation." ) ]
17
+ public class LoadDictionaryCommand : ScriptCommand
18
+ {
19
+ [ XmlAttribute ]
20
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please Enter the Dictionary Name" ) ]
21
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter the unique instance name that was specified in the **Create Excel** command" ) ]
22
+ [ Attributes . PropertyAttributes . SampleUsage ( "**myInstance** or **seleniumInstance**" ) ]
23
+ [ Attributes . PropertyAttributes . Remarks ( "Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error" ) ]
24
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
25
+ public string v_DictionaryName { get ; set ; }
26
+
27
+ [ XmlAttribute ]
28
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the workbook file path" ) ]
29
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
30
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file that should be opened by Excel." ) ]
31
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\myfile.xlsx or [vFilePath]" ) ]
32
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
33
+ public string v_FilePath { get ; set ; }
34
+ [ XmlAttribute ]
35
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the Sheet Name" ) ]
36
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
37
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file that should be opened by Excel." ) ]
38
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\myfile.xlsx or [vFilePath]" ) ]
39
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
40
+ public string v_SheetName { get ; set ; }
41
+ [ XmlAttribute ]
42
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the Key column" ) ]
43
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
44
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file that should be opened by Excel." ) ]
45
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\myfile.xlsx or [vFilePath]" ) ]
46
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
47
+ public string v_KeyColumn { get ; set ; }
48
+ [ XmlAttribute ]
49
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the Value Column" ) ]
50
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
51
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file that should be opened by Excel." ) ]
52
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\myfile.xlsx or [vFilePath]" ) ]
53
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
54
+ public string v_ValueColumn { get ; set ; }
55
+ public LoadDictionaryCommand ( )
56
+ {
57
+ this . CommandName = "ExcelOpenWorkbookCommand" ;
58
+ this . SelectionName = "Load Dictionary" ;
59
+ this . CommandEnabled = true ;
60
+ this . CustomRendering = true ;
61
+ }
62
+ public override void RunCommand ( object sender )
63
+ {
64
+ var engine = ( Core . Automation . Engine . AutomationEngineInstance ) sender ;
65
+ var vInstance = "Test" ;
66
+ var vFilePath = v_FilePath . ConvertToUserVariable ( sender ) ;
67
+
68
+ var newExcelSession = new Microsoft . Office . Interop . Excel . Application
69
+ {
70
+ Visible = true
71
+ } ;
72
+
73
+ engine . AddAppInstance ( "Test" , newExcelSession ) ;
74
+
75
+
76
+ var excelObject = engine . GetAppInstance ( vInstance ) ;
77
+ Microsoft . Office . Interop . Excel . Application excelInstance = ( Microsoft . Office . Interop . Excel . Application ) excelObject ;
78
+ //excelInstance.Workbooks.Open(vFilePath);
79
+
80
+ DatasetCommands dataSetCommand = new DatasetCommands ( ) ;
81
+ DataTable requiredData = dataSetCommand . CreateDataTable ( @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + v_FilePath + @";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""" , "Select " + v_KeyColumn + "," + v_ValueColumn + " From [" + v_SheetName + "$]" ) ;
82
+ //Console.WriteLine(requiredData.Columns[0].ColumnName);
83
+
84
+ // requiredData.Columns[requiredData.Columns[0].ToString()].ColumnName = v_KeyColumn;
85
+ //requiredData.Columns[requiredData.Columns[1].ToString()].ColumnName = v_ValueColumn;
86
+ var dictlist = requiredData . AsEnumerable ( ) . Select ( x => new
87
+ {
88
+ keys = ( string ) x [ v_KeyColumn ] ,
89
+ values = ( string ) x [ v_ValueColumn ]
90
+ } ) . ToList ( ) ;
91
+ Dictionary < string , string > outputDictionary = new Dictionary < string , string > ( ) ;
92
+ foreach ( var dict in dictlist )
93
+ {
94
+ outputDictionary . Add ( dict . keys , dict . values ) ;
95
+ }
96
+
97
+
98
+ Script . ScriptVariable newDictionary = new Script . ScriptVariable
99
+ {
100
+ VariableName = v_DictionaryName ,
101
+ VariableValue = outputDictionary
102
+ } ;
103
+ //close excel
104
+ excelInstance . Quit ( ) ;
105
+
106
+ //remove instance
107
+ engine . RemoveAppInstance ( vInstance ) ;
108
+ engine . VariableList . Add ( newDictionary ) ;
109
+ }
110
+ public override List < Control > Render ( frmCommandEditor editor )
111
+ {
112
+ base . Render ( editor ) ;
113
+
114
+ //create standard group controls
115
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_FilePath" , this , editor ) ) ;
116
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_DictionaryName" , this , editor ) ) ;
117
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_SheetName" , this , editor ) ) ;
118
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_KeyColumn" , this , editor ) ) ;
119
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_ValueColumn" , this , editor ) ) ;
120
+
121
+
122
+ return RenderedControls ;
123
+
124
+ }
125
+ public override string GetDisplayValue ( )
126
+ {
127
+ return base . GetDisplayValue ( ) + " [Open from '" + v_FilePath + "', Instance Name: '" + "']" ;
128
+ }
129
+ }
130
+ }
0 commit comments