Skip to content

Commit

Permalink
Added ExcelWriteRowCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
openbots-ff committed Mar 26, 2020
1 parent 4c25479 commit 5b386e3
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
4 changes: 2 additions & 2 deletions taskt/Core/Automation/Commands/ExcelWriteRangeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
var dataSetVariable = LookupVariable(engine); //engine.VariableList.Where(f => f.VariableName == v_TextToSet).FirstOrDefault();
var dataSetVariable = LookupVariable(engine);
var targetAddress = v_ExcelCellAddress.ConvertToUserVariable(sender);
var excelObject = engine.GetAppInstance(vInstance);

Expand Down Expand Up @@ -154,7 +154,7 @@ public override List<Control> Render(frmCommandEditor editor)
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Writing Cells starting form '" + v_ExcelCellAddress + "' to " + "', Instance Name: '" + v_InstanceName + "']";
return base.GetDisplayValue() + " [Writing Cells starting from '" + v_ExcelCellAddress + "', Instance Name: '" + v_InstanceName + "']";
}
}
}
122 changes: 122 additions & 0 deletions taskt/Core/Automation/Commands/ExcelWriteRowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.UI.CustomControls;
using taskt.UI.Forms;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command writes a datarow to an excel sheet starting from the given cell address.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to set a value to a specific cell.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelWriteRowCommand : 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.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 the DataRow Variable Name to Set")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter the text value that will be set.")]
[Attributes.PropertyAttributes.SampleUsage("Hello World or [vText]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataRowToSet { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the Cell Location to start from (ex. A1 or B2)")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter the actual location of the cell.")]
[Attributes.PropertyAttributes.SampleUsage("A1, B10, [vAddress]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_ExcelCellAddress { get; set; }

public ExcelWriteRowCommand()
{
this.CommandName = "ExcelWriteRowCommand";
this.SelectionName = "Write Row";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
var dataRowVariable = LookupVariable(engine);
var targetAddress = v_ExcelCellAddress.ConvertToUserVariable(sender);
var excelObject = engine.GetAppInstance(vInstance);

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

DataRow row = (DataRow)dataRowVariable.VariableValue;
if (string.IsNullOrEmpty(targetAddress)) throw new ArgumentNullException("columnName");

var numberOfRow = Regex.Match(targetAddress, @"\d+").Value;
targetAddress = Regex.Replace(targetAddress, @"[\d-]", string.Empty);
targetAddress = targetAddress.ToUpperInvariant();

int sum = 0;

for (int i = 0; i < targetAddress.Length; i++)
{
sum *= 26;
sum += (targetAddress[i] - 'A' + 1);
}


//Write row
string cellValue;
for (int j = 0; j < row.ItemArray.Length; j++)
{
if (row.ItemArray[j] == null)
cellValue = string.Empty;

else
cellValue = row.ItemArray[j].ToString();

excelSheet.Cells[Int32.Parse(numberOfRow), j + sum] = cellValue;
}
}

private Script.ScriptVariable LookupVariable(Core.Automation.Engine.AutomationEngineInstance sendingInstance)
{
//search for the variable
var requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == v_DataRowToSet).FirstOrDefault();

//if variable was not found but it starts with variable naming pattern
if ((requiredVariable == null) && (v_DataRowToSet.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataRowToSet.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
{
//reformat and attempt
var reformattedVariable = v_DataRowToSet.Replace(sendingInstance.engineSettings.VariableStartMarker, "").Replace(sendingInstance.engineSettings.VariableEndMarker, "");
requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == reformattedVariable).FirstOrDefault();
}

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

//create standard group controls
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataRowToSet", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ExcelCellAddress", this, editor));

return RenderedControls;

}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Writing Cells starting from '" + v_ExcelCellAddress + "', Instance Name: '" + v_InstanceName + "']";
}
}
}
1 change: 1 addition & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(ExcelGetRangeCommandAsDT))]
[XmlInclude(typeof(ExcelGetRangeCommand))]
[XmlInclude(typeof(ExcelWriteRangeCommand))]
[XmlInclude(typeof(ExcelWriteRowCommand))]
[XmlInclude(typeof(ExcelSplitRangeByColumnCommand))]
[XmlInclude(typeof(UIAutomationCommand))]
[XmlInclude(typeof(ResizeWindowCommand))]
Expand Down
1 change: 1 addition & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<Compile Include="Core\Automation\Commands\CreateFolderCommand.cs" />
<Compile Include="Core\Automation\Commands\DeleteFolderCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelSplitRangeByColumnCommand.cs" />
<Compile Include="Core\Automation\Commands\ExcelWriteRowCommand.cs" />
<Compile Include="Core\Automation\Commands\ExtractFileCommand.cs" />
<Compile Include="Core\Automation\Commands\GetDataRowCommand.cs" />
<Compile Include="Core\Automation\Commands\GetDataRowCountCommand.cs" />
Expand Down

0 comments on commit 5b386e3

Please sign in to comment.