Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Insert Excel Sheet name into a Field

Hi everybody,

I'm uploading several Excel files in Qlik Sense. I know their name and I don't want to loop over all files, but just to read the name of the sheets (I don't know if I've been clear, for file I mean test.xlsx and the sheets are within the file like Sheet1, Sheet2,...) in order to put them in a field.

So I've got this situation:


[Table1]:

LOAD *

FROM [lib://Desktop\test.xlsx]

(ooxml, embedded labels, table is Sheet1);

and I want to read "Sheet1" and put it in a field.

How could I do it? I tried with different macro using the ActiveDocument.ActiveSheet.GetProperties.Name method, but I did'n get what I wanted.

Thanks a lot for your help,

best regards

Nicolò Cogno

10 Replies
vlad_komarov
Partner - Specialist III
Partner - Specialist III

Nicolò,

Your question is a bit confusing: Are you trying to extract the Sheet Name during the load and create a special field with it? Or are you trying to modify your script to load all sheets into different tables (named by each Excel sheet)  in one script?

VK

Not applicable
Author

I'm sorry Vladimir,

you're right. I'm trying to do the first of the things you said, I'd like to extract the Sheet Name during the load and create a special field with it.

I hope this helps,

thanks a lot in advance

vlad_komarov
Partner - Specialist III
Partner - Specialist III

I think the best way in your case is to use the following:

LOAD

...

TableName(n) as TableName

FROM .....;

The only problem is that Qlik requires a 'n' to be defined "as number of the specified table". The first table has number 0. This number should be based on number of tables currently in memory (based on the sequence of the load, I believe)...

I did not have chance to test it, but you should be able to drop each table after each loading statement (since you can save it into QVD anyway), so in this case the 'n' could always be 0 (since only one table will be loaded at a time).

Hopefully, this solution will work for you.

Regards,

Vladimir

P.S. When applicable, please mark this answer Correct or Helpful.

Not applicable
Author

Hi Vladimir,

thanks a lot for your help!! I used your code, but I'm actually not getting any data into the TableName field.

Do you have any other suggestion?

Thanks again,

Best regards

Nicolò Cogno

Not applicable
Author

Hi Vladimir,

I found out that the only way to make that works is to use the TableName(i) after loading that table. Is there a way to achieve this within the load script of the table that I need the name of?

Thanks again

Best regards

Nicolò Cogno

swuehlstalwar1vlad.komarov

vlad_komarov
Partner - Specialist III
Partner - Specialist III

Nicolò,


I am surprised that TableName(i)  call does not work inside the load statement.

But your approach is correct too.


You can do a data load first, get the Table name after that and do a JOIN with Resident load to add your new field after that... Couple more steps, but it should not take much more time...


Regards,

Vlad

neelamsaroha157
Specialist II
Specialist II

Try this:

This script loads data from all the sheets in excel and variable 'sheetName' creates a fields with all the sheet names.

ODBC CONNECT32 TO [Excel Files;DBQ=$(file)];

SheetNames:

SQLtables;  // Loads all sheet names in the Excel file.

DISCONNECT;

SystemTable:

NoConcatenate

LOAD *

Resident SheetNames

Where Not Match(Upper(TABLE_TYPE), 'TABLE');

DROP Table SheetNames;

FOR index = 0 to NoOfRows('SystemTable')-1  // Loops for each sheet in the Excel file.

LET sheetName = PurgeChar(PurgeChar(Peek('TABLE_NAME', index, 'SystemTable'), Chr(39)), Chr(36));

Load * ,

  FileName() as File_Name,

  '$(sheetName)' as Sheet_Name

From [$(file)]

(biff, embedded labels, table is [$(sheetName)$]);

NEXT index

DROP TABLE SystemTable;

NEXT

Hope this helps.

Not applicable
Author

Hi Neelam,

first of all thanks for your help.

I tried with your help, but it gives me this error

CONNECTs other than LIB CONNECT are not available in this script mode.

Do you know which could be the problem?

Best regards

Nicolò Cogno

neelamsaroha157
Specialist II
Specialist II

I am not sure of the error nicolocogno‌ but instead of using the connect string directly you can try to open your excel via ODBC connection. It might resolve the issue, but again not sure.