Skip to Content
Technical Articles
Author's profile photo Monica Paiu

SAP Intelligent RPA: Read Data from Excel: Fixed Header

Introduction

This is a submission of the Enhance your bot building with templates blog post series.

In this post I will show you how to automate a process in which you will have to read an Excel file with a fixed(known) header, but an unknown number of rows.

What will we learn in this tutorial:

  • How to use the Excel Library;
  • How to read unknown number of rows from Excel File;
  • How to use Loops;

Steps to follow:

  1. Create a new workflow;

2. Import Excel Library Scripts;

3. Add Activities and functions from ‘Excel Lib’ category;

4. Use a loop for dynamic reading;

5. Read the file row by row;

6. Display the read values;

Prerequisites:

  • Desktop Studio 1.0.9.16;
  • Microsoft Office;

Instructions:

1. Create a new workflow

Create a new project and give it a name.

Go to ‘Workflow’ perspective and create a new workflow.

2. Import Excel Library Scripts

In the work space of your new workflow you can now add activities from ‘Excel Lib’ Category, But, in order for your project to compile and run without errors, you first have to enable the Excel Library scripts in your project:

-> go to ‘Scripts’ perspective;

-> select ‘Project’ tab(bottom-left corner);

->right click on ‘GLOBAL’ from the Panel;

->select ‘Include library Script’: The ‘Add Library Script’ window pops;

-> enable ‘Excel integration’;

->click on ‘Save’;

 

 

3. Add Activities from ‘Excel Lib’ category

First Activity that we have to add in the workflow is ‘Initialize Excel‘ , which initializes Excel Library. This one is always used in pair with ‘End Excel‘ activity in order to close the Excel Library once you are done using it in your project. Both activities do not require parameters.

Second activity in the flowchart is ‘Open an existing Excel file‘ in which we will set as parameters the path and the name of the file that we want to read. The name of my excel file is CreateDatatype.xlsx. Because the excel file i want to read was saved in the ‘log’ folder inside my project, I will use the ctx.options.path.log to acces the location:

 

Now that we accessed our file, we can start reading its content:

The saved data should be saved in some variables, and for it we have to create the proper Context structure:

4. Use a loop for dynamic reading

In the next steps we will be reading the data row by row. In order to fully automate this process, we will be using:

  • Loop structure in order to access all the rows;
  • ctx.excel.sheet.getLastRow() function from the Excel Library, in order to determine the number of rows inside the file. This value will be used to determine the number of times the loop will be executed.

The Loop structure contains 3 elements:

  • Start Loop :  to determine the start of the loop;
  • Exit Loop( can be used at the beginning or the end ): to check the condition to exit the loop;
  • Loop to start: to limit the steps that should be executed in the loop;

In my example i chose to use ‘Exit Loop’ activity at the beginning:

 

 

‘Exit loop’ requires as parameter the condition based on which the bot will end the loop: in my example, i want to exit the loop when the iterator, sc.localData.Startloop(is automatically created) will reach the same value as the number of rows to be read from the file.

5. Read the file row by row

Now we can add the functions that read data from the file and stores the values in the context. For this i will be using ‘Get one value from a cell‘ activity for each column.

 

As parameters, i used:

  • sc.localData.Startloop(iterator)+2 to indicate the row number;
  • the letter of the colum to indicate the column to be read;
  • $data$ to indicate where to store the value;

6. Display the read values

Before closing the loop, we want to print the values in the console, in order to see the data that have been read. To do that, use the ‘Log‘ activity from ‘System‘ category. As parameter, enter the message you want to display:

 

Next step should be ‘Loop to start‘ activity, but first i should add a delay of 10 ms in order to avoid the  ‘Error, Out of stack space’ :

 

I ended my workflow with ‘End Excel‘ activity to mark the end of using the library, and ‘End scenario‘ in order to mark the end of my process.

 

Conclusion

This blog post should help you to understand the use of the ‘Excel Library’ and how to integrate Loops for dynamic activities. At the end, you should be able to understand this functionalities and use them according to your scenario.

