Streamline your operations, improve productivity
In RPA processes, we often need to automate interactions with common software programs like Microsoft Office programs (Word, Excel, etc.) and various web browsers. Of course, these software programs all have interface, and you can interact with them directly through Laiye RPA with the knowledge you’ve learned in Chapter 3, “Command with Target.” However, because of how common these software automations are needed, Laiye RPA provides special commands that encapsulate automated interactions with Excel, Word, Outlook, browsers, and databases. Working with these special commands are vastly more efficient and convenient than simulating interface interactions. For example, while we can simulate interactions with the Excel interface to open and read an Excel file, it’s quite complicated. However, using special Excel automation commands, we can achieve the same goal using only one command.
Before we can get started using Laiye RPA to automate these software programs, you must install the programs on your local computer. For Excel and Word automation, you need to install Office 2007 or above, or WPS 2016 or above. For browser automation, you need to install Internet Explorer (IE), Google Chrome, or Firefox.
In this chapter, we assume that the reader has a preliminary understanding of browser, Word, Excel, database, and working knowledge with all these programs is even better. If you need to learn more about these tools before we get started, resources on these topics are easily accessible on the internet through a simple Google search.
Excel is an important member of Office software suite. It provides powerful tools for making calculations, analyses, and visualizing data, and it is one of the most used software for processing spreadsheets. This makes it a common target for RPA platforms to automate.
Before we start automating Excel, let us define a few concepts. Every Excel file corresponds to a workbook, which the user can use to modify and store data in the file. Whenever you open an Excel file, the title on top is the name of the workbook. A workbook has multiple worksheets, and a workbook contains three worksheets by default, named Sheet1, Sheet2, and Sheet3. Of course, you can always add or delete worksheets.
Figure 53: Workbook and worksheet of Excel
An Excel worksheet is a two-dimensional table made up of cells. Each cell is determined by a row and column number. Row numbers are represented by a sequence of numbers such as 1, 2, 3, 4, etc. Column numbers are represented by a sequence of uppercase letters such as A, B, C, D, etc. Therefore, you can refer to a cell by its column number + row number. For example, cell B3 refers to the cell intersected by the third row and the second column of the worksheet.
Figure 54: Rows and columns of Excel
When using Laiye RPA to automate operations on an Excel table, you first need to open a workbook. The ensuing operations to a particular worksheet or cell are all within the scope of the opened workbook. When you are done using a workbook, you need to close it using a command.
Let's try to open a workbook with Laiye RPA. Click to expand the Software Automation category in the command area and expand the item Excel. Insert the first command listed, Open Excel File, into the assembly area.
Notice that this command takes three properties (Figure 55). The Path parameter takes the file location of the intended Excel file (we support .xls, .xlsx, .xlsm files). As we have mentioned earlier, the Path parameter can be the absolute path to the file or a relative path like @res"example.xlsx", which points to the "res" directory under the directory of the current process. Moreover, whenever the "\" appears in a string in Laiye RPA, rewrite it as "\\".
Figure 55: Open Excel workbook
If the given workbook file exists, we will operate on that file when running the process. If it does not exist, then Laiye RPA will create an empty Excel workbook file in that directory and operate on the created file.
The Visible property takes a Boolean value (true or false) and indicates if Laiye RPA will display the opened file using Excel. If the value is false, Laiye RPA will operate on the file without displaying the Excel software interface.
The Output To parameter takes a variable name to take on a value that refers to the Excel workbook we have opened. The value is a workbook object, and when we use other commands to operate on this workbook, we need to pass in the workbook object to indicate that this is the workbook to operate on. For example, in Figure 56, we store the workbook object in the variable objExcelWorkBook. In subsequent Excel commands, we will pass in objExcelWorkBook to the Workbook parameter.
Let's try to read the content of cell A1 in Sheet1 of this workbook. Insert a Read Cell command. Its properties are shown in Figure 56.
Figure 56: Read cells
As we have said, the Workbook property here should be the same as variable name we chose for the Output To parameter in Open Excel. Therefore, we use objExcelWorkBook to indicate that we want to read the cell content from the workbook we have just opened.
The properties Worksheet and Cell take in strings (text values surrounded by double quotes to indicate that they are strings) that specify which worksheet and cell to read. For our example, enter "Sheet1”"and “"1”" respectively.
This is great, but in our daily work, we often need to read in data from multiple cells. If we can only use Laiye RPA to read one cell at a time, this would quickly become cumbersome and inefficient. Luckily, Laiye RPA provides a Read Area command, which allows you to read the content of all the cells in a rectangular area. Insert a Read Area commands. Its properties are shown in Figure 57.
Figure 57: Read area
The Read Area command shares two common parameters with the Read Cell command: Workbook and Worksheet, which refer to the workbook and worksheet to read data from.
The Area property takes a string that indicates the area to read, specified by the top left cell and the bottom right cell of the area, joined by a colon. For example, filling in "A2:B6" will read an area from the worksheet that starts with the A2 cell as the top left corner and ends with the B6 cell as the bottom right corner, with a total of 12 cells distributed in 6 rows and 2 columns.
The Output To property has the variable name arrayRet, which will take on the values of the cells read. Inserting an Output Debug Information command and printing the value of arrayRet show that the output of Read Area is a two-dimensional array, with values like [["Joe", 123456 ],["Jack"， 654321 ],["James"，987654 ],["Jay"，741258 ],["John"，951753 ]].
Even though we haven’t formally introduced arrays and, in particular, two-dimensional arrays, we will get to know them in detail later. For now, you only need to know that we can use the Read Area command to read a rectangular area of an Excel worksheet and store it in a variable arrayRet.
In addition to reading the content, Laiye RPA also provides a series of Excel commands to modify the content of a workbook. Let's try to write "Jake" in cell A7 of Sheet1 of the example workbook. Insert the Open Excel command, and then insert a Write Cell command. Figure 58 shows the properties of the Write Cell command.
Figure 58: Write Cell
The properties Workbook, Worksheet, and Cell mean the same thing as their counterparts in the Read Cell command, selecting which cell of which worksheet of which workbook to write to. The Data property specifies the actual content to be written into the selected cell. It can be a number, a string, a variable, or an expression.
Commands that write data into an Excel file all share an important property: Save. Selecting Yes will cause Laiye RPA to save the change immediately, just like when we manually modify the content of an Excel file and press ctrl-s to save it. Selecting No results in the change not being saved immediately, and we have to use the Save Excel command or set the Save property to true on the Close Excel command to save the changes. Both methods save the changes, and it’s just a matter of personal preference.
Other commands that write data into an Excel file work similarly, so we refrain from going into details here. Do keep in mind that the Data property of each write command must be consistent with its write area for the data to be written correctly. Specifically, when writing into a cell, the Data property should be the data of that cell. When writing into a row of cells, the Data property should be a onedimensional array representing a row of data points. When writing into an area, the Data property should be a two-dimensional array representing several rows and columns of data points.
Similar to Excel, Word is also an important member of Office software suite. Word files are the standard medium for digitally encoding work documents. Therefore, automating Word is a necessity for a mature RPA platform.
Like in Excel, we need to first open a Word document to operate on it, and subsequent operations are all done on that opened document. After we are done making changes, we need to close the opened file.
Let’s try and open a Word document using Laiye RPA. In the command area of Laiye RPA Creator, under the Software Automation category, click on Word to expand it and select the first command,Open File, which allows us to open a Word file.
This command has five properties, as shown in Figure 59. Let's first look at the "file path" property. Here we need to specify to path a Word document (we support .doc and .docx files). Otherwise, the details are exactly the same as the Open Excel command. Here we open a test document example.docx, located under the “res” directory.
Figure 59: Open a Word document
Now, pay attention the two new properties Access Password and Edit Password. What do these mean? Sometimes, due to privacy considerations, we do not want other people to open our documents, or we do not want others to modify the document after opening it, so we set a password. There are two types of passwords. The first type is an Access Password, which you need to enter in order to open the document. The second type is an Edit Password, which you need to enter in order to modify the document. Laiye RPA will use the provided passwords to open the document. If the document has no password, simply set these properties to empty strings "".
The Visible property here has the same meaning as the Visible property of Open Excel, indicating whether to open the Word software interface when operating on the document.
The Output To property is similar to the Output To property of Open Excel. It expects a variable name, and this variable will refer to the Word document we open. When performing various read and write operations on the document, we need to supply this variable to the Document property of the command to indicate which document to read or write to. As in our example in Figure 59, the Output To variable is objWord, and in subsequent Word commands, we need to provide objWord to the Document property.
Now, let's read the content of this Word document. Insert a Read Document command after the Open Document command. The properties of this command are shown in Figure 60.
Figure 60: Read the Word document
The Document property should be the same variable as the Output To property in Open Document:objWord. This indicates that we’re reading from the document we just opened.
The Output To property is filled with a variable named sRet, which means that content we read will be outputted to the variable sRet. Insert an Output Debug Information command to print the content of sRet. After it runs, we can see something like Figure 61.
Figure 61: The output of reading a Word document
When we open the original document, we notice that the original Word document includes text, tables, and pictures, and the text is formatted in specific ways. The Read Document command will read all the text in a document, but it does not support parsing text formatting, tables, and figures.
Figure 62: Original Word document
The Read Document command operates on the entire document. Similar commands include Rewrite Document, Save Document, Save Document As, Close Document, Get Document Path, etc, all of which operate on the entire document. If we need to perform more fine-grained operations on the document, we need to use an important concept in Word: focus. Focus refers to the currently selected area in a Word document, and it is usually highlighted by the software. If no area is selected, the current cursor position is the focus. Therefore, the focus can be either a cursor position or a selected area. Word operations usually revolve around the focus. For example, if we want to change the font of a paragraph, we must select the text first, and then we can modify its size, color, style, etc. If we want to insert some text, picture, or other content, we also need to move the cursor to the insertion point first.
Let's see how to control the focus using Laiye RPA. Insert a Set Cursor Position command, which can move the cursor focus to a specified position. This command has three properties: the Document property is the objWord document object we have created earlier; the Move Times property is used with the optional Move Method property to indicate how many times to move; the Move Method property can be one of “character”, “line”, and “paragraph”, each corresponding to moving the cursor to the right by 1 character, moving it down by one line, and moving it down by 1 paragraph respectively. Here, we set Move Method to “line” and Move Times to 2, which instruct our command to move the focus down by two lines onto the third line. Please note that Move Times cannot be a negative number. This means that we cannot use this command to move up or back.
Figure 63: Set focus
Let's insert a Select Line command to highlight a specific line. This command has three properties: the Document property is the same as before, which takes the document object objWord; the Start Line and End Line properties indicate the selected area. Here, we set Start Line to 1 and End Line to 2, selecting lines 1 to 2 (2 lines in total).
Figure 64: Select row
In practical application, just using Set Cursor Position and Select Line does not work well. Why is that? Even though Word is a What You See Is What You Get software that allows you to format text and images, it often has some hidden formatting markings that affect the calculation of the positions of each character, line, and paragraph. This makes locating the focus difficult and leads to unexpected results. Here’s a little tip. We can mark the locations in a Word document we want to operate with text segments. For example, if we want to insert a name somewhere, then we can add the text “Name” in that location of the Word document. Use the Select Text command to find the location of the special marker “Name” you have added and use the Write Text command to replace the selected marker with the actual content. We can use this same technique to setup multiple special markers in a Word document and repeatedly use the Select Text and Set Cursor command to fill out a Word document.
Back to our example. After we have moved our cursor to the specified position or after we have selected the specified content, we can start executing edit operations. Available operations include inserting content, reading content, deleting content, setting content format, cut/copy/paste content, etc. Here, we demonstrate the Set Font Size command as an example. Insert a Set Font Size command after the Select Line. It has two properties: the Document property is set as the document object objWord we have created before, and the Font Size property specifies the font size to change the text to. Here, we set Font Size to 9, which would change the selected text’s font size to 9 points.
Figure 65: Set font size
Browser automation is an important part of software automation. Automating actions like retrieving data from a certain website and interacting with web-based service systems is dependent on automating browser interactions.
First of all, we need to open a browser by using the Start Browse" command. If there is already a browser program open on the computer, we can directly use that browser by invoking the Bind Browser command, which gives you the same output object to work with as the Start Browser command.
Figure 66: Start a new browser
The URL property specifies which link to open on the browser. Supplying "www.google.com", for example, will instruct the browser to open Google. You can always leave the link blank and open a website later using the Open Webpage command.
When the Open Browser command behaves unexpectedly, such as if the browser cannot be found or the specific URL cannot be opened, Laiye RPA will try repeatedly until some maximum time limit is exceeded. This time limit is set using the Timeout property.
There are two commonly used optional properties. The Browser Path property links to the executable file of the intended browser. This is useful when you have multiple versions of the same browser software installed on one computer. This property allows you to select the intended version to use. If this property is empty, Laiye RPA will search in the default installation directory of the browser and use that version. The Browser Parameters property allows us to pass in additional specifications when launching a browser. Besides just the default startup method when we launch a browser by doubleclicking its shortcut, browsers can actually be instructed to launch in very specific ways, like what website(s) to open by default, whether to full screen the browser window, whether to enable certain features, etc. We can specify these browser-specific parameters through the Browser Parameters parameter. To learn more about the supported parameters of each browser, please consult the relevant documentation.
After launching the browser, we can perform a series of operations to the browser itself and the webpages displayed on the browser. We can browse the web, enter texts in webpages, click on links and buttons, etc. For example, we can open Google’s homepage, enter "Laiye RPA" on the search bar, and click the "Google Search" button to retrieve the search results of "Laiye RPA". We can complete these steps using commands with target, which we have introduced in Chapter 3. Moreover, we can process the search results—scraping data, parsing data, etc.--using the commands under the Data Processing category. We will introduce Data Processing commands later.
In an information system, the most important content is its data. Nowadays, almost all information systems store their data inside databases. Besides using software clients to access the database, sometimes we also need to access and make changes to the database directly. Therefore, automating database operations is an indispensable part of RPA. Specifically, automating database operation allows us to login to a database using our username and password and interact with the database using SQL queries, all through a secure connection.
Let’s see how we can access a database through Laiye RPA. First, we need to establish a connection to the database. Insert a Create Database Object command, located under Software Automation – Database. This command will connect to a specific database and create a database object for that database.
Figure 67: Create database object
The Create Database Object command has three properties. The Database Type property specifies the type of the database we are connecting to. Laiye RPA currently supports MySQL, SQLServer, Oracle, and Sqlite3 databases. The Database Configuration property is a string that describes some key information used to create a database object. This string is relatively long and difficult to parse, but that is no problem. We can click on the button to the right of this property to view the property value broken down as a list of sub-properties (Figure 68).
Figure 68: Database configuration
Charset refers to the character set of the database, and normally we can just keep the default "utf8". Database refers to the name of the database we are connecting to. Host and Port refer to the IP address and port number of the database. In this tutorial, we connect to a database located on port 3306 of IP address 192.168.0.1, which can also be access through
"http://192.168.0.1:3306". Keep in mind that this database is not accessible to the public, so the connection would fail for you. If you want to try this command on your own databases, please change the configuration accordingly. The Username and Password sub-parameters refer to the username and password used to access the database. By configuring these parameters, we have created a database object.
Different types of databases often have different parameters. For example, an Oracle database does not have the Database sub-parameter, but it has a sid parameter, which amounts to a similar meaning. A Sqlite 3 database is a file database, which differs from the other three types of supported databases, which are relational databases. Therefore, of database may have different parameters. For example, the Oracle database does not have the "database" parameter but only the "sid" parameter with a similar meaning. The Sqlite3 database is quite different from the other three databases: MySQL, SQL Server, and Oracle are typical relational databases, while Sqlite3 is a file database. Therefore, the Database Configuration property of Sqlite3 only has a sub-property Filepath, indicating the location of the Sqlite3 database file.
The Output To property specifies which variable to assign the created database object to. Here, we create a variable objDatabase, and we will perform all subsequent database operations on this object. Now that we have created the database object, we can operate on the database. Laiye RPA supports two database operations: retrieving data from the database and modifying the database. To retrieve data, we can use the SQL Get One and SQL Get All commands. To modify the database, we can use the Execute SQL Query and Execute SQL Queries commands.
Let’s take a look at the SQL Get One command. This command executes an SQL query command and returns the first query result. Insert an SQL Get One command. For the Database Object parameter, supply our newly created database object objDatabase. For the Query parameter, write the SQL query statement to execute. Here, we write "select * from table1", which selects all data from the table table1 and returns the first result. The Output To parameter indicates a variable to assign the query result to. Here we supply iRet. We can use the value of iRet to determine whether the SQL query executed successfully.
Figure 69: Perform a single SQL query
Finally, remember to use Close Connection command to close our database connection. The only property of this command is Database. We supply our database object objDatabase to close our connection to that database.
Figure 70: Close connection