Example of reading data from a spreadsheet
- Updated: 2023/08/31
Example of reading data from a spreadsheet
Build a bot that reads data from one Excel worksheet and writes it to another worksheet.
Prerequisites
Suppose an Order Processing team receives order data in a spreadsheet as part of the Order Fulfillment process. The team automates the process of creating a priority shipping list from the spreadsheet and sharing it with the Picking department. The sample bot in this tutorial performs the following tasks:
- Reads spreadsheet data that contains a list of orders using the Excel advanced package.
- Creates a new spreadsheet containing a list of orders that need to be shipped as a priority.
For this tutorial, you can download and use a sample Excel file from here.
Procedure
-
Create a new bot.
- On the left panel, click Automation.
- Click .
- In the Create Task Bot window, enter Priority List in Name.
- Click Create and edit.
-
To read data from the spreadsheet that contains the list of orders, open the
Excel sheet.
- From the Actions panel, find and add the action to the Bot editor.
- Click File and select the sample Excel file you downloaded.
- Select Sheet contains a header.
- Select Specific sheet name and enter order_list.
- In Create Excel session, click Local session, and enter orderlist as the session name.
-
For the list of priority orders, create a new spreadsheet.
- From the Actions panel, find and add the action to the Bot editor.
- In File path, specify a file path for the new file. For example, C:\\bots\priority-orders.xlsx .
- In Sheet name, enter priority orders.
- In Create Excel session, select Local session and enter prioritylist as the session name.
- Save the changes.
-
Copy the header from the source spreadsheet to the new spreadsheet.
- From the Actions panel, find and add the action to the Bot editor.
- In Select range of cells to be returned, select Specific rows, and then enter 1 in both the From row and to row fields.
- In Session name, enter orderlist.
- To store the copied values to a variable, create and assign a variable named HeaderData in Assign value to the variable. The values are stored in a data table variable.
- Save the changes.
-
Write the copied header to the new sheet.
- Find and add the action to the Bot editor.
- In Enter data table variable, select HeaderData.
- Click Specific worksheet and enter priority orders.
- In Specify the first cell, enter A1.
- In Session name, enter prioritylist.
- Save the changes.
-
Before you write the data to the new spreadsheet, make sure that the A2 cell is
selected on the new worksheet. To do so:
- From the Actions panel, find and add the action to the Bot editor.
- In Specific cell, enter A2.
- In Session name, enter prioritylist.
- Save the changes.
-
Loop through the list of orders in the source spreadsheet.
- From the Actions panel, find and add the Loop action to the Bot editor.
- From Iterator, select For each row in worksheet for Excel advanced.
- In Loop through, select All rows.
- In Session name, enter orderlist.
- In Assign the current value to this variable, select Record, and then create a variable named order-record.
- Save changes.
-
Select the records where ShippingType equals Priority.
- a. From the Actions panel, find and add the If action within the Loop action.
- In Condition, select String condition.
- In Source value, enter $order_record[4]$ to select the fifth column in the current record.
- In Operator, select Equal to(=).
- In Target value, enter Priority.
- Save the changes.
-
If a matching record is found, write the record to the new spreadsheet. You can
achieve this by using the Set cell action as many times as the
number of columns in the source data. However, this example uses the Loop action to achieve the same result.
-
Reset the recordValueCounter variable to zero.
- Find and add the action after the inner loop but within the If action.
- Select recordValueCounter in both Enter number and Assign the output value to a variable fields.
- In Enter increment value, enter 7.
- Save the changes.
-
Move the cursor to the next row before the next iteration.
- Find and add the action within the first Loop action (after the Decrement action).
- Click Active cell and select Beginning of the row.
- In Session name, enter prioritylist.
- Find and add the action.
- Click Active cell and select One cell below.
- In Session name, enter prioritylist.
- Save the changes.
- Run the bot.