I’m looking for tips to automate a process. Basically I have 30 test questions saved in a Word document and I would need to randomly select 20 of those, could you help me? I’m a beginner and I still haven’t been able to develop a solution for this
When dealing with word documents, we would have to perform some more operations in order to get the Desired expected output format.
Here, we would like to know How are the Questions arranged / also if there are choices available in it. We can then deduce a pattern and maybe use regex to extract all the Questions from the Document grouping each questions so that we can select the questions randomly.
In order to correctly understand your requirement, we would need sample of the input data and How would you like it to be presented as the expected Output.
If you still have an option to choose between Word/Excel, to make it simpler, an Excel spreadsheet could be used.
Since we are dealing with Word documents, the need for Word Activities pack is a must in order for us to read the data as Text (String), Pick up the Questions as required using String Manipulations and then Write the Data Back.
The Steps are explained briefly below :
Read Text Activity :
Reads the Input word document that is supplied to it.
Gets the Output in the form of String or text data.
Declare a Random type and initialise it.
This is the key step/steps for selecting the questions at random.
Split the text data into array of Strings making each item in the array a Single Question.
Here, Regex methods are applied for proper split of the Combined Questions into Individual questions.
We test/check the patterns using .net regex / regex 101 applications/ UiPath’s Matches Activity and apply the pattern to our Expressions.
We get the Output as Array of Strings with each item a Individual Question.
Ordering of the individual Questions and Converting to String.
Here, we use Linq method OrderBy to perform the Random Ordering on array using the random object created.
We then take the Required number of questions using the Take(15) as 15 was the required number.
Next, As the Numbering of the Questions also resides along with it’s text data, we would need to re-order the number according to the new ordering. the Select(x,i) uses the Ordered questions one by one along with it’s index number, we then use regex replace method to replace just the number, thus modifying the value for the Selection.
As we now have the required output in the form of Array, we would have to convert back to string which can be done using String.Join().
Word Application Scope - Append Text Activity
As we have the Output (Ordered Random Questions from Input), we could write it to a Different word file. Which you could then use for later operations.
The explanation might not cover everything, but do read more on the topics Regex and Linq below for better understanding :
For this the logic would be the same, it’s just that we would have to apply Datatable methods as we are reading the data from Excel and perform Similar operations on it as well. But it also depends on how the data is formatted/Structured in the excel sheet.