Assigned Tags

      19 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Chakravarti Gupta
      Chakravarti Gupta

      Thankyou for Information. It was helpful.

      Author's profile photo Kapil Karwa
      Kapil Karwa

      how can we do start and end loop with SAP GUI , Do we need to start new session every time?

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      Hi,

      Between ‘Start Loop’ and ‘Loop to the Start’ activities you should only integrate the steps that must be executed multiple times( each time the bot enters a new iteration).

      Opening a SAP GUI session should be done only once, and this step should be integrated before starting the loop.

      Hope this answers your question.

      Best regards,

      Monica

      Author's profile photo Edwards Lee
      Edwards Lee

      Hi Monica

      Very useful information!

      I tried to follow the description on the blog, but I ran into some problems。How do I create the proper Context structure?

      Could you share a more detailed example?

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      Hi Lee,

      The context can be created in the Workflows perspective, by opening the Context panel. In here you can add elements to create your desired structure by right-clicking in the blank space and select either Folder or Item.

      A Folder can contain multiple Folders and Items, and an Item cannot contain anything else( is a final node).

      For each type of element, you can define it as an array by checking the box on the right, on each element's level. See example below:

      Keep in mind that in Desktop Studio we are working with a JavaScript framework, therefor our Context Structures are in fact JSONs.

      Setting the proper Context structure depends on the type of your data set and the way you want to manage it.

      Hope this helped.

      For more information, visit the Help page about the Context

       

       

       

      Author's profile photo Karl Johann
      Karl Johann

      Hello Monica,

      thank you for this guide. It helped me a lot with my first steps with the SAP IRPA tool!

      Best regards

      Karl

      Author's profile photo Gaurav Malik
      Gaurav Malik

      Hello All,

       

      I am new to this tool and I would really appreciate if someone answer below queries:

      1. What is the significance for start loop+2, and what is the criteria for initializing the row in the excel?

      2. Is there a separate blog for looping logic as well such as this one?

      3.What is the significance for getlastrow('A1')-2 and why we are subtracting -2?

       

      Regards,

      Gaurav

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      Hi,

       

      1.I added value 2 to sc.localData.Startloop at step 5 because:

      • sc.localData.Startloop starts from value 0, and the numbering of rows inside any Excel file start from 1 ( so we add 1 to adjust the starting point gap);
      • first row in the Excel file is the Header, and we do not need to read it. So in order to skip the header we add another 1 )

      About initilizing the row in Excel file, can you be more specific to what part of the blog you are referring?

      2. You can check the developer guide on Loop activities. I'll update if i find a blog about it.

      3.Please read the documentation about the getLastRow() function.

      It returns the first empty cell, not the last row completed(so we extract 1); we extract another one to elevate the starting point gap between excel row numbering and the value of sc.localData.Startloop.

      Hope this helps.

       

      Best regards,

      Monica

       

       

      Author's profile photo Kurian Jacob Eldho
      Kurian Jacob Eldho

      Hello Monica,

      Very nice blog!

      I got 2 queries.

      1. I am using Desktop Studio 1.0.8.36. Where can i download the version 1.0.9.16 ?
      2. When I added the activity 'System->Log', i do not get an input field to write the message. Is this feature available only in the version 1.0.9.16 or am i missing some other settings ?

      Could you please provide help here.

      Thank you in advance,

      Regards,

      Eldho.

      Author's profile photo Avinash Y
      Avinash Y

      Hi,

      i have implemented the code as provided but getting the following errors while implementing,can you please have a look and suggest

       

      Error%20while%20debugging%20the%20code

      Error while debugging the code

       

      Thanks

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      Hi Avinash,

       

      Make sure that each Loop that you created contains all three components:

      • Start loop
      • Exit loop (condition check)
      • Loop to the Start block

      Best regards,

      Monica

      Author's profile photo Avinash Y
      Avinash Y

      Hi Monica,

      Attached the flow please let me know if anything is missing,getting the same error as posted in previous post.

       

      Conditions are mentioned as shown above in the article,screenshot below for your reference.

       

      Kindly have a look and suggest.

      Thanks

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      Be careful when renaming your objects from the loop, as they are connected based on the name. If you change the name, make sure all of them are synchronized.

      Author's profile photo Aleksandar Vukojevic
      Aleksandar Vukojevic

      Hello Monica,

       

      thank you for this very helpful post about working with Excel.

       

      I’m getting the ‘overstack error’ even when setting the delay at 1000 ms. I’m using the version 2.0.1.49 of the Desktop Studio. Do you know how I can solve this issue?

       

      Thanks in advance!

      Author's profile photo Monica Paiu
      Monica Paiu
      Blog Post Author

      HI Aleksandar, Use the debugger to indentify at which step you get the error. Add a delay before that step.

      Author's profile photo Aleksandar Vukojevic
      Aleksandar Vukojevic

      Hello Monica,

       

      I fixed the issue. I had a sleep instead of a delay.

       

      Thank you very much!

      Author's profile photo KaHo Yim
      KaHo Yim

      Hi Monica,

      Thanks for your blog! This really helped a lot however may I ask one question? This is confusing to explain but hopefully I make it clear enough

      Let's say I need to check in an excel file that each row has all the required information needed for that row and if all rows are valid then I can proceed with the workflow

      However I've found one case that If I have 5 rows in an excel where the first and second row are valid but the third is not as it has one value missing in a cell for example in cell A4 (A4 is empty).

      Then If I use the exit loop condition as shown here (.getLastRow('A4')-2) when reading the excel it will only read to the 3 row and continue on with the rest of the process i.e. not checking/reading the 4th and 5th rows?

      Is there anyway to get around this problem? Apologies if i'm not clear enough but any help would be appreciated thanks!

       

       

      Best Regards,

       

      Author's profile photo Edgar Chavez
      Edgar Chavez

      Hi, Monica.

      Your post is great.

      I have 2 questions:

      It is convenient to work that way, eg, while we read the file, records are added to the web, this would not delay or make the process take longer.

      What advantages does it have if I first store all the excel info in an array and then start the loop in the application.

      I hope to be clear.

      Best Regards.

      Author's profile photo Paul Cafita
      Paul Cafita

      Hi Monica

      I have several vendor invoices in an excel file with two sheets “Header” and “Line Items” (one “Header” row have many rows in the “Line Items sheet”)

      Based on them, I have to post in SAP FI several documents in a single bot execution, one document for one “Header” row

      I need a loop for Header and another for Line Items

      how can I make the connection between row 1 from sheet “Header” and the related rows from sheet “Line Items”?

      Best Regards,