Created
April 10, 2013 15:56
-
-
Save 1dolinski/5355910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'win32ole' | |
#check if there is an excel application open | |
#yes | |
#check through the open excel applications, see if one of the tab names is the same as the one specified in the parameter | |
#yes | |
#set that workbook and that sheet equal to the variables @wb and @ws1 | |
#no | |
#create a new workbook, delete all of the pages except "Sheet1", rename "Sheet1" to "openxls" | |
#no | |
#create a new instance of excel | |
#create a new workbook, delete all pages except "Sheet1", rename "Sheet1" to "openxls" | |
def openxls(templatename=nil) | |
excel = WIN32OLE::connect('excel.Application') rescue WIN32OLE::new('excel.Application') | |
excel.Visible = true | |
setsheet(excel, templatename) | |
end | |
def setsheet(excel, templatename=nil) | |
if templatename != nil #this checks to see if the user specified an excel template, otherwise it will create a new one | |
excel.Workbooks.each do |wb| # loop through all excel workbooks (i.e. open documents) | |
wb.Worksheets.each do |ws| # loop through each workbook's worksheets | |
if ws.name == templatename | |
@wb = wb | |
@ws1 = ws | |
return | |
end | |
end | |
end | |
puts "Could not find the sheet #{templatename}, creating a new one called 'openxls'." | |
newwb(excel) | |
else | |
newwb(excel) | |
puts "Opened instance of excel, sheet named openxls" | |
end | |
end | |
def newwb(excel) | |
@wb = excel.Workbooks.Add | |
@ws1 = @wb.Worksheets('Sheet1') | |
@wb.Worksheets("Sheet2").Delete | |
@wb.Worksheets("Sheet3").Delete | |
@ws1.name = "openxls" | |
end | |
openxls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment