Skip to content

Commit

Permalink
Fixed Excel Set Cell Value Bug, Added Additional Excel Commands and t…
Browse files Browse the repository at this point in the history
…esting files
  • Loading branch information
saucepleez committed Aug 8, 2018
1 parent 37eef17 commit fb592d5
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 2 deletions.
201 changes: 199 additions & 2 deletions taskt/Core/AutomationCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ namespace taskt.Core.AutomationCommands
[XmlInclude(typeof(ExcelCloseApplicationCommand))]
[XmlInclude(typeof(ExcelGetCellCommand))]
[XmlInclude(typeof(ExcelRunMacroCommand))]
[XmlInclude(typeof(ExcelActivateSheetCommand))]
[XmlInclude(typeof(ExcelDeleteRowCommand))]
[XmlInclude(typeof(ExcelDeleteCellCommand))]
[XmlInclude(typeof(ExcelGetLastRowCommand))]
[XmlInclude(typeof(SeleniumBrowserCreateCommand))]
[XmlInclude(typeof(SeleniumBrowserNavigateURLCommand))]
[XmlInclude(typeof(SeleniumBrowserNavigateForwardCommand))]
Expand Down Expand Up @@ -2214,6 +2218,7 @@ public override string GetDisplayValue()
public class BeginNumberOfTimesLoopCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyDescription("Enter how many times to perform the loop")]
public string v_LoopParameter { get; set; }

Expand All @@ -2232,7 +2237,9 @@ public override void RunCommand(object sender, Core.Script.ScriptAction parentCo
int loopTimes;
Script.ScriptVariable complexVarible = null;

loopTimes = int.Parse(loopCommand.v_LoopParameter);
var loopParameter = loopCommand.v_LoopParameter.ConvertToUserVariable(sender);

loopTimes = int.Parse(loopParameter);

for (int i = 0; i < loopTimes; i++)
{
Expand Down Expand Up @@ -2576,7 +2583,7 @@ 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;
excelSheet.Range[targetAddress].Value = targetAddress;
excelSheet.Range[targetAddress].Value = targetText;
}
}
public override string GetDisplayValue()
Expand Down Expand Up @@ -2662,6 +2669,48 @@ public override string GetDisplayValue()
}
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command allows you to find the last row in a used range in an Excel Workbook.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelGetLastRowCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
public string v_InstanceName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter Letter of the Column to check (ex. A, B, C)")]
public string v_ColumnLetter { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the row number")]
public string v_applyToVariableName { get; set; }
public ExcelGetLastRowCommand()
{
this.CommandName = "ExcelGetLastRowCommand";
this.SelectionName = "Get Last Row";
this.CommandEnabled = true;
}
public override void RunCommand(object sender)
{
var sendingInstance = (UI.Forms.frmScriptEngine)sender;
if (sendingInstance.appInstances.TryGetValue(v_InstanceName, out object excelObject))
{

Microsoft.Office.Interop.Excel.Application excelInstance = (Microsoft.Office.Interop.Excel.Application)excelObject;
var excelSheet = excelInstance.ActiveSheet;
var lastRow = (int)excelSheet.Cells(excelSheet.Rows.Count, "A").End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;


lastRow.ToString().StoreInUserVariable(sender, v_applyToVariableName);


}
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Instance Name: '" + v_InstanceName + "']";
}
}
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command allows you to close Excel.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelCloseApplicationCommand : ScriptCommand
Expand Down Expand Up @@ -2693,6 +2742,154 @@ public override string GetDisplayValue()
return base.GetDisplayValue() + " [Save On Close: " + v_ExcelSaveOnExit + ", Instance Name: '" + v_InstanceName + "']";
}
}

