Created
March 4, 2012 11:29
-
-
Save karronoli/1972520 to your computer and use it in GitHub Desktop.
Open or Create excel book by specified sheet name.
This file contains hidden or 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
var file_name = "hello.xls", | |
target_sheet_name = "Hello.t", | |
bin_dir = WScript.ScriptFullName.replace(WScript.ScriptName, ""), | |
excel = new ActiveXObject("Excel.Application"), | |
fso = new ActiveXObject("Scripting.FileSystemObject"); | |
var path = fso.buildPath(bin_dir, file_name); | |
var book = (fso.FileExists(path)) | |
? excel.Workbooks.Open(path) | |
: excel.Workbooks.Add(); | |
var work_sheet = (function(sheet_name) { | |
for (var i = 1, sheet_names = {}; i <= book.Worksheets.Count; ++i) { | |
sheet_names[book.Worksheets.Item(i).Name] = true; | |
} | |
if (sheet_names[sheet_name]) { | |
return book.Worksheets(sheet_name); | |
} else { | |
var new_sheet = book.Worksheets.Add(); | |
new_sheet.Name = sheet_name; | |
return new_sheet; | |
} | |
})(target_sheet_name); | |
// modify something | |
work_sheet.Cells(1, 2) = "Hello"; | |
try { | |
book.SaveAs(path); | |
} catch (e) {WScript.Echo("no updated. " + e.message);} | |
finally { | |
book.Close(false); | |
excel.Quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment