//------------------------------------------------
// Excelè
//------------------------------------------------
var xlUp		= -4162;
var xlDown		= -4121;
var xlToLeft	= -4159;
var xlToRight	= -4161;
 
var xlWorkbookNormal		= -4143;
var xlNone 					= -1412;
 
//-- \èt¯ÎÛ --
var xlPasteAll				= -4104;		//·×Ä
var xlPasteFormulas			= -4123;		//®
var xlPasteValues			= -4163;		//l
var xlPasteFormats			= -4122;		//®
var xlPasteComments			= -4144;		//Rg
var xlDataValidation		= 6;			//üÍK¥
var xlPasteAllExceptBorders = 7;			//rüð­·×Ä
var xlPasteColumnWidths 	= 8;			//ñ
//------------------------------------------------
var fso = new ActiveXObject("Scripting.FileSystemObject");


WScript.Quit(main( WScript.Arguments.Count(), WScript.Arguments ));
function main( argc, argv ) 
{
	// WScriptÈçCScriptÅN®µ¼µ
	if ( WScript.FullName.substr(WScript.FullName.length - 11, 11).toUpperCase() == "WSCRIPT.EXE" ) {
		var wshShell = new ActiveXObject("WScript.Shell");
		var cmd = "cmd.exe /c cscript.exe //NOLOGO \"" + WScript.ScriptFullName + "\"";
		for ( var i = 0; i < argc; i++ ) {
			cmd += " \"" + argv(i) + "\"";
		}
		cmd += " & pause"
		return wshShell.Run(cmd, 1, true);
	}
	
	return ExecScript();
}

function ExecScript()
{
	Sample_ReadExcel();
	Sample_CreateExcel();
}

//----------------------------
// î{eXg
//----------------------------
function Sample_ReadExcel()
{
	var file = WScript.ScriptFullName.replace(WScript.ScriptName, "test1.xls");
	
	// ExcelIuWFNgÌì¬
	var objExcel = new ActiveXObject("Excel.Application");
	objExcel.Visible = true;			// EBhE\¦Ýè
	objExcel.DisplayAlerts = false;		// A[gÝè
	
	var objBook = objExcel.Workbooks.Open(file);
	var objSheet = objBook.WorkSheets(1);
	
	LOG( "Text" );
	LOG( "  - Cells(7,4): " + objSheet.Cells(7,4).Text );
	LOG( "  - Range(D7)  : " + objSheet.Range("D7").Text );
	
	LOG( "Range <-> Cells" );
	LOG( "  - Range(D7) => Cells(" + objSheet.Range("D7").Row + "," + objSheet.Range("D7").Column + ")" );
	LOG( "  - Cells(7,4) => Range(" + objSheet.Cells(7,4).Address + ")" );
	
	objBook.Close();
	
	objExcel.Quit();
	objExcel = null;
}

function Sample_CreateExcel()
{
	var file = WScript.ScriptFullName.replace(WScript.ScriptName, "test_created.xlsx");
	
	// ExcelIuWFNgÌì¬
	var objExcel = new ActiveXObject("Excel.Application");
	objExcel.Visible = true;			// EBhE\¦Ýè
	objExcel.DisplayAlerts = false;		// A[gÝè
	
	var objOutBook = objExcel.Workbooks.Add();
	var objOutSheet = objOutBook.WorkSheets(1);
	
	
	objOutSheet.Range("A1").Value = "Ù°Ù°";
	objOutSheet.Cells(2, 2).Formula = "=1+1";
	
	
	// oÍt@CÌÛ¶
	LOG("save: " + file);
	objOutBook.SaveAs(file);
	
	objOutBook.Close();
	
	objExcel.Quit();
	objExcel = null;
}

function LOG( str )
{
	WScript.Echo(str);
}
