Skip to content

Commit

Permalink
Merge pull request saucepleez#201 from fodfran/TTB
Browse files Browse the repository at this point in the history
BeginLoop, ExcelSplitRangeByColumn, RunTask modification, bug fixes
  • Loading branch information
saucepleez authored Apr 16, 2020
2 parents 968b706 + 83b8e51 commit c256800
Show file tree
Hide file tree
Showing 66 changed files with 3,525 additions and 93 deletions.
3 changes: 2 additions & 1 deletion taskt/Core/Automation/Attributes/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public enum UIAdditionalHelperType
ShowDLLExplorer,
AddInputParameter,
ShowHTMLBuilder,
ShowIfBuilder
ShowIfBuilder,
ShowLoopBuilder
}
}
[System.AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
Expand Down
9 changes: 7 additions & 2 deletions taskt/Core/Automation/Commands/BeginListLoopCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Forms;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
using Microsoft.Office.Interop.Outlook;

namespace taskt.Core.Automation.Commands
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public override void RunCommand(object sender, Core.Script.ScriptAction parentCo
//if still null then throw exception
if (complexVariable == null)
{
throw new Exception("Complex Variable '" + v_LoopParameter + "' or '" + v_LoopParameter.ApplyVariableFormatting() + "' not found. Ensure the variable exists before attempting to modify it.");
throw new System.Exception("Complex Variable '" + v_LoopParameter + "' or '" + v_LoopParameter.ApplyVariableFormatting() + "' not found. Ensure the variable exists before attempting to modify it.");
}

dynamic listToLoop;
Expand All @@ -75,6 +76,10 @@ public override void RunCommand(object sender, Core.Script.ScriptAction parentCo
{
listToLoop = ((DataTable)complexVariable.VariableValue).Rows;
}
else if (complexVariable.VariableValue is List<MailItem>)
{
listToLoop = (List<MailItem>)complexVariable.VariableValue;
}
else if ((complexVariable.VariableValue.ToString().StartsWith("[")) && (complexVariable.VariableValue.ToString().EndsWith("]")) && (complexVariable.VariableValue.ToString().Contains(",")))
{
//automatically handle if user has given a json array
Expand All @@ -92,7 +97,7 @@ public override void RunCommand(object sender, Core.Script.ScriptAction parentCo
}
else
{
throw new Exception("Complex Variable List Type<T> Not Supported");
throw new System.Exception("Complex Variable List Type<T> Not Supported");
}


Expand Down
957 changes: 957 additions & 0 deletions taskt/Core/Automation/Commands/BeginLoopCommand.cs

Large diffs are not rendered by default.

237 changes: 237 additions & 0 deletions taskt/Core/Automation/Commands/BeginMultiLoopCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
using System;
using System.Linq;
using System.Xml.Serialization;
using System.Data;
using taskt.Core.Automation.User32;
using System.Collections.Generic;
using System.Windows.Forms;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
using System.Drawing;
using System.Text;

namespace taskt.Core.Automation.Commands
{




[Serializable]
[Attributes.ClassAttributes.Group("Loop Commands")]
[Attributes.ClassAttributes.Description("This command allows you to evaluate a logical statement to determine if the statement is true. The following actions will repeat continuously until that statement becomes false")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to check if a statement is 'true' or 'false' and subsequently loop actions based on either condition. Any 'BeginLoop' command must have a following 'EndLoop' command.")]
[Attributes.ClassAttributes.ImplementationDescription("This command evaluates supplied arguments and if evaluated to true runs sub elements")]
public class BeginMultiLoopCommand : ScriptCommand
{

[XmlElement]
[Attributes.PropertyAttributes.PropertyDescription("Multiple Loop Conditions - All Must Be True")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowLoopBuilder)]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.SampleUsage("n/a")]
[Attributes.PropertyAttributes.Remarks("")]
public DataTable v_LoopConditionsTable { get; set; }

[XmlIgnore]
[NonSerialized]
private DataGridView LoopConditionHelper;

[XmlIgnore]
private List<Script.ScriptVariable> ScriptVariables { get; set; }

public BeginMultiLoopCommand()
{
this.CommandName = "BeginMultiLoopCommand";
this.SelectionName = "Begin Multi Loop";
this.CommandEnabled = true;
this.CustomRendering = true;

v_LoopConditionsTable = new DataTable();
v_LoopConditionsTable.TableName = DateTime.Now.ToString("MultiLoopConditionTable" + DateTime.Now.ToString("MMddyy.hhmmss"));
v_LoopConditionsTable.Columns.Add("Statement");
v_LoopConditionsTable.Columns.Add("CommandData");

}


public override void RunCommand(object sender, Core.Script.ScriptAction parentCommand)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;

bool isTrueStatement = DetermineMultiStatementTruth(sender);

engine.ReportProgress("Starting Loop");

while (isTrueStatement)
{
foreach (var cmd in parentCommand.AdditionalScriptCommands)
{
if (engine.IsCancellationPending)
return;

engine.ExecuteCommand(cmd);

if (engine.CurrentLoopCancelled)
{
engine.ReportProgress("Exiting Loop");
engine.CurrentLoopCancelled = false;
return;
}

if (engine.CurrentLoopContinuing)
{
engine.ReportProgress("Continuing Next Loop");
engine.CurrentLoopContinuing = false;
break;
}
}
isTrueStatement = DetermineMultiStatementTruth(sender);
}

}


private bool DetermineMultiStatementTruth(object sender)
{
bool isTrueStatement = true;
foreach (DataRow rw in v_LoopConditionsTable.Rows)
{
var commandData = rw["CommandData"].ToString();
var loopCommand = Newtonsoft.Json.JsonConvert.DeserializeObject<Commands.BeginLoopCommand>(commandData);

var statementResult = loopCommand.DetermineStatementTruth(sender);

if (!statementResult)
{
isTrueStatement = false;
break;
}
}
return isTrueStatement;
}

public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//get script variables for feeding into loop builder form
ScriptVariables = editor.scriptVariables;

//create controls
var controls = CommandControls.CreateDataGridViewGroupFor("v_LoopConditionsTable", this, editor);
LoopConditionHelper = controls[2] as DataGridView;

//handle helper click
var helper = controls[1] as taskt.UI.CustomControls.CommandItemControl;
helper.Click += (sender, e) => CreateLoopCondition(sender, e);

//add for rendering
RenderedControls.AddRange(controls);

//define if condition helper
LoopConditionHelper.Width = 450;
LoopConditionHelper.Height = 200;
LoopConditionHelper.AutoGenerateColumns = false;
LoopConditionHelper.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
LoopConditionHelper.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "Condition", DataPropertyName = "Statement", ReadOnly = true, AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill });
LoopConditionHelper.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "CommandData", DataPropertyName = "CommandData", ReadOnly = true, Visible = false });
LoopConditionHelper.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "Edit", UseColumnTextForButtonValue = true, Text = "Edit", Width = 45 });
LoopConditionHelper.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "Delete", UseColumnTextForButtonValue = true, Text = "Delete", Width = 45 });
LoopConditionHelper.AllowUserToAddRows = false;
LoopConditionHelper.AllowUserToDeleteRows = true;
LoopConditionHelper.CellContentClick += LoopConditionHelper_CellContentClick;


