環境
- mac OS 10.15.4
- Excel for mac 16.35(20030802)
実現したいこと
文字列をURIエンコードしたいです。
試したこと
1. JavaScript#encodeURI()を使う
vb
1 2Function UrlEncode(mySource As Variant) As String 3 Dim objSC As Object 4 Set objSC = CreateObject("ScriptControl") 5 objSC.Language = "JScript" 6 UrlEncode = objSC.CodeObject.encodeURIComponent(mySource) 7 Set objSC = Nothing 8End Function 9 10Sub test 11 MsgBox UrlEncode("テスト") 12End
** 結果 **
「ActiveXコンポーネントはオブジェクトを作成できません。」
とエラーが出ます。
2. rubyを使う
vb
1Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr 2Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long 3Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long 4Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr 5 6Function execShell(command As String, Optional ByRef exitCode As Long) As String 7 Dim file As LongPtr 8 file = popen(command, "r") 9 10 If file = 0 Then 11 Exit Function 12 End If 13 14 While feof(file) = 0 15 Dim chunk As String 16 Dim read As Long 17 chunk = Space(50) 18 read = fread(chunk, 1, Len(chunk) - 1, file) 19 If read > 0 Then 20 chunk = Left$(chunk, read) 21 execShell = execShell & chunk 22 End If 23 Wend 24 25 exitCode = pclose(file) 26End Function 27 28Function UrlEncode(mySource As Variant) As String 29 Dim command As String 30 Dim result As String 31 32 command = "do shell script ""ruby -r cgi -e 'puts CGI.escape(""" & mySource & """);'""" 33 result = execShell(command, exitCode) 34 UrlEncode = result 35End Function 36 37Sub test 38 MsgBox UrlEncode("テスト") 39End
** 結果 **
何もリターンされません。(空文字が返ります)
ターミナルでruby -r cgi -e 'puts CGI.escape("テスト");'
を実行すると「%E3%83%86%E3%82%B9%E3%83%88」と返ります。
3. nkfを使う
vb
1 2Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr 3Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long 4Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long 5Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr 6 7Function execShell(command As String, Optional ByRef exitCode As Long) As String 8 Dim file As LongPtr 9 file = popen(command, "r") 10 11 If file = 0 Then 12 Exit Function 13 End If 14 15 While feof(file) = 0 16 Dim chunk As String 17 Dim read As Long 18 chunk = Space(50) 19 read = fread(chunk, 1, Len(chunk) - 1, file) 20 If read > 0 Then 21 chunk = Left$(chunk, read) 22 execShell = execShell & chunk 23 End If 24 Wend 25 26 exitCode = pclose(file) 27End Function 28 29Function UrlEncode(mySource As Variant) As String 30 Dim command As String 31 Dim result As String 32 33 command = "do shell script ""echo '" & mySource & "' | nkf -WwMQ | sed 's/=$//g' | tr = % | tr -d '\n'""" 34 result = execShell(command, exitCode) 35 UrlEncode = result 36End Function 37 38Sub test 39 MsgBox UrlEncode("テスト") 40End
** 結果 **
何もリターンされません。(空文字が返ります)
ターミナルでecho 'テスト' | nkf -WwMQ | sed 's/=$//g' | tr = % | tr -d '\n'
を実行すると「%E3%83%86%E3%82%B9%E3%83%88」と返ります。
4. AppleScript経由でruby / nkfを使う
vb
1Function UrlEncode(mySource As Variant) As String 2 Dim result As String 3 4 result = AppleScriptTask("urlencode.applescript", "urlEncode", mySource) 5 UrlEncode = result 6End Function 7 8Sub test 9 MsgBox UrlEncode("テスト") 10End
applescript
1# ~/Library/Application Scripts/com.microsoft.Excel/urlencode.applescript 2on urlEncode(inData) 3 set scpt to "ruby -r cgi -e 'puts CGI.escape(""" & quote & inData & quote & """);'" 4 return (do shell script scpt) as string 5end urlEncode
** 結果 **
「プロシージャの呼び出し、または引数が無効です。」
とエラーが出ます。
(上はrubyですがnkfのコードも同じエラーになります)
何か良い方法がありましたら、ご教示いただけますと幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/01 14:21