Skip to content

Commit

Permalink
Added WriteDataRowValue and modified ExcelWriteRange command by addin…
Browse files Browse the repository at this point in the history
…g an option to add column name headers
  • Loading branch information
openbots-ff committed Mar 25, 2020
1 parent b50ea1e commit 4c25479
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 7 deletions.
70 changes: 63 additions & 7 deletions taskt/Core/Automation/Commands/ExcelWriteRangeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,36 @@ public class ExcelWriteRangeCommand : ScriptCommand
[Attributes.PropertyAttributes.InputSpecification("Enter the text value that will be set.")]
[Attributes.PropertyAttributes.SampleUsage("Hello World or [vText]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_TextToSet { get; set; }
public string v_DataTableToSet { 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; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Add Headers")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
[Attributes.PropertyAttributes.InputSpecification("When selected, the column headers from the specified spreadsheet range are also added.")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Yes** or **No**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_AddHeaders { get; set; }
public ExcelWriteRangeCommand()
{
this.CommandName = "ExcelWriteRangeCommand";
this.SelectionName = "Write Range";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_AddHeaders = "Yes";
}
public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
var dataSetVariable = engine.VariableList.Where(f => f.VariableName == v_TextToSet).FirstOrDefault();
var dataSetVariable = LookupVariable(engine); //engine.VariableList.Where(f => f.VariableName == v_TextToSet).FirstOrDefault();
var targetAddress = v_ExcelCellAddress.ConvertToUserVariable(sender);
var excelObject = engine.GetAppInstance(vInstance);

Expand All @@ -71,27 +81,73 @@ public override void RunCommand(object sender)
sum += (targetAddress[i] - 'A' + 1);
}

for (int i = 0; i < Dt.Rows.Count; i++)
if (v_AddHeaders == "Yes")
{
//Write column names
string columnName;
for (int j = 0; j < Dt.Columns.Count; j++)
{
if (Dt.Rows[i][j].ToString() == "null")
if (Dt.Columns[j].ColumnName == "null")
columnName = string.Empty;

else
columnName = Dt.Columns[j].ColumnName;

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

for (int i = 0; i < Dt.Rows.Count; i++)
{
for (int j = 0; j < Dt.Columns.Count; j++)
{
if (Dt.Rows[i][j].ToString() == "null")
{
Dt.Rows[i][j] = string.Empty;
}
excelSheet.Cells[i + Int32.Parse(numberOfRow) + 1, j + sum] = Dt.Rows[i][j].ToString();
}
}
}
else {
for (int i = 0; i < Dt.Rows.Count; i++)
{
for (int j = 0; j < Dt.Columns.Count; j++)
{
Dt.Rows[i][j] = string.Empty;
if (Dt.Rows[i][j].ToString() == "null")
{
Dt.Rows[i][j] = string.Empty;
}
excelSheet.Cells[i + Int32.Parse(numberOfRow), j + sum] = Dt.Rows[i][j].ToString();
}
excelSheet.Cells[i + Int32.Parse(numberOfRow), j + sum] = Dt.Rows[i][j].ToString();
}
}

}

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

//if variable was not found but it starts with variable naming pattern
if ((requiredVariable == null) && (v_DataTableToSet.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataTableToSet.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
{
//reformat and attempt
var reformattedVariable = v_DataTableToSet.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_TextToSet", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataTableToSet", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ExcelCellAddress", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_AddHeaders", this, editor));

return RenderedControls;

Expand Down
1 change: 1 addition & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(AddDataRowCommand))]
[XmlInclude(typeof(GetDataRowCommand))]
[XmlInclude(typeof(GetDataRowValueCommand))]
[XmlInclude(typeof(WriteDataRowValueCommand))]
[XmlInclude(typeof(GetDataRowCountCommand))]
[XmlInclude(typeof(RemoveDataRowCommand))]
[XmlInclude(typeof(ThickAppClickItemCommand))]
Expand Down
133 changes: 133 additions & 0 deletions taskt/Core/Automation/Commands/WriteDataRowValueCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Linq;
using System.Xml.Serialization;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using taskt.UI.Forms;
using taskt.UI.CustomControls;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("DataTable Commands")]
[Attributes.ClassAttributes.Description("This command allows you to get a DataRow Value from a DataTable")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to add a datarow to a DataTable.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class WriteDataRowValueCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the DataRow Name")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a existing DataTable to add rows to.")]
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataRowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Select value by Index or Column Name")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Index")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Column Name")]
[Attributes.PropertyAttributes.InputSpecification("Select whether the DataRow value should be found by index or column name")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Index** or **Column Name**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_Option { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter the index of the DataRow")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter a valid DataRow index value")]
[Attributes.PropertyAttributes.SampleUsage("0 or [vIndex]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataValueIndex { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter the Value")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter the value to write to the DataRow cell")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_DataRowValue { get; set; }

public WriteDataRowValueCommand()
{
this.CommandName = "WriteDataRowValueCommand";
this.SelectionName = "Write DataRow Value";
this.CommandEnabled = true;
this.CustomRendering = true;
v_Option = "Index";

}

public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
var dataRowValue = v_DataRowValue.ConvertToUserVariable(sender);

var dataRowVariable = LookupVariable(engine);
var variableList = engine.VariableList;
DataRow dataRow;

//check in case of looping through datatable using BeginListLoopCommand
if (dataRowVariable.VariableValue is DataTable && engine.VariableList.Exists(x => x.VariableName == "Loop.CurrentIndex"))
{
var loopIndexVariable = engine.VariableList.Where(x => x.VariableName == "Loop.CurrentIndex").FirstOrDefault();
int loopIndex = int.Parse(loopIndexVariable.VariableValue.ToString());
dataRow = ((DataTable)dataRowVariable.VariableValue).Rows[loopIndex - 1];
}

else dataRow = (DataRow)dataRowVariable.VariableValue;

var valueIndex = v_DataValueIndex.ConvertToUserVariable(sender);
//string value = "";
if (v_Option == "Index")
{
int index = int.Parse(valueIndex);
dataRow[index] = dataRowValue;

}
else if (v_Option == "Column Name")
{
string index = valueIndex;
dataRow.SetField<string>(index, dataRowValue); //Field<string>(index) = dataRowValue;
}

//value.StoreInUserVariable(sender, v_UserVariableName);

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

//if variable was not found but it starts with variable naming pattern
if ((requiredVariable == null) && (v_DataRowName.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataRowName.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
{
//reformat and attempt
var reformattedVariable = v_DataRowName.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);

RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataRowName", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_Option", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataValueIndex", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataRowValue", this, editor));


return RenderedControls;
}



public override string GetDisplayValue()
{
return base.GetDisplayValue() + $" [Write Value '{v_DataRowValue}' to '{v_DataValueIndex}' in '{v_DataRowName}']";
}
}
}
1 change: 1 addition & 0 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ public static class Images
uiImages.Add("AddDataRowCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("GetDataRowCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("GetDataRowValueCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("WriteDataRowValueCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("GetDataRowCountCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("CreateDataTableCommand", taskt.Properties.Resources.command_spreadsheet);
uiImages.Add("FilterDataTableCommand", taskt.Properties.Resources.command_spreadsheet);
Expand Down
1 change: 1 addition & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
<Compile Include="Core\Automation\Commands\UploadDataCommand.cs" />
<Compile Include="Core\Automation\Commands\VariableCommand.cs" />
<Compile Include="Core\Automation\Commands\WaitForFileToExistCommand.cs" />
<Compile Include="Core\Automation\Commands\WriteDataRowValueCommand.cs" />
<Compile Include="Core\Automation\Commands\WriteTextFileCommand.cs" />
<Compile Include="Core\DocumentationGeneration.cs" />
<Compile Include="Core\EncryptionServices.cs" />
Expand Down

0 comments on commit 4c25479

Please sign in to comment.