return RenderedControls;
}

private void LoopConditionHelper_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;

if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
var buttonSelected = senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewButtonCell;
var selectedRow = v_LoopConditionsTable.Rows[e.RowIndex];

if (buttonSelected.Value.ToString() == "Edit")
{
//launch editor
var statement = selectedRow["Statement"];
var commandData = selectedRow["CommandData"].ToString();

var loopCommand = Newtonsoft.Json.JsonConvert.DeserializeObject<Commands.BeginLoopCommand>(commandData);

var automationCommands = taskt.UI.CustomControls.CommandControls.GenerateCommandsandControls().Where(f => f.Command is BeginLoopCommand).ToList();
frmCommandEditor editor = new frmCommandEditor(automationCommands, null);
editor.selectedCommand = loopCommand;
editor.editingCommand = loopCommand;
editor.originalCommand = loopCommand;
editor.creationMode = frmCommandEditor.CreationMode.Edit;
editor.scriptVariables = ScriptVariables;

if (editor.ShowDialog() == DialogResult.OK)
{
var editedCommand = editor.editingCommand as BeginLoopCommand;
var displayText = editedCommand.GetDisplayValue();
var serializedData = Newtonsoft.Json.JsonConvert.SerializeObject(editedCommand);

selectedRow["Statement"] = displayText;
selectedRow["CommandData"] = serializedData;
}


}
else if (buttonSelected.Value.ToString() == "Delete")
{
//delete
v_LoopConditionsTable.Rows.Remove(selectedRow);
}
else
{
throw new NotImplementedException("Requested Action is not implemented.");
}

}

}

