ajReadFile function
Description
The ajReadFile function imports the content of a file into a defined area in Excel worksheet. It is mainly used for loading data from a text file to Excel for processing.
Syntax
ajReadFile(file_location, [column_separator], [left_enclosure_character], [right_enclosure_character], [begin_at_line], [number_of_lines], [character_set], [skip_empty_row], [run_condition], [run_by_function_point_only])
Argument Name | Argument Type | Description |
---|---|---|
file_location (required) | String | The path of the file to be read. |
column_separator (optional) | String | The character that is used to split input record into fields. In a CSV file, a comma is usually used. |
left_enclosure_character (optional) | String | The left enclosure character. When a Column_separator is used, a field value itself can contain a column separator character. For example, when using a comma-delimited format and with a field containing "Cathy, Chow" as its actual value, the field would be separated into two fields as there is a comma in it. Using an enclosure character in writing and reading operations can solve this problem. The default value is an empty string (“”) which means no enclosure character. |
right_enclosure_character (optional) | String | The right enclosure character. The default value is an empty string (“”) which means no enclosure character. |
begin_at_line (optional) | Double | The starting line to begin the extraction of the content of the file. It will start from the beginning by default. |
number_of_lines (optional) | Double | The number of lines to be extracted from the file. It will read all the lines starting from Begin_at_line and till the end of the file by default. |
character_set (optional) | String | The character set used when writing the file. Some common character set codes are UTF-8 (ISO 10646 Unicode), GB2312 (Chinese Simplified), Big5 (Chinese Traditional) and us-ascii. The default value is UTF-8. |
skip_empty_row (optional) | Boolean | If it equals FALSE, the empty rows will be read from the file. If it equals TRUE, the empty rows will not be read from the file. The default value is FALSE. |
run_condition (optional) | Boolean | The function will run when the value is TRUE. Otherwise, it will not run. The default value is TRUE. |
run_by_function_point_only (optional) | Boolean | If it equals FALSE, the function can be executed through ‘Excel Calculation’ (can be either Automatic or Manual, Calculate Now or Calculate Sheet) or Preview Function Point. If it equals TRUE, the function can be executed with Preview Function Point (AlchemyJ ribbon \ Preview Function Point) only. The default value is TRUE. |
The function will return:
1) Return Value: Contents of the selected file
2) Return Type: Multiple values (array formula)
Examples
Here are a few examples of ajReadFile function.
Example 1
The file below uses comma as the column separator and round brackets as the left and right enclosure character.
We call ajReadFile without specifying any column separator and enclosure character. The content is read line by line. Each cell contains the whole line.
Example 2
In this example, it reads the same file using ajReadFile but we specify comma as column_separator, ( as left_enclosure_character and ) as right_enclosure_character. ajReadFile properly loads the content in its intended format.
In addition, we set begin_at_line to 2 and the number_of_lines to 3 so that only lines 2 to 4 are loaded.
Click here to download the use case workbooks for further reference.
Error Scenarios
It will return #VALUE! when missing any required parameter or mismatch parameter type. Besides, system will raise error for below scenario(s).
Error Scenario |
---|
Invalid file location. |
Character set is not supported. |