Skip to content

Commit 41b3190

Browse files
committed
Added GetListCount and GetListItem and fixed a bunch of other things
1 parent e796c47 commit 41b3190

10 files changed

+326
-11
lines changed

taskt/Core/Automation/Commands/ExcelAppendRowCommand.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ public override void RunCommand(object sender)
4747

4848
Microsoft.Office.Interop.Excel.Application excelInstance = (Microsoft.Office.Interop.Excel.Application)excelObject;
4949
Microsoft.Office.Interop.Excel.Worksheet excelSheet = excelInstance.ActiveSheet;
50-
var lastUsedRow = excelSheet.Cells.Find("*", System.Reflection.Missing.Value,
50+
int lastUsedRow;
51+
try
52+
{
53+
lastUsedRow = excelSheet.Cells.Find("*", System.Reflection.Missing.Value,
5154
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
5255
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
5356
false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
57+
}
58+
catch(Exception ex)
59+
{
60+
lastUsedRow = 0;
61+
}
62+
5463
var targetText = v_TextToSet.ConvertToUserVariable(sender);
5564
splittext = targetText.Split(',');
5665

taskt/Core/Automation/Commands/ExcelSplitRangeByColumnCommand.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public class ExcelSplitRangeByColumnCommand : ScriptCommand
5959
[Attributes.PropertyAttributes.Remarks("")]
6060
public string v_OutputDirectory { get; set; }
6161

62+
[XmlAttribute]
63+
[Attributes.PropertyAttributes.PropertyDescription("Indicate the File Type to save as")]
64+
[Attributes.PropertyAttributes.PropertyUISelectionOption("xlsx")]
65+
[Attributes.PropertyAttributes.PropertyUISelectionOption("csv")]
66+
[Attributes.PropertyAttributes.InputSpecification("Specify the file format type for the split ranges")]
67+
[Attributes.PropertyAttributes.SampleUsage("Select either **xlsx* or **csv**")]
68+
[Attributes.PropertyAttributes.Remarks("")]
69+
public string v_FileType { get; set; }
70+
6271
[XmlAttribute]
6372
[Attributes.PropertyAttributes.PropertyDescription("Assign DataTable List to Variable")]
6473
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
@@ -72,6 +81,7 @@ public ExcelSplitRangeByColumnCommand()
7281
this.SelectionName = "Split Range By Column";
7382
this.CommandEnabled = true;
7483
this.CustomRendering = true;
84+
v_FileType = "xlsx";
7585
}
7686

7787
public override void RunCommand(object sender)
@@ -168,7 +178,17 @@ public override void RunCommand(object sender)
168178
newSheet.Cells[j + 2, k + 1] = newDT.Rows[j].ItemArray[k].ToString();
169179
}
170180
}
171-
newWorkBook.SaveAs(Path.Combine(vOutputDirectory, newName + ".xlsx"));
181+
if (v_FileType == "csv")
182+
{
183+
newWorkBook.SaveAs(Path.Combine(vOutputDirectory, newName), Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, Type.Missing, Type.Missing,
184+
Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
185+
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
186+
}
187+
else
188+
{
189+
newWorkBook.SaveAs(Path.Combine(vOutputDirectory, newName + ".xlsx"));
190+
}
191+
172192
}
173193
}
174194
}
@@ -183,6 +203,7 @@ public override List<Control> Render(frmCommandEditor editor)
183203
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ExcelCellAddress2", this, editor));
184204
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ColumnName", this, editor));
185205
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_OutputDirectory", this, editor));
206+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileType", this, editor));
186207
//create control for variable name
187208
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_userVariableName", this));
188209
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_userVariableName", this).AddVariableNames(editor);

