-
Notifications
You must be signed in to change notification settings - Fork 0
/
xls2csv.vbs
executable file
·173 lines (168 loc) · 5.05 KB
/
xls2csv.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'
' Run as "cscript //NoLogo xls2csv.vbs [-sn SheetName|-si SheetIndex] [-i input.xls] [-o output.csv]"
'
'--------------------------------------------------
' 共通処理
'
Option Explicit
Function include(filename)
ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile(filename).ReadAll()
End Function
include("mytoolkit.vbs")
'--------------------------------------------------
' 大局変数
'
Dim objEndSw : Set objEndSw = new MySwitch
Dim objMisc : Set objMisc = new MyMisc
' User coding start
Dim objOpt : Set objOpt = new MyOption
Dim objStdio : Set objStdio = new MyStdio
Dim strInFile : strInFile = ""
Dim strOutFile : strOutFile = ""
Dim objDir : Set objDir = new MyDir
Dim objExcel : Set objExcel = new MyExcel
Dim objFilename : Set objFilename = new MyString
Dim objFsOpe : Set objFsOpe = new MyFsOpe
Dim strSheetName : strSheetName = ""
Dim intSheetIndex : intSheetIndex = 1
Dim strArrayFilesEtc
Dim intArrayFilesEtcIndex : intArrayFilesEtcIndex = 0
' User coding end
'--------------------------------------------------
' 処理開始
'
sub_open ' オープン処理
sub_initialize ' 開始処理
While objEndSw.isOff
sub_main ' 主処理
Wend
sub_terminate ' 終了処理
sub_close ' クローズ処理
objMisc.exitProg(0)
'--------------------------------------------------
' オープン処理
'
Sub sub_open
' User coding start
objOpt.initialize("-h=n,--help=n,-sn=y,-si=y,-i=y,-o=y")
If objOpt.isSpecified("-h") or objOpt.isSpecified("--help") Then
objStdio.writeLine "Usage : cscript //NoLogo xls2csv.vbs [-sn SheetName|-si SheetIndex] [-i input.xls] [-o output.csv]"
objStdio.writeLine "Convert a sheet of excel to csv."
objStdio.writeLine ""
objStdio.writeLine " -sn SheetName SheetName must be specified in STRING."
objStdio.writeLine " -si SheetIndex SheetIndex must be specified in INTEGER."
objStdio.writeLine " If SheetName and SheetIndex are not specified, it is assumed that ""-si 1"" is specified."
objStdio.writeLine " -i input.xls specify input excel file."
objStdio.writeLine " -o output.csv specify output csv file."
objStdio.writeLine " If both of input.xls and output.csv are not specified, all *.xls and *.xlsx in current directory will be converted into *.csv."
objStdio.writeLine " If input.xls is specified and output.csv is not specified, input.xls will be converted into stdout."
objEndSw.turnOn
End If
strArrayFilesEtc = objOpt.getArrayNonOptions
If objEndSw.isOff Then
If objOpt.isSpecified("-sn") Then
strSheetName = objOpt.getValue("-sn")
End If
If objOpt.isSpecified("-si") Then
intSheetIndex = CInt(objOpt.getValue("-si"))
End If
If objOpt.isSpecified("-i") Then
strInFile = objOpt.getValue("-i")
End If
If objOpt.isSpecified("-o") Then
strOutFile = objOpt.getValue("-o")
End If
End If
' User coding end
End Sub
'--------------------------------------------------
' 開始処理
'
Sub sub_initialize
' User coding start
If objEndSw.isOff Then
If strInFile = "" Then
objDir.setDir(".")
objFilename.setValue objDir.getFirstFilename
If objFilename.getValue = "" Then
objEndSw.turnOn
End If
End If
End If
' User coding end
End Sub
'--------------------------------------------------
' 主処理
'
Sub sub_main
' User coding start
Dim strCsvFilename
If strInFile = "" Then
If objFilename.isMatch("\.[Xx][Ll][Ss][XxMm]*$") Then
' On Error Resume Next
objExcel.open objFilename.getValue
strCsvFilename = objFilename.getReplace("\.[Xx][Ll][Ss][XxMm]*$", ".csv")
If strSheetName = "" Then
objExcel.saveAsCsv intSheetIndex, strCsvFilename
Else
objExcel.saveAsCsv strSheetName, strCsvFilename
End If
objExcel.close
' On Error Goto 0
objStdio.writeLine objFilename.getValue & " -> " & strCsvFilename
End If
objFilename.setValue objDir.getNextFilename
If objFilename.getValue = "" Then
objEndSw.turnOn
End If
Else
' On Error Resume Next
objExcel.open strInFile
If strOutFile = "" Then
strCsvFilename = objFsOpe.getTempFileName
Else
strCsvFilename = strOutFile
End If
If strSheetName = "" Then
objExcel.saveAsCsv intSheetIndex, strCsvFilename
Else
objExcel.saveAsCsv strSheetName, strCsvFilename
End If
objExcel.close
' On Error Goto 0
If strOutFile = "" Then
Dim objTemp : Set objTemp = new MyFso
Dim strRec
objTemp.openInput strCsvFilename
strRec = objTemp.readLine
While objTemp.isReadSuccess
objStdio.writeLine strRec
strRec = objTemp.readLine
Wend
objTemp.close
objFsOpe.deleteFile strCsvFilename
Set objTemp = Nothing
End If
objEndSw.turnOn
End If
' User coding end
End Sub
'--------------------------------------------------
' 終了処理
'
Sub sub_terminate
' User coding start
' User coding end
End Sub
'--------------------------------------------------
' クローズ処理
'
Sub sub_close
' User coding start
' User coding end
End Sub
'--------------------------------------------------
' その他の処理
'
' User coding start
' User coding end