[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command allows you to switch worksheet tabs")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelActivateSheetCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
public string v_InstanceName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Indicate the name of the sheet within the Workbook to activate")]
public string v_SheetName { get; set; }
public ExcelActivateSheetCommand()
{
this.CommandName = "ExcelActivateSheetCommand";
this.SelectionName = "Activate Sheet";
this.CommandEnabled = true;
}
public override void RunCommand(object sender)
{
var sendingInstance = (UI.Forms.frmScriptEngine)sender;
if (sendingInstance.appInstances.TryGetValue(v_InstanceName, out object excelObject))
{
Microsoft.Office.Interop.Excel.Application excelInstance = (Microsoft.Office.Interop.Excel.Application)excelObject;
string sheetToDelete = v_SheetName.ConvertToUserVariable(sender);
Microsoft.Office.Interop.Excel.Worksheet workSheet = excelInstance.Sheets[sheetToDelete];
workSheet.Select();



}
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Sheet Name: " + v_SheetName + ", Instance Name: '" + v_InstanceName + "']";
}
}
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command allows you to delete a specified row in Excel")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelDeleteRowCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
public string v_InstanceName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyDescription("Indicate the row number to delete")]
public string v_RowNumber { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
public string v_ShiftUp { get; set; }
public ExcelDeleteRowCommand()
{
this.CommandName = "ExcelDeleteRowCommand";
this.SelectionName = "Delete Row";
this.CommandEnabled = true;
}
public override void RunCommand(object sender)
{
var sendingInstance = (UI.Forms.frmScriptEngine)sender;
if (sendingInstance.appInstances.TryGetValue(v_InstanceName, out object excelObject))
{


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

string rowToDelete = v_RowNumber.ConvertToUserVariable(sender);

var cells = workSheet.Range["A" + rowToDelete, Type.Missing];
var entireRow = cells.EntireRow;
if (v_ShiftUp == "Yes")
{
entireRow.Delete();
}
else
{
entireRow.Clear();
}


}
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Row Number: " + v_RowNumber + ", Instance Name: '" + v_InstanceName + "']";
}
}
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command allows you to delete a specified row in Excel")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
public class ExcelDeleteCellCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
public string v_InstanceName { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyDescription("Indicate the range to delete ex. A1 or A1:C1")]
public string v_Range { get; set; }
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Should the cells below shift upward after deletion?")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Yes")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("No")]
public string v_ShiftUp { get; set; }
public ExcelDeleteCellCommand()
{
this.CommandName = "ExcelDeleteCellCommand";
this.SelectionName = "Delete Cell";
this.CommandEnabled = true;
}
public override void RunCommand(object sender)
{
var sendingInstance = (UI.Forms.frmScriptEngine)sender;
if (sendingInstance.appInstances.TryGetValue(v_InstanceName, out object excelObject))
{


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

string range = v_Range.ConvertToUserVariable(sender);
var cells = workSheet.Range[range, Type.Missing];


if (v_ShiftUp == "Yes")
{
cells.Delete();
}
else
{
cells.Clear();
}



}
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Range: " + v_Range + ", Instance Name: '" + v_InstanceName + "']";
}
}
[Serializable]
[Attributes.ClassAttributes.Group("Excel Commands")]
[Attributes.ClassAttributes.Description("This command gets a range of cells and applies them against a dataset")]
Expand Down
15 changes: 15 additions & 0 deletions taskt/Sample Scripts/Excel Activate Sheet Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="ExcelCreateApplicationCommand" CommandName="ExcelOpenApplicationCommand" IsCommented="false" SelectionName="Create Excel Application" DefaultPause="250" LineNumber="1" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelOpenWorkbookCommand" CommandName="ExcelOpenWorkbookCommand" IsCommented="false" SelectionName="Open Workbook" DefaultPause="250" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_FilePath="D:\Dropbox\Environment Folders\Desktop\Sample.xlsx" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelActivateSheetCommand" CommandName="ExcelActivateSheetCommand" IsCommented="false" SelectionName="Activate Sheet" DefaultPause="250" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_SheetName="Sheet2" />
</ScriptAction>
</Commands>
<Variables />
</Script>
24 changes: 24 additions & 0 deletions taskt/Sample Scripts/Excel Delete Cell Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="ExcelCreateApplicationCommand" CommandName="ExcelOpenApplicationCommand" IsCommented="false" SelectionName="Create Excel Application" DefaultPause="250" LineNumber="1" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelOpenWorkbookCommand" CommandName="ExcelOpenWorkbookCommand" IsCommented="false" SelectionName="Open Workbook" DefaultPause="250" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_FilePath="D:\Dropbox\Environment Folders\Desktop\Sample.xlsx" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelActivateSheetCommand" CommandName="ExcelActivateSheetCommand" IsCommented="false" SelectionName="Activate Sheet" DefaultPause="250" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_SheetName="Sheet1" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelDeleteCellCommand" CommandName="ExcelDeleteCellCommand" IsCommented="false" SelectionName="Delete Cell" DefaultPause="250" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_Range="[vRangeValue]" v_ShiftUp="false" />
</ScriptAction>
</Commands>
<Variables>
<ScriptVariable>
<currentPosition>0</currentPosition>
<variableName>vRangeValue</variableName>
<variableValue xsi:type="xsd:string">A1</variableValue>
</ScriptVariable>
</Variables>
</Script>
24 changes: 24 additions & 0 deletions taskt/Sample Scripts/Excel Delete Row Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="ExcelCreateApplicationCommand" CommandName="ExcelOpenApplicationCommand" IsCommented="false" SelectionName="Create Excel Application" DefaultPause="250" LineNumber="1" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelOpenWorkbookCommand" CommandName="ExcelOpenWorkbookCommand" IsCommented="false" SelectionName="Open Workbook" DefaultPause="250" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_FilePath="D:\Dropbox\Environment Folders\Desktop\Sample.xlsx" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelActivateSheetCommand" CommandName="ExcelActivateSheetCommand" IsCommented="false" SelectionName="Activate Sheet" DefaultPause="250" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_SheetName="Sheet1" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelDeleteRowCommand" CommandName="ExcelDeleteRowCommand" IsCommented="false" SelectionName="Delete Row" DefaultPause="250" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_RowNumber="[vRowNumber]" v_ShiftUp="No" />
</ScriptAction>
</Commands>
<Variables>
<ScriptVariable>
<currentPosition>0</currentPosition>
<variableName>vRowNumber</variableName>
<variableValue xsi:type="xsd:string">2</variableValue>
</ScriptVariable>
</Variables>
</Script>
27 changes: 27 additions & 0 deletions taskt/Sample Scripts/Excel Get Last Row Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="ExcelCreateApplicationCommand" CommandName="ExcelOpenApplicationCommand" IsCommented="false" SelectionName="Create Excel Application" DefaultPause="250" LineNumber="1" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelOpenWorkbookCommand" CommandName="ExcelOpenWorkbookCommand" IsCommented="false" SelectionName="Open Workbook" DefaultPause="250" LineNumber="2" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_FilePath="D:\Dropbox\Environment Folders\Desktop\Sample.xlsx" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelActivateSheetCommand" CommandName="ExcelActivateSheetCommand" IsCommented="false" SelectionName="Activate Sheet" DefaultPause="250" LineNumber="3" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_SheetName="Sheet1" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="ExcelGetLastRowCommand" CommandName="ExcelGetLastRowCommand" IsCommented="false" SelectionName="Get Last Row" DefaultPause="250" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_InstanceName="default" v_ColumnLetter="A" v_applyToVariableName="vTotalRows" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="250" LineNumber="5" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="[vTotalRows] total rows were found" v_AutoCloseAfter="0" />
</ScriptAction>
</Commands>
<Variables>
<ScriptVariable>
<currentPosition>0</currentPosition>
<variableName>vTotalRows</variableName>
<variableValue xsi:type="xsd:string"></variableValue>
</ScriptVariable>
</Variables>
</Script>
Loading

0 comments on commit fb592d5

Please sign in to comment.