taskt/Core/Automation/Commands/ExcelWriteRowCommand.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,27 @@ public override void RunCommand(object sender)
5050
{
5151
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
5252
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
53-
var dataRowVariable = LookupVariable(engine);
53+
var dataRowVariable = LookupVariable(engine);
54+
var variableList = engine.VariableList;
55+
DataRow row;
56+
5457
var targetAddress = v_ExcelCellAddress.ConvertToUserVariable(sender);
5558
var excelObject = engine.GetAppInstance(vInstance);
5659

5760
Microsoft.Office.Interop.Excel.Application excelInstance = (Microsoft.Office.Interop.Excel.Application)excelObject;
5861
var excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelInstance.ActiveSheet;
5962

60-
DataRow row = (DataRow)dataRowVariable.VariableValue;
63+
//check in case of looping through datatable using BeginListLoopCommand
64+
if (dataRowVariable.VariableValue is DataTable && engine.VariableList.Exists(x => x.VariableName == "Loop.CurrentIndex"))
65+
{
66+
var loopIndexVariable = engine.VariableList.Where(x => x.VariableName == "Loop.CurrentIndex").FirstOrDefault();
67+
int loopIndex = int.Parse(loopIndexVariable.VariableValue.ToString());
68+
row = ((DataTable)dataRowVariable.VariableValue).Rows[loopIndex - 1];
69+
}
70+
71+
else row = (DataRow)dataRowVariable.VariableValue;
72+
73+
6174
if (string.IsNullOrEmpty(targetAddress)) throw new ArgumentNullException("columnName");
6275

6376
var numberOfRow = Regex.Match(targetAddress, @"\d+").Value;

taskt/Core/Automation/Commands/GetDataRowCountCommand.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace taskt.Core.Automation.Commands
1111
{
1212
[Serializable]
1313
[Attributes.ClassAttributes.Group("DataTable Commands")]
14-
[Attributes.ClassAttributes.Description("This command allows you to add a datarow to a DataTable")]
15-
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to add a datarow to a DataTable.")]
14+
[Attributes.ClassAttributes.Description("This command allows you to get the datarow count of a DataTable")]
15+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to get the datarow count of a DataTable.")]
1616
[Attributes.ClassAttributes.ImplementationDescription("")]
1717
public class GetDataRowCountCommand : ScriptCommand
1818
{
1919
[XmlAttribute]
2020
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the DataTable Name")]
2121
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
22-
[Attributes.PropertyAttributes.InputSpecification("Enter a existing DataTable to add rows to.")]
22+
[Attributes.PropertyAttributes.InputSpecification("Enter a existing DataTable.")]
2323
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
2424
[Attributes.PropertyAttributes.Remarks("")]
2525
public string v_DataTableName { get; set; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Linq;
3+
using System.Xml.Serialization;
4+
using System.Data;
5+
using System.Windows.Forms;
6+
using System.Collections.Generic;
7+
using taskt.UI.Forms;
8+
using taskt.UI.CustomControls;
9+
using Microsoft.Office.Interop.Outlook;
10+
11+
namespace taskt.Core.Automation.Commands
12+
{
13+
[Serializable]
14+
[Attributes.ClassAttributes.Group("Data Commands")]
15+
[Attributes.ClassAttributes.Description("This command allows you to get the item count of a List")]
16+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to get the item count of a List.")]
17+
[Attributes.ClassAttributes.ImplementationDescription("")]
18+
public class GetListCountCommand : ScriptCommand
19+
{
20+
[XmlAttribute]
21+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the List Name")]
22+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
23+
[Attributes.PropertyAttributes.InputSpecification("Enter a existing List.")]
24+
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
25+
[Attributes.PropertyAttributes.Remarks("")]
26+
public string v_ListName { get; set; }
27+
28+
[XmlAttribute]
29+
[Attributes.PropertyAttributes.PropertyDescription("Assign to Variable")]
30+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
31+
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
32+
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
33+
public string v_UserVariableName { get; set; }
34+
35+
public GetListCountCommand()
36+
{
37+
this.CommandName = "GetListCountCommand";
38+
this.SelectionName = "Get List Count";
39+
this.CommandEnabled = true;
40+
this.CustomRendering = true;
41+
42+
}
43+
44+
public override void RunCommand(object sender)
45+
{
46+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
47+
//get variable by regular name
48+
Script.ScriptVariable listVariable = engine.VariableList.Where(x => x.VariableName == v_ListName).FirstOrDefault();
49+
50+
51+
52+
53+
//user may potentially include brackets []
54+
if (listVariable == null)
55+
{
56+
listVariable = engine.VariableList.Where(x => x.VariableName.ApplyVariableFormatting() == v_ListName).FirstOrDefault();
57+
}
58+
59+
//if still null then throw exception
60+
if (listVariable == null)
61+
{
62+
throw new System.Exception("Complex Variable '" + v_ListName + "' or '" + v_ListName.ApplyVariableFormatting() + "' not found. Ensure the variable exists before attempting to modify it.");
63+
}
64+
65+
dynamic listToCount;
66+
if (listVariable.VariableValue is List<string>)
67+
{
68+
listToCount = (List<string>)listVariable.VariableValue;
69+
}
70+
else if (listVariable.VariableValue is List<MailItem>)
71+
{
72+
listToCount = (List<MailItem>)listVariable.VariableValue;
73+
}
74+
else if (listVariable.VariableValue is List<OpenQA.Selenium.IWebElement>)
75+
{
76+
listToCount = (List<OpenQA.Selenium.IWebElement>)listVariable.VariableValue;
77+
}
78+
else if ((listVariable.VariableValue.ToString().StartsWith("[")) && (listVariable.VariableValue.ToString().EndsWith("]")) && (listVariable.VariableValue.ToString().Contains(",")))
79+
{
80+
//automatically handle if user has given a json array
81+
Newtonsoft.Json.Linq.JArray jsonArray = Newtonsoft.Json.JsonConvert.DeserializeObject(listVariable.VariableValue.ToString()) as Newtonsoft.Json.Linq.JArray;
82+
83+
var itemList = new List<string>();
84+
foreach (var item in jsonArray)
85+
{
86+
var value = (Newtonsoft.Json.Linq.JValue)item;
87+
itemList.Add(value.ToString());
88+
}
89+
90+
listVariable.VariableValue = itemList;
91+
listToCount = itemList;
92+
}
93+
else
94+
{
95+
throw new System.Exception("Complex Variable List Type<T> Not Supported");
96+
}
97+
98+
string count = listToCount.Count.ToString();
99+
100+
count.StoreInUserVariable(sender, v_UserVariableName);
101+
102+
}
103+
104+
public override List<Control> Render(frmCommandEditor editor)
105+
{
106+
base.Render(editor);
107+
108+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ListName", this, editor));
109+
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
110+
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
111+
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
112+
RenderedControls.Add(VariableNameControl);
113+
114+
return RenderedControls;
115+
}
116+
117+
118+
119+
public override string GetDisplayValue()
120+
{
121+
return base.GetDisplayValue() + $" [From '{v_ListName}', Store In: '{v_UserVariableName}']";
122+
}
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using System;
2+
using System.Linq;
3+
using System.Xml.Serialization;
4+
using System.Data;
5+
using System.Windows.Forms;
6+
using System.Collections.Generic;
7+
using taskt.UI.Forms;
8+
using taskt.UI.CustomControls;
9+
using Microsoft.Office.Interop.Outlook;
10+
11+
namespace taskt.Core.Automation.Commands
12+
{
13+
[Serializable]
14+
[Attributes.ClassAttributes.Group("Data Commands")]
15+
[Attributes.ClassAttributes.Description("This command allows you to get the item count of a List")]
16+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to get the item count of a List.")]
17+
[Attributes.ClassAttributes.ImplementationDescription("")]
18+
public class GetListItemCommand : ScriptCommand
19+
{
20+
[XmlAttribute]
21+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the List Name")]
22+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
23+
[Attributes.PropertyAttributes.InputSpecification("Enter a existing List.")]
24+
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
25+
[Attributes.PropertyAttributes.Remarks("")]
26+
public string v_ListName { get; set; }
27+
28+
[XmlAttribute]
29+
[Attributes.PropertyAttributes.PropertyDescription("Please enter the index of the List item")]
30+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
31+
[Attributes.PropertyAttributes.InputSpecification("Enter a valid List index value")]
32+
[Attributes.PropertyAttributes.SampleUsage("0 or [vIndex]")]
33+
[Attributes.PropertyAttributes.Remarks("")]
34+
public string v_ItemIndex { get; set; }
35+
36+
[XmlAttribute]
37+
[Attributes.PropertyAttributes.PropertyDescription("Assign to Variable")]
38+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
39+
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
40+
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
41+
public string v_UserVariableName { get; set; }
42+
43+
public GetListItemCommand()
44+
{
45+
this.CommandName = "GetListItemCommand";
46+
this.SelectionName = "Get List Item";
47+
this.CommandEnabled = true;
48+
this.CustomRendering = true;
49+
50+
}
51+
52+
public override void RunCommand(object sender)
53+
{
54+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
55+
var itemIndex = v_ItemIndex.ConvertToUserVariable(sender);
56+
int index = int.Parse(itemIndex);
57+
//get variable by regular name
58+
Script.ScriptVariable listVariable = engine.VariableList.Where(x => x.VariableName == v_ListName).FirstOrDefault();
59+
60+
//user may potentially include brackets []
61+
if (listVariable == null)
62+
{
63+
listVariable = engine.VariableList.Where(x => x.VariableName.ApplyVariableFormatting() == v_ListName).FirstOrDefault();
64+
}
65+
66+
//if still null then throw exception
67+
if (listVariable == null)
68+
{
69+
throw new System.Exception("Complex Variable '" + v_ListName + "' or '" + v_ListName.ApplyVariableFormatting() + "' not found. Ensure the variable exists before attempting to modify it.");
70+
}
71+
72+
dynamic listToIndex;
73+
if (listVariable.VariableValue is List<string>)
74+
{
75+
listToIndex = (List<string>)listVariable.VariableValue;
76+
}
77+
else if (listVariable.VariableValue is List<MailItem>)
78+
{
79+
listToIndex = (List<MailItem>)listVariable.VariableValue;
80+
}
81+
else if (listVariable.VariableValue is List<OpenQA.Selenium.IWebElement>)
82+
{
83+
listToIndex = (List<OpenQA.Selenium.IWebElement>)listVariable.VariableValue;
84+
}
85+
else if ((listVariable.VariableValue.ToString().StartsWith("[")) && (listVariable.VariableValue.ToString().EndsWith("]")) && (listVariable.VariableValue.ToString().Contains(",")))
86+
{
87+
//automatically handle if user has given a json array
88+
Newtonsoft.Json.Linq.JArray jsonArray = Newtonsoft.Json.JsonConvert.DeserializeObject(listVariable.VariableValue.ToString()) as Newtonsoft.Json.Linq.JArray;
89+
90+
var itemList = new List<string>();
91+
foreach (var jsonItem in jsonArray)
92+
{
93+
var value = (Newtonsoft.Json.Linq.JValue)jsonItem;
94+
itemList.Add(value.ToString());
95+
}
96+
97+
listVariable.VariableValue = itemList;
98+
listToIndex = itemList;
99+
}
100+
else
101+
{
102+
throw new System.Exception("Complex Variable List Type<T> Not Supported");
103+
}
104+
105+
var item = listToIndex[index];
106+
107+
Script.ScriptVariable newListItem = new Script.ScriptVariable
108+
{
109+
VariableName = v_UserVariableName,
110+
VariableValue = item
111+
};
112+
113+
//Overwrites variable if it already exists
114+
if (engine.VariableList.Exists(x => x.VariableName == newListItem.VariableName))
115+
{
116+
Script.ScriptVariable temp = engine.VariableList.Where(x => x.VariableName == newListItem.VariableName).FirstOrDefault();
117+
engine.VariableList.Remove(temp);
118+
}
119+
engine.VariableList.Add(newListItem);
120+
121+
}
122+
123+
public override List<Control> Render(frmCommandEditor editor)
124+
{
125+
base.Render(editor);
126+
127+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ListName", this, editor));
128+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ItemIndex", this, editor));
129+
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
130+
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
131+
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
132+
RenderedControls.Add(VariableNameControl);
133+
134+
return RenderedControls;
135+
}
136+
137+
138+
139+
public override string GetDisplayValue()
140+
{
141+
return base.GetDisplayValue() + $" [From '{v_ListName}', Store In: '{v_UserVariableName}']";
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)