views:

190

answers:

3

I'm attempting to convert some VBScript to javascript, but I doubt it's possible because it seems to be specific to MS apps and code. I'd like help with either of the two possible outcomes: a) actually converting the code to javascript, or b) demonstrating that converting it to javascript is currently not possible.

The specific VBScript statements that I consider too MS-specific are:

set oExcel = CreateObject("Excel.Application")
set oBook = oExcel.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.RefreshDocument()
oExcel.Visible = True
oExcel.UserControl = True

Has anyone interfaced javascript and Excel well enough for a solution to exist?

+1  A: 

You can do this only with JScript, not JavaScript- this will show you how. This may be fine if you are using only IE.

RichardOD
+3  A: 

Sure like this:-

 var excel = new ActiveXObject("Excel.Application")
 var book = excel.Workbooks.Add()

 //The line below doesn't work in Excel 2007
 // book.HTMLProject.HTMLProjectItems["Sheet1"].Text = sHtml

 var sheet =  book.Sheets("Sheet1")
 sheet.Range("A2").Value = "Hello World"
 excel.Visible = true
 excel.UserControl = true
AnthonyWJones
This code worked like a charm! After fixing a minor typo from the non-quoted code (OuterHTML != outerHTML in javascript), everything is just as expected. Thank you!
taserian
A: 

I think all you need to do is change 'set' to 'var'; 'True' to 'true' and add semicolons to the ends of the lines.

Sounds like you're using WSH... If so, the Excel interface is accessible via OLE Automation and therefore usable with any compatible scripting language.

steamer25