Appending CSV File Using String BuilderLast Updated: 07/19/2016 Introduced in Verision: 2.0 |
This tutorial demonstrates how to append new rows to the CSV file using String Builder steps. These steps can be found in Data > Text > Builder category in the ToolBox.
Example:
We begin in the Designer Folder with clicking Create Flow on the Folder Actions panel.
Then, we Name the Flow and click Create to proceed to the Flow Designer.
First, we need to create Data that will hold our CSV file. We add Create Data step from Favorite Steps category in the Flow Designer Wizard.
In the resulting pop-up we click Add New link to add new Data Definition.
In the Add DataDefinitions window we Name our Data and define its Type FileData. Then we click Ok to save and close this pop-up window.
When we finished defining our Data we click Add in the Data Definitions window to continue.
To use Text Builder steps in the Flow we need to Create a String Builder first. We add Create String Builder step from All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard.
Because the Contents of our CSV file is of type Byte Array, we add Get String From Bytes step from All Steps [Catalog] > Data > Text category to convert CSV file Contents into String.
Next, we can pick a CSV file for our Create Data step. The CSV file we are going to work with in this Example looks as following…
We select Create Data step on the workspace and Step Information Layer pops up. Then, we use Value picker for our OriginalCSVFile Data in the Inputs section to select the desirable file.
Next, we need to Append the String from Original CSV File to our String Builder. We add Append Line step from All Steps [Catalog] > Data > Text > Builder category.
Then, to map data for the Get String From Bytes step we select Show Mapping Editor from step’s Options Menu.
In the Mapping Editor we expand OriginalCSVFile and connect its Contents to the bytes field. Click Ok to save and close Mapping Editor.
Back in the Flow Designer we call Mapping Editor for Append Line step.
In the Mapping Editor we connect CreateStringBuilder_Output to the data field and GetStringFromBytes_Output to the lineToAdd field. Then save and close Mapping Editor.
Next, we are going to create a simple Form for the user to be able to add new rows. We add Show Form step from Favorite Steps category in the Flow Designer Wizard.
In the resulting pop-up window we Name the Form and click Create to proceed to the Form Designer.
This simple Form is designed as following… It has a Title implemented with Label component from the Data category. Then, it has a Text Block component from Data category for user to input new Rows. And, it has Done button, implemented with Button component from Actions category. When we finish designing our Form we can save and close Form Designer.
Next, we are going to add new rows from the Form step to the String Builder. We add Append Text step from the All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard.
Then, we Show Mapping Editor for this step.
In the Mapping Editor we connect CreateStringBuilder_Output to the data field. Then, we connect NewRows outcome from the Form step to the textToAdd field. Click Ok to save and close Mapping Editor.
Back in the Flow Designer we add To Text step from the All Steps [Catalog] > Data > Text > Builder category in the Flow Designer Wizard. With this step we are able to retrieve Data from the String Builder in the form of String.
We select Show Mapping Editor option to map Data for the To Text step.
In the Mapping Editor we connect CreateStringBuilder_Output to the data field. Then, we click Ok to save and close Mapping Editor.
Next, we add Get Bytes From String step from All Steps [Catalog] > Data > Text category in the Flow Designer Wizard. With this step we are going to convert String output from To Text step into an Array of Bytes. Then we will be able to use this Array of Bytes as Contents for our New CSV file.
We use Step Information Layer to map data to this step. For the str field in the Inputs section we choose Select Value Mapping type, and using Path picker point to the ToText_Output.
Then, we click Add Step in the Step Information Layer to add final step to the Flow.
We add Show Form step from Favorite Steps category in the Flow Designer Wizard. This Form will serve for the user to be able to Download an updated CSV file.
In the resulting pop-up window we Name the Form and click Create to proceed to the Form Designer.
This Download File Form is designed as following… It has a Title that is displayed with a Label component from the Data category. It has a Close Button control from the Actions category. Finally, it has File Download component from File Handling category. With this control user will be able to download the File that is mapped to this control. We save the Form and close Form Designer, when finished.
Back in the Flow Designer we connect Close outcome from Download File Form to the End Step in our Flow. Then, we select Show Mapping Editor from the Download File Form’s Options.
In the Mapping Editor we use Build Data Mapping type for the DownloadFile control.
Next, we connect GetBytesFromString_Output to the Contents field. Note, that we need to use .csv file extension in the FileName field. We can save and close Mapping Editor.
This completes our Flow. We click Debug Flow link on the top panel in the Flow Designer to test our Flow.
Note that in Decisions version 3.5 and above, you’ll need to click Test Flow to access the Debugger.
Our Form for adding new rows opens in the Debugger. We add new coma separated rows and click Done.
Then, our Download File Form opens in the Debugger. We click Download File…
We confirm File Download process…
Next, we Name our file and save it.
In the Debugger, Decisions informs us that the File is Saved.
We click Close on the Download File Form…
Diagram Tab in the Debugger indicates that our Flow ran to the End Step with no errors.
We locate and open our New CSV file… Confirm that new rows of Data were added.