Working With Excel Files via COM: set required sheet as active using javascript
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Working With Excel Files via COM: set required sheet as active using javascript
I am using excel, which has two sheets. I have to use COM to read excel because of microsoft access database issue.
I have to access the second sheet in the excel based on its name.
Code is as below
var Excel = getActiveXObject("Excel.Application");
Excel.Workbooks.Open(excelPath);
var excel_sheet = Excel.workbook.worksheets.getItem("sheetname");
excel_sheet.activate();
Log.Message(Excel.Cells.Item(1, 1));
I am getting error at '
var excel_sheet = Excel.workbook.worksheets.getItem("sheetname");
Error is showing as
TypeError: Cannot read property 'worksheets' of undefined
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My rough guess is that the problem is not on that line but actually on the line where you're opening the file. "Excel.workbook" is undefined which means there was a problem opening the workbook.
What is the value of the excel path?
Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----
Why automate? I do automated testing because there's only so much a human being can do and remain healthy. Sleep is a requirement. So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.
Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @tristaanogre ,
excelpath is as below
var dataFileName="PartDetails.xlsx";
var sCurPath = Sys.OleObject("Scripting.FileSystemObject").GetAbsolutePathName(".");
var excelPath = sCurPath + "\\Data\\" + dataFileName;
I am not getting any error when removed mentioned two lines of code, the value of active sheet is getting retrieved.
I am using those lines to point to specific sheet. is the two lines of code is correct or is there any others methods to make specific sheet using COM object and in javascript?
Thank you,
Anumolu.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try this out,
filePath="C:\Users\sebastian\Desktop\Sample.xlsx";
SheetName="Sheet2";
Excelbook= Excel.Workbooks.Open(filePath);
var xlsheet = Excel.ActiveWorkBook.WorkSheets.Item(SheetName);
I am using the aove and its working
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you everyone for help!
Hi @anumolu9999 , did you find a solution? Did you have a chance to try what SebastianP is suggesting?
Sonya Mihaljova
Community and Education Specialist
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