private void CreateLoopCondition(object sender, EventArgs e)
{

var automationCommands = taskt.UI.CustomControls.CommandControls.GenerateCommandsandControls().Where(f => f.Command is BeginLoopCommand).ToList();

frmCommandEditor editor = new frmCommandEditor(automationCommands, null);
editor.selectedCommand = new BeginLoopCommand();
var res = editor.ShowDialog();

if (res == DialogResult.OK)
{
//get data
var configuredCommand = editor.selectedCommand as BeginLoopCommand;
var displayText = configuredCommand.GetDisplayValue();
var serializedData = Newtonsoft.Json.JsonConvert.SerializeObject(configuredCommand);

//add to list
v_LoopConditionsTable.Rows.Add(displayText, serializedData);

}

}

public override string GetDisplayValue()
{

if (v_LoopConditionsTable.Rows.Count == 0)
{
return "Loop <Not Configured>";
}
else
{
var statements = v_LoopConditionsTable.AsEnumerable().Select(f => f.Field<string>("Statement")).ToList();
return string.Join(" && ", statements);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExcelActivateSheetCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion taskt/Core/Automation/Commands/ExcelAddWorkbookCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExcelAddWorkbookCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion taskt/Core/Automation/Commands/ExcelAppendCellCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ExcelAppendCellCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
Expand Down
17 changes: 13 additions & 4 deletions taskt/Core/Automation/Commands/ExcelAppendRowCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class ExcelAppendRowCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter text to set")]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the Row to set")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter the text value that will be set (This could be a DataRow).")]
[Attributes.PropertyAttributes.SampleUsage("Hello,world or [vText]")]
Expand All @@ -47,10 +47,19 @@ public override void RunCommand(object sender)

Microsoft.Office.Interop.Excel.Application excelInstance = (Microsoft.Office.Interop.Excel.Application)excelObject;
Microsoft.Office.Interop.Excel.Worksheet excelSheet = excelInstance.ActiveSheet;
var lastUsedRow = excelSheet.Cells.Find("*", System.Reflection.Missing.Value,
int lastUsedRow;
try
{
lastUsedRow = excelSheet.Cells.Find("*", System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
}
catch(Exception ex)
{
lastUsedRow = 0;
}

var targetText = v_TextToSet.ConvertToUserVariable(sender);
splittext = targetText.Split(',');

Expand Down Expand Up @@ -82,7 +91,7 @@ public override List<Control> Render(frmCommandEditor editor)
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Append Row '" +v_TextToSet+ " to last row" + "of workboook with Instance Name: '" + v_InstanceName + "']";
return base.GetDisplayValue() + " [Append Row '" +v_TextToSet+ " to last row of workboook with Instance Name: '" + v_InstanceName + "']";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExcelCloseApplicationCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion taskt/Core/Automation/Commands/ExcelDeleteCellCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ExcelDeleteCellCommand : ScriptCommand
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Excel** command")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **excelInstance**")]
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Excel** command will cause an error")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
public string v_InstanceName { get; set; }
Expand Down
Loading

0 comments on commit c256800

Please sign in to comment.