Last active
February 3, 2017 08:16
-
-
Save muzudho/f382a49b48c20dac43517f7bdbe322ff to your computer and use it in GitHub Desktop.
UnityのAnimationControllerのデータをLibraOfficeを使って設定することに挑戦 ref: http://qiita.com/muzudho1/items/b9d16d1f64bf6e9639c0
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
REM ***** BASIC ***** | |
Option Explicit | |
' CreateUpdateRequest シート用 | |
Sub Main | |
' 0 設定、1 入力、 2 出力 | |
Dim oSheet0 as Object ' 設定シート | |
Dim oSheet1_obj as Object ' 入力シート 中間ファイル | |
Dim oSheet2_up as Object ' 出力シート | |
Dim sheetName As String ' シートの名前 | |
Dim sheetName1_obj As String ' シートの名前 | |
Dim sheetName2_up As String ' シートの名前 | |
Dim oDoc1 As Object | |
Dim oDoc2 As Object ' 新しいブック | |
Dim Dummy() ' 使わない引数に | |
Dim folder As String ' 信頼のおけるディレクトリ | |
Dim dataFilePath As String ' アニメーション・コントローラー・データ | |
Dim outputFilePath As String | |
Dim sheetNames (3*8) As String | |
' アクティブ・シートを取得 | |
oSheet0 = ThisComponent.GetCurrentController.ActiveSheet | |
' ( column, row ) is 0 start. | |
folder = oSheet0.getCellByPosition( 5, 11 ).String ' folder. end is "\" | |
dataFilePath = folder & oSheet0.getCellByPosition( 5, 12 ).String ' .ods file | |
outputFilePath = folder & oSheet0.getCellByPosition( 5, 13 ).String ' .ods file | |
oDoc1 = StarDesktop.loadComponentFromURL( ConvertToUrl(dataFilePath) , "_blank", 0, Array()) | |
sheetNames(0) = oSheet0.getCellByPosition( 5, 16 ).String ' parameters | |
sheetNames(1) = oSheet0.getCellByPosition( 5, 16 ).String & "_obj" | |
sheetNames(2) = oSheet0.getCellByPosition( 5, 16 ).String & "_up" | |
sheetNames(3) = oSheet0.getCellByPosition( 5, 18 ).String ' layers | |
sheetNames(4) = oSheet0.getCellByPosition( 5, 18 ).String & "_obj" | |
sheetNames(5) = oSheet0.getCellByPosition( 5, 18 ).String & "_up" | |
sheetNames(6) = oSheet0.getCellByPosition( 5, 20 ).String ' stateMachines | |
sheetNames(7) = oSheet0.getCellByPosition( 5, 20 ).String & "_obj" | |
sheetNames(8) = oSheet0.getCellByPosition( 5, 20 ).String & "_up" | |
sheetNames(9) = oSheet0.getCellByPosition( 5, 22 ).String ' states | |
sheetNames(10) = oSheet0.getCellByPosition( 5, 22 ).String & "_obj" | |
sheetNames(11) = oSheet0.getCellByPosition( 5, 22 ).String & "_up" | |
sheetNames(12) = oSheet0.getCellByPosition( 5, 24 ).String ' transitions | |
sheetNames(13) = oSheet0.getCellByPosition( 5, 24 ).String & "_obj" | |
sheetNames(14) = oSheet0.getCellByPosition( 5, 24 ).String & "_up" | |
sheetNames(15) = oSheet0.getCellByPosition( 5, 26 ).String ' conditions | |
sheetNames(16) = oSheet0.getCellByPosition( 5, 26 ).String & "_obj" | |
sheetNames(17) = oSheet0.getCellByPosition( 5, 26 ).String & "_up" | |
sheetNames(18) = oSheet0.getCellByPosition( 5, 28 ).String ' positions | |
sheetNames(19) = oSheet0.getCellByPosition( 5, 28 ).String & "_obj" | |
sheetNames(20) = oSheet0.getCellByPosition( 5, 28 ).String & "_up" | |
sheetNames(21) = oSheet0.getCellByPosition( 5, 30 ).String ' updateRequest | |
sheetNames(22) = oSheet0.getCellByPosition( 5, 30 ).String & "_obj" | |
sheetNames(23) = oSheet0.getCellByPosition( 5, 30 ).String & "_up" | |
oDoc2 = StarDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, Dummy()) ' ブックを新規作成 | |
'-------------------------------------------------------------------------------- | |
' 最初からあるシートを名前変更 | |
oSheet2_up = oDoc2.Sheets(0) | |
oSheet2_up.Name = sheetNames(2) | |
'作成したシートへの書込 | |
ReadOds(oDoc1, oDoc2, sheetNames(0), sheetNames(1), sheetNames(2) ) ' ODS読込み | |
'-------------------------------------------------------------------------------- | |
' 3つ目以降のシートを作成 | |
Dim index As Integer | |
For index = 3 To 18 Step 3 | |
sheetName = sheetNames(index) | |
sheetName1_obj = sheetNames(index+1) | |
sheetName2_up = sheetNames(index+2) | |
' 更新要求シートを追加 | |
if oDoc2.getSheets().hasByName(sheetName2_up) = false then | |
oDoc2.getSheets().insertNewByName(sheetName2_up,index) | |
end if | |
oSheet2_up = oDoc2.getSheets().getByName(sheetName2_up) | |
'作成したシートへの書込 | |
ReadOds(oDoc1, oDoc2, sheetName, sheetName1_obj, sheetName2_up ) ' CSV読込 | |
Next | |
'-------------------------------------------------------------------------------- | |
'C:test.odsとして保存 | |
oDoc2.storeAsURL(ConvertToUrl( outputFilePath), Dummy()) | |
'ファイルを閉じる | |
oDoc1.dispose | |
oDoc2.dispose | |
End Sub | |
'-------------------------------------------------------------------------------- | |
' ODS読取 | |
' 参考: LibreOffice Calc Basic fun!!! 「既存ドキュメント(ブック)を開く」 http://calibreblo.blogspot.jp/2011/04/blog-post_2121.html | |
' 参考: LibreOffice Calc Basic fun!!! 「文字列関数 (1)」 http://calibreblo.blogspot.jp/2011/05/1_30.html | |
' 参考: White Tiger 「セルの背景色とフォント色を設定する方法」 http://www7b.biglobe.ne.jp/~whitetiger/ex/liboffice015.html | |
' 参考: 列幅の指定に : OSS( Open Source Software )でいこう!! 「 OpenOffice.org / LibreOfficeを使おう編 」 「Macroの杜 (OpenOffice.org/LibreOffice Basic編) Calc No.2 ###【 Previous Page ( Calc No.1 ) 】###」 http://openoffice3.web.fc2.com/OOoBasic_Calc_No2.html#OOoCCR06a | |
' 参考: kojのとりあえず日記2 「OpenOffice.org/LibreOffice Calcでマクロからウィンドウの固定」 http://www.ofug.net/koj/2011/10/21/openoffice-orglibreoffice-calc%E3%81%A7%E3%83%9E%E3%82%AF%E3%83%AD%E3%81%8B%E3%82%89%E3%82%A6%E3%82%A3%E3%83%B3%E3%83%89%E3%82%A6%E3%81%AE%E5%9B%BA%E5%AE%9A/ | |
' 参考: ウィンドウの固定に : ApacheOpenOfficeFORUM 「[Solved] Calc Macro FreezeAtPosition is relative to location」 https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=72745 | |
' 参考: ubuntu & LibreOffice 「シートをアクティブにする LibreOffice Calc Basic [LibreOffice Calc Basic]」 http://j11.blog.so-net.ne.jp/2013-10-22 | |
Sub ReadOds( oDoc1 As Object, oDoc2 As Object, sheetName1_view As String, sheetName1_obj As String, sheetName2_up As String ) | |
' ビューを、そのまま 更新要求シート にコピーする。 | |
Dim oSheet1_view As Object | |
oSheet1_view = oDoc1.getSheets().getByName( sheetName1_view ) | |
Dim oSheet2_up As Object | |
oSheet2_up = oDoc2.getSheets().getByName( sheetName2_up ) | |
Dim column1_view As Integer | |
Dim row1_view As Integer | |
Dim columnLen1_view As Integer | |
Dim rowLen1_view As Integer | |
' ビュー・シートの 1行目の [EOL] を探す | |
column1_view = 0 | |
Do While "[EOL]"<>oSheet1_view.getCellByPosition( column1_view, 0 ).String | |
column1_view = column1_view + 1 | |
Loop | |
columnLen1_view = column1_view - 1 ' EOLはカウントしないので 1 引く | |
' ビュー・シートの 1列目の [EOF] を探す | |
row1_view = 0 | |
Do While "[EOF]"<>oSheet1_view.getCellByPosition( 0, row1_view ).String | |
row1_view = row1_view + 1 | |
Loop | |
rowLen1_view = row1_view - 1 ' EOFはカウントしないので 1 引く | |
Dim column2_up As Integer | |
Dim row2_up As Integer | |
Dim oCell1_view As Object | |
Dim oCell2_up As Object | |
' 更新要求シート( ~_up )の編集 | |
oDoc2.getCurrentController().setActiveSheet( oSheet2_up ) ' 更新要求シートをアクティブにする | |
row2_up = 0 | |
For row1_view=0 To rowLen1_view ' ビュー・シートを垂直方向に走査する | |
column2_up = 0 | |
For column1_view=0 To columnLen1_view ' ビュー・シートを水平方向に走査する | |
oCell1_view = oSheet1_view.getCellByPosition( column1_view, row1_view ) | |
oCell2_up = oSheet2_up.getCellByPosition( column2_up, row2_up ) | |
oCell2_up.String = oCell1_view.String | |
oCell2_up.CellBackColor = oCell1_view.CellBackColor | |
oCell2_up.CharColor = oCell1_view.CharColor | |
column2_up = column2_up + 1 | |
Next | |
row2_up = row2_up + 1 | |
Next | |
oSheet2_up.getCellByPosition( columnLen1_view + 1, 0 ).String = "[EOL]" ' ヘッダー末尾に [EOL] を追加 | |
oSheet2_up.getCellByPosition( 0, row1_view ).String = "[EOF]" ' ファイル末尾に [EOF] を追加 | |
End Sub |
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
'-------------------------------------------------------------------------------- | |
' ODS読取 | |
' 参考: LibreOffice Calc Basic fun!!! 「既存ドキュメント(ブック)を開く」 http://calibreblo.blogspot.jp/2011/04/blog-post_2121.html | |
' 参考: LibreOffice Calc Basic fun!!! 「文字列関数 (1)」 http://calibreblo.blogspot.jp/2011/05/1_30.html | |
' 参考: White Tiger 「セルの背景色とフォント色を設定する方法」 http://www7b.biglobe.ne.jp/~whitetiger/ex/liboffice015.html | |
' 参考: 列幅の指定に : OSS( Open Source Software )でいこう!! 「 OpenOffice.org / LibreOfficeを使おう編 」 「Macroの杜 (OpenOffice.org/LibreOffice Basic編) Calc No.2 ###【 Previous Page ( Calc No.1 ) 】###」 http://openoffice3.web.fc2.com/OOoBasic_Calc_No2.html#OOoCCR06a | |
' 参考: kojのとりあえず日記2 「OpenOffice.org/LibreOffice Calcでマクロからウィンドウの固定」 http://www.ofug.net/koj/2011/10/21/openoffice-orglibreoffice-calc%E3%81%A7%E3%83%9E%E3%82%AF%E3%83%AD%E3%81%8B%E3%82%89%E3%82%A6%E3%82%A3%E3%83%B3%E3%83%89%E3%82%A6%E3%81%AE%E5%9B%BA%E5%AE%9A/ | |
' 参考: ウィンドウの固定に : ApacheOpenOfficeFORUM 「[Solved] Calc Macro FreezeAtPosition is relative to location」 https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=72745 | |
' 参考: ubuntu & LibreOffice 「シートをアクティブにする LibreOffice Calc Basic [LibreOffice Calc Basic]」 http://j11.blog.so-net.ne.jp/2013-10-22 | |
Sub ReadOds( oDoc1 As Object, oDoc2 As Object, sheetName_1view As String, sheetName_1obj As String, sheetName_2up As String ) | |
' ビューを、そのまま 更新要求シート にコピーする。 | |
Dim oSheet_1view As Object | |
oSheet_1view = oDoc1.getSheets().getByName( sheetName_1view ) | |
Dim oSheet_1obj As Object | |
oSheet_1obj = oDoc1.getSheets().getByName( sheetName_1obj ) | |
Dim oSheet_2up As Object | |
oSheet_2up = oDoc2.getSheets().getByName( sheetName_2up ) | |
Dim column_1view As Integer | |
Dim columnLen_1view As Integer | |
Dim row_1view As Integer | |
Dim column_1obj As Integer | |
Dim row_1obj As Integer | |
Dim rowLen_1obj As Integer | |
' ビュー・シートの 1行目の [EOL] を探す | |
column_1view = 0 | |
Do While "[EOL]"<>oSheet_1view.getCellByPosition( column_1view, 0 ).String | |
column_1view = column_1view + 1 | |
Loop | |
columnLen_1view = column_1view - 1 ' EOLはカウントしないので 1 引く | |
' ビュー・シートの 1列目の [EOF] を探す | |
row_1obj = 1 ' [0]行目はヘッダーなので [1]行目から | |
Do While "[EOF]"<>oSheet_1obj.getCellByPosition( 0, row_1obj ).String | |
row_1obj = row_1obj + 1 | |
Loop | |
rowLen_1obj = row_1obj - 1 ' EOFはカウントしないので 1 引く | |
Dim column_2up As Integer | |
Dim row_2up As Integer | |
Dim oCell_2up As Object | |
Const categoryColumn_2up = 0 | |
Const keyColumn_2up = 1 | |
Const nameColumn_2up = 2 | |
Const oldColumn_2up = 3 | |
Const newColumn_2up = 4 | |
Const deleteColumn_2up = 5 | |
Dim oCell_1view As Object | |
Dim phase As Integer | |
' ビュー・シート( ~_view )と、中間シート( ~_obj )を読取り、 | |
' 更新要求シート( ~_up )の書き出し | |
oDoc2.getCurrentController().setActiveSheet( oSheet_2up ) ' 更新要求シートをアクティブにする | |
row_1view = 0 | |
row_2up = 0 | |
For row_1obj = 0 To rowLen_1obj ' 中間シートを垂直方向に走査する | |
Dim name_1obj As String | |
Dim type_1obj As String | |
Dim key_1obj As String | |
Const nameColumn_1obj = 0 | |
Const typeColumn_1obj = 1 | |
Const keyColumn_1obj = 2 | |
name_1obj = oSheet_1obj.getCellByPosition( nameColumn_1obj, row_1obj ).String | |
type_1obj = oSheet_1obj.getCellByPosition( typeColumn_1obj, row_1obj ).String | |
key_1obj = oSheet_1obj.getCellByPosition( keyColumn_1obj, row_1obj ).String | |
If phase = 0 Then | |
' Identify フィールド | |
If "Identify" = key_1obj Then | |
oSheet_2up.getCellByPosition( categoryColumn_2up, row_2up ).String = "IDカテ" | |
oSheet_2up.getCellByPosition( keyColumn_2up, row_2up ).String = "IDキー" | |
oSheet_2up.getCellByPosition( nameColumn_2up, row_2up ).String = "IDネーム" | |
oSheet_2up.getCellByPosition( oldColumn_2up, row_2up ).String = "ID古" | |
oSheet_2up.getCellByPosition( newColumn_2up, row_2up ).String = "ID新" | |
oSheet_2up.getCellByPosition( deleteColumn_2up, row_2up ).String = "ID削" | |
row_2up = row_2up + 1 | |
Else | |
phase = phase + 1 | |
End If | |
ElseIf phase = 2 Then | |
' Presentable フィールド | |
If "Presentable" = key_1obj Then | |
oSheet_2up.getCellByPosition( categoryColumn_2up, row_2up ).String = "名カテ" | |
oSheet_2up.getCellByPosition( keyColumn_2up, row_2up ).String = "名キー" | |
oSheet_2up.getCellByPosition( nameColumn_2up, row_2up ).String = "名ネーム" | |
oSheet_2up.getCellByPosition( oldColumn_2up, row_2up ).String = "名古" | |
oSheet_2up.getCellByPosition( newColumn_2up, row_2up ).String = "名新" | |
oSheet_2up.getCellByPosition( deleteColumn_2up, row_2up ).String = "名削" | |
row_2up = row_2up + 1 | |
Else | |
phase = phase + 1 | |
End If | |
ElseIf phase = 4 Then | |
' それ以外のフィールド | |
If "#Skipped" = key_1obj Then | |
' 出力していないフィールド | |
' 無視する | |
Else | |
' 出力されているフィールド | |
oSheet_2up.getCellByPosition( categoryColumn_2up, row_2up ).String = "項目カテ" | |
oSheet_2up.getCellByPosition( keyColumn_2up, row_2up ).String = "項目キー" | |
oSheet_2up.getCellByPosition( nameColumn_2up, row_2up ).String = "項目ネーム" | |
oSheet_2up.getCellByPosition( oldColumn_2up, row_2up ).String = "項目古" | |
oSheet_2up.getCellByPosition( newColumn_2up, row_2up ).String = "項目新" | |
oSheet_2up.getCellByPosition( deleteColumn_2up, row_2up ).String = "項目削" | |
row_2up = row_2up + 1 | |
End If | |
End If | |
If phase = 1 Then | |
' #Key フィールド | |
oSheet_2up.getCellByPosition( categoryColumn_2up, row_2up ).String = "長IDカテ" | |
oSheet_2up.getCellByPosition( keyColumn_2up, row_2up ).String = "長IDキー" | |
oSheet_2up.getCellByPosition( nameColumn_2up, row_2up ).String = "長IDネーム" | |
oSheet_2up.getCellByPosition( oldColumn_2up, row_2up ).String ="長ID古" | |
oSheet_2up.getCellByPosition( newColumn_2up, row_2up ).String = "長ID新" | |
oSheet_2up.getCellByPosition( deleteColumn_2up, row_2up ).String = "長ID削" | |
row_2up = row_2up + 1 | |
phase = phase + 1 | |
ElseIf phase = 3 Then | |
' #FullPath フィールド | |
oSheet_2up.getCellByPosition( categoryColumn_2up, row_2up ).String = "長名カテ" | |
oSheet_2up.getCellByPosition( keyColumn_2up, row_2up ).String = "長名キー" | |
oSheet_2up.getCellByPosition( nameColumn_2up, row_2up ).String = "長名ネーム" | |
oSheet_2up.getCellByPosition( oldColumn_2up, row_2up ).String ="長名古" | |
oSheet_2up.getCellByPosition( newColumn_2up, row_2up ).String = "長名新" | |
oSheet_2up.getCellByPosition( deleteColumn_2up, row_2up ).String = "長名削" | |
row_2up = row_2up + 1 | |
phase = phase + 1 | |
End If | |
Next | |
oSheet_2up.getCellByPosition( columnLen_1view + 1, 0 ).String = "[EOL]" ' ヘッダー末尾に [EOL] を追加 | |
oSheet_2up.getCellByPosition( 0, row_2up ).String = "[EOF]" ' ファイル末尾に [EOF] を追加 | |
End Sub |
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
Sub VLookup( foreignKey As String, oSheet_target As Object, keyColumn As Integer, valueColumn As Integer ) As String | |
Dim row_foreignSheet As Integer | |
row_foreignSheet = 0 | |
Do While "[EOF]" <> oSheet_target.getCellByPosition( 0, row_foreignSheet ).String | |
If foreignKey = oSheet_target.getCellByPosition( keyColumn, row_foreignSheet ).String Then | |
VLookup = oSheet_target.getCellByPosition( valueColumn, row_foreignSheet ).String | |
Exit Sub | |
End If | |
row_foreignSheet = row_foreignSheet + 1 | |
Loop | |
VLookup = "#NotFound" | |
End Sub |
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
' 複数の同じ列形のシートを、1つのCSVファイルにまとめ、書き出す。 | |
' LibreOffice Calc Basic fun!!! 「テキストファイルへの書き込み」 http://calibreblo.blogspot.jp/2011/04/blog-post_06.html | |
Sub WriteCsv( oDoc2 As Object, outputFilePath2 As String ) | |
Dim oParametersSheet_2upd As Object | |
Dim oLayersSheet_2upd As Object | |
Dim oStatemachinesSheet_2upd As Object | |
Dim oStatesSheet_2upd As Object | |
Dim oTransitionsSheet_2upd As Object | |
Dim oConditionsSheet_2upd As Object | |
Dim oPositionsSheet_2upd As Object | |
oParametersSheet_2upd = oDoc2.getSheets().getByName( "parameters_upd" ) | |
oLayersSheet_2upd = oDoc2.getSheets().getByName( "layers_upd" ) | |
oStatemachinesSheet_2upd = oDoc2.getSheets().getByName( "stateMachines_upd" ) | |
oStatesSheet_2upd = oDoc2.getSheets().getByName( "states_upd" ) | |
oTransitionsSheet_2upd = oDoc2.getSheets().getByName( "transitions_upd" ) | |
oConditionsSheet_2upd = oDoc2.getSheets().getByName( "conditions_upd" ) | |
oPositionsSheet_2upd = oDoc2.getSheets().getByName( "positions_upd" ) | |
Dim column_2upd As Integer | |
Dim columnLen_2upd As Integer | |
column_2upd = 0 | |
Do While "[EOL]" <> oParametersSheet_2upd.getCellByPosition( column_2upd, 0 ).String | |
column_2upd = column_2upd + 1 | |
Loop | |
columnLen_2upd = column_2upd - 1 ' [EOL] は含まないので-1。 | |
Dim contents As String | |
' ヘッダー作成 | |
contents = "#Category,#Fullpath,#FullpathTransition,#FullpathCondition,#FullpathPropertyname,#Name,#Old,#New,#Delete,[EOL]," & Chr$(13) ' 改行 | |
contents = contents & WriteCsv2( oParametersSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oLayersSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oStatemachinesSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oStatesSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oTransitionsSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oConditionsSheet_2upd, columnLen_2upd ) | |
contents = contents & WriteCsv2( oPositionsSheet_2upd, columnLen_2upd ) | |
' ファイルの末端に追加 | |
contents = contents & "[EOF]," & Chr$(13) ' 改行 | |
Dim intFileNo As Integer | |
Dim strData As String | |
'ファイルのオープン | |
intFileNo = FreeFile() | |
Open outputFilePath2 For Output As #intFileNo | |
'レコードの出力 | |
Print #intFileNo, contents | |
'ファイルのクローズ | |
Close #intFileNo | |
End Sub | |
' シート1個分を CSVにする | |
Sub WriteCsv2( oSheet_2upd As Object, columnLen_2upd As Integer ) As String | |
Dim row_2upd As Integer | |
Dim column_2upd As Integer | |
Dim contents As String | |
contents = "" | |
row_2upd = 1 ' [0]行目はヘッダーなので[1]行目から | |
Do While "[EOF]" <> oSheet_2upd.getCellByPosition( 0, row_2upd ).String | |
For column_2upd = 0 To columnLen_2upd | |
contents = contents & EscapeCsv( oSheet_2upd.getCellByPosition( column_2upd, row_2upd ).String ) & "," | |
Next | |
contents = contents & Chr$(13) ' 改行 | |
row_2upd = row_2upd + 1 | |
Loop | |
WriteCsv2 = contents | |
End Sub | |
' カンマや、ダブルクオーテーションを含む文字列を、ダブルクォーテーションで挟む。 | |
' この際、ダブルクォーテーション1つは 2つ に変換する。 | |
Sub EscapeCsv(source As String) As String | |
Dim isEscape As Boolean | |
isEscape = false ' エスケープが必要なら真 | |
Dim str As String | |
str = "" | |
Dim caret As Integer | |
For caret = 1 To Len(source) ' 文字列は 1 スタート | |
If ","= Mid( source, caret, 1 ) Then ' カンマが含まれていたので、エスケープが必要になった | |
isEscape = true | |
str = str & Mid( source, caret, 1 ) | |
ElseIf """" = Mid( source, caret, 1 ) Then ' ダブルクォーテーションが含まれていたので、エスケープが必要になった | |
isEscape = true | |
str = str & """""" ' ダブルクォーテーションを、1つの代わりに2つ追加 | |
Else | |
str = str & Mid( source, caret, 1 ) | |
End If | |
Next | |
If isEscape Then | |
str = """" & str & """" ' ダブルクォーテーションで挟む | |
End If | |
EscapeCsv = str | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment