Created
December 17, 2024 07:20
-
-
Save YujiFukami/0a8e9dd78d66f4cf665f3493e11ea860 to your computer and use it in GitHub Desktop.
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
| 'InputText ・・・元場所:IkiAddin.ModFile | |
| 'InputTextUTF ・・・元場所:IkiAddin.ModFile | |
| 'InputTextShiftJIS・・・元場所:IkiAddin.ModFile | |
| '宣言セクション※※※※※※※※※※※※※※※※※※※※※※※※※※※ | |
| '----------------------------------- | |
| '元場所:IkiAddin.ModFile.EnumStringEncode | |
| Public Enum EnumStringEncode | |
| vbUTF8 | |
| vbUTF16 | |
| vbShiftJIS | |
| End Enum | |
| '宣言セクション終了※※※※※※※※※※※※※※※※※※※※※※※※※※※ | |
| Public Function InputText(ByRef FilePath As String, _ | |
| ByRef StringEncode As EnumStringEncode, _ | |
| Optional ByRef Delimiter As String = ",", _ | |
| Optional ByRef NewLine As String = vbCrLf) _ | |
| As Variant | |
| 'テキストファイルを読み込んで二次元配列として返す | |
| '文字エンコーディングはUTF-8,UTF-16,ShiftJISから選択 | |
| '20240105 リファクタリング | |
| '参考 | |
| 'https://www.softex-celware.com/post/inputtext | |
| '引数 | |
| 'FilePath ・・・テキストファイルのフルパス | |
| 'StringEncode・・・文字エンコーディング | |
| '[Delimiter] ・・・区切り文字 | |
| '[NewLine] ・・・改行文字(デフォルトでvbCrLf) | |
| ' 他vbLf,vbCrを設定可能 | |
| '処理 | |
| Dim Text As String | |
| Select Case StringEncode | |
| Case vbUTF8 | |
| Text = InputTextUTF(FilePath, "utf-8") | |
| Case vbUTF16 | |
| Text = InputTextUTF(FilePath, "utf-16") | |
| Case vbShiftJIS | |
| Text = InputTextShiftJIS(FilePath) | |
| End Select | |
| '二次元配列に変換 | |
| Dim ListLines As Variant: ListLines = Split(Text, NewLine) '改行で区切る | |
| Dim I As Long | |
| Dim J As Long | |
| Dim N As Long: N = UBound(ListLines, 1) + 1 | |
| Dim M As Long: M = 1 | |
| Dim line As String | |
| For I = 0 To UBound(ListLines, 1) | |
| line = ListLines(I) | |
| If InStr(line, Delimiter) > 0 Then | |
| M = WorksheetFunction.Max(M, UBound(Split(line, Delimiter), 1) + 1) | |
| End If | |
| Next | |
| Dim Output As Variant: ReDim Output(1 To N, 1 To M) | |
| Dim TmpSplit As Variant | |
| For I = 0 To UBound(ListLines, 1) | |
| line = ListLines(I) | |
| If InStr(line, Delimiter) > 0 Then | |
| TmpSplit = Split(line, Delimiter) | |
| For J = 0 To UBound(TmpSplit, 1) | |
| Output(I + 1, J + 1) = TmpSplit(J) | |
| Next | |
| Else | |
| Output(I + 1, 1) = line | |
| End If | |
| Next | |
| '出力 | |
| InputText = Output | |
| End Function | |
| Private Function InputTextUTF(ByRef FilePath As String, _ | |
| ByRef UTF As String) _ | |
| As String | |
| 'テキストファイルを読み込む | |
| 'テキストファイルの文字エンコーディングはUTF-8,UTF-16を対象とする | |
| '20231218 | |
| '引数 | |
| 'FilePath・・・テキストファイルのフルパス | |
| 'UTF ・・・文字エンコーディング | |
| '処理 | |
| Dim stream As Object: Set stream = CreateObject("ADODB.Stream") | |
| stream.Type = 2 '2:テキストファイル | |
| stream.Charset = UTF ' 文字エンコーディングを指定 | |
| stream.Open | |
| stream.LoadFromFile FilePath | |
| Dim Output As String: Output = stream.ReadText | |
| stream.Close | |
| '出力 | |
| InputTextUTF = Output | |
| End Function | |
| Private Function InputTextShiftJIS(ByRef FilePath As String) As String | |
| 'テキストファイルを読み込む | |
| 'テキストファイルの文字エンコーディングはShiftJISを対象とする | |
| '20231218 | |
| '引数 | |
| 'FilePath・・・テキストファイルのフルパス | |
| '処理 | |
| Dim TmpText As String | |
| Dim Output As String: Output = "" | |
| Dim FileNo As Long: FileNo = FreeFile | |
| Open FilePath For Input As #FileNo | |
| Do Until EOF(FileNo) | |
| Line Input #FileNo, TmpText | |
| Output = Output & TmpText & vbCrLf | |
| Loop | |
| Close #FileNo | |
| '出力 | |
| InputTextShiftJIS = Output | |
| End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment