Importing Excel to internal table with same layout - abap

I know of function module ALSM_EXCEL_TO_INTERNAL_TABLE. This FM creates an internal table with three columns (row, column, value). But I want to create an internal table which has the same layout as my Excel sheet. How can I achieve this?

You can use class cl_mass_spreadsheet_service if you are uploading the excel in foreground. See my example code below:
DATA:
lv_file TYPE if_mass_spreadsheet_types=>file_name,
lt_result TYPE STANDARD TABLE OF zsd_salesorder_create. "your result table
lv_file = 'C:\some_file.xlsx'.
cl_mass_spreadsheet_service=>fill_table(
EXPORTING
iv_file = lv_file "full path+name of file. See method navigate_to_file below
iv_from_file = abap_true "use to upload from excel/CSV
iv_from_clipboard = abap_false "use to copy directly from clipbiard
iv_tabname = 'Order_Create' "can be whatever
CHANGING
ct_table = lt_result "if ct_table have the same column names as the excel file, the order of the columns does not matter
).

If you upload the data with FM ALSM_EXCEL_TO_INTERNAL_TABLE, you can LOOP through the internal table this FM is uses (the one with row, column, value as you mentioned) and fill your own internal table (which looks like the Excel sheet) accordingly.

You could use
cl_mass_spreadsheet_service=>import_from_file
as well but without ddic structure.
Unfortunately, these methods will actually open and show EXCEL...

Related

How to create hyperlinks in VBA for mutiple cells?

I have to create hyperlinks for a list of variables to get easily to the datas in a database. My database contains summary of many variables coming from different sources. My problem is, I have to skip multiple rows fill with duplicates and blank cells. The number of rows to skip depends for every variable. I also want that my hyperlinks have the name of the variable from the database (example : Customer_Since). Is there a way to create a loop to skip the good number of rows and create a hyperlink? Example : if I click on the link named Customer_Since, it takes me to the row that contains the summary of the variable Customer_Since. Can somebody help me?
To create a hyperlink you can use this code:
ThisWorkbook.Sheets("SheetName").Hyperlinks.Add Anchor:=Range("A10"), Address:="", SubAddress:="Sheet2!B5", TextToDisplay:="Link"
where:
Anchor - cell, where there will be a hyperlink;
SubAddress - range to navigate to;
TextToDisplay - text in the link.
You can use another vba code with your rules to correctly insert Anchor`s range.
Or please provide more information and examples of the input and output data to get help.

How to keep the first space in Cell on a .xlt

I worked on an export of data from an ERP to Excel but I encoutered a problem.
When I received my datas on my model Excel (.xlt, i don't have a choice for the extension...), all first spaces of fields in the ERP disappeared on my worksheet...
An exemple (Here, spaces before "Holder") :
And now, on excel, without spaces... :
And the last information, I think the problem is only on file type .xlt (97/03) (The only one I can use of course...) because when I try an export in .xls, there is no problem.
I already tried to change the type of cell in Text or Standard but it doesn't work.
Did you have a solution ?
Thanks !
Let me outline a typical solution:
You have a "data source" you cannot control - in this case it's an xlt file that somewhere on your hard drive - call it export1.xlt
You want to add the data from a data source (export1.xlt) to a "database" which could just be another aggregate spreadsheet or whatever. Let's call it database1.xlsx.
Typcially you would create a marcro inside database1.xlsx that knows how to import data into intself - in this case let's say you give a path e.g. C:\temp\export1.xlt and tell it to copy that data to Sheet1.
When you run that macro it will open export1.xlt, read the data into Sheet1 of database1.xlsx, and perform any necessary post-processing.
In this case the post processing could simply be looping over every cell to looking for a missing space.

Calling to a different stimuli lists

I need to call to a different column with the stimuli depending on a previous given response. I.e. age
ask for age
if age = X --> choose a certain stimuli column (or a certain xlsx file)
What would be the basic procedure to do this. I am new in Psychopy.
Thanks a lot
Elena
You can add a field for age to the experiment info dialog box shown at the beginning of each session. Then you can refer to that value later in code as expInfo['age'].
Let's say you have two columns in your conditions file, labelled list_A and list_B, and you want to use either of those to put values into a text stimulus. In that text stimulus, put a variable called something like $text_list. Then in a code component, in the "begin routine" tab, put something like this:
if expInfo['age'] > 30:
text_list = list_A
else:
text_list = list_B
The same principles apply if you want to select between .xlsx files. Put a variable name in the conditions file file field of the loop dialog, and in some routine prior to the loop, set that variable to contain the desired filename.

Read spreadsheet data with cell length > 32 characters into itab

I am trying to read an Excel file into my internal table with cell values longer than 32 characters. I am using the function KCD_EXCEL_OLE_TO_INT_CONVERT to read the files in.
I have tried copying the function module and table using SE37 and SE80, but it will not let me save the table with a value named ROW.
Is there a better function module I'm not seeing or is there a way I can make a Z_ copy of the table and FM to allow me to change the length of the value column in a kcde_cells formatted table?
My program works doing everyting except reading in the 33rd and beyond characters, so I know the rest of the functionality is fine. I just need to get the length of the read in value to be longer to accommodate longer cell contents.
Edit: Adding the code I used to upload the file.
CALL FUNCTION 'KCD_EXCEL_OLE_TO_INT_CONVERT'
EXPORTING
filename = infile "Input file.
i_begin_col = st_col
i_begin_row = st_row
i_end_col = e_col
i_end_row = e_row
TABLES
intern = ttab "Internal table for storing the Excel data.
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
FORMAT COLOR COL_BACKGROUND INTENSIFIED.
WRITE:/ 'Error Uploading file'.
EXIT.
ENDIF.
IF ttab[] IS INITIAL. "Internal table is empty.
FORMAT COLOR COL_BACKGROUND INTENSIFIED.
WRITE:/ 'No Data Uploaded'.
EXIT.
ELSE.
SORT ttab BY row col.
LOOP AT ttab.
MOVE ttab-col TO index.
ASSIGN COMPONENT 'ROW' OF STRUCTURE itab TO <fs>.
MOVE ttab-row TO <fs>.
ASSIGN COMPONENT 'COL' OF STRUCTURE itab TO <fs>.
MOVE ttab-col TO <fs>.
ASSIGN COMPONENT 'VALUE' OF STRUCTURE itab TO <fs>.
MOVE ttab-value TO <fs>.
APPEND itab.
CLEAR itab.
ENDLOOP.
ENDIF.
As I stated in similar question , use FM FILE_READ_AND_CONVERT_SAP_DATA. It allows reading cells with the length up to 256 characters.

An item with the same key has already been added - csvreader.fieldcount

I'm trying to create an import program from CSV.
My code is
csv = New CsvReader(New StreamReader("CSVFileLocation"), True)
Dim fieldCount As Integer = csv.FieldCount
The error message "An item with the same key has already been added." on the second line.
If I changed "HasReaders" to "False", there's no such error. But, I'm not able to get the Headers.
Could somebody help me on this, please?
FYI: I'm using Visual Studio 2010 version.
Regards,
Richard
Check that your CSV file may have duplicate column names, or multiple empty cells, in the header row?
If that's the case, try to loop through your csv object, and try rename the headers in code before calling the property FieldCount.
My guess is that the CsvReader class is going through the first row adding strings to a dictionary, and the header row has two cells with the same value (so two identically named fields). Take a look at your data and see if this is the case. Alternately, if you have access to the source code for CsvReader, you could have it handle this case by naming the second field something slightly different (e.g., by appending a "1" onto the end of its name).