LoginSignup
3
2

More than 5 years have passed since last update.

WSHとExcelでふりがなの生成

Last updated at Posted at 2018-04-27

Excelの機能を使ってふりがなを生成します。
完全ではありませんが、自分でやるより全然楽です。

cscript //nologo genPhonetic.js [読みを付けたい感じのリスト]

みたいな感じで実行します。

genPhonetic.js
// magic spell for WSH ///////////////////////////////////////////////////////
if(typeof console==="undefined")  console={log: function(s){WScript.Echo(s)}};
if(typeof process==="undefined"){ process={exit:function(e){WScript.Quit(e)}};
var v=process.argv=[];v.push(WScript.FullName);v.push(WScript.ScriptFullName);
var n=WScript.Arguments.Unnamed;for(var i=0;i<n.length;i++)v.push(n.item(i))};
//////////////////////////////////////////////////////////////////////////////
//
// ExcelのApplication.GetPhonetic メソッドを使ってふりがなを生成する
//
// Application.GetPhonetic メソッド (Excel)
// https://msdn.microsoft.com/ja-jp/vba/excel-vba/articles/application-getphonetic-method-excel

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ExcelApp = new ActiveXObject( "Excel.Application" );

try {
    var book = ExcelApp.Workbooks.Add();
    var sheet = book.Worksheets( 1 );

    var argv = process.argv.slice(2);
    if( argv.length > 0 ) {
        for( var i=0; i<argv.length; i++ ) {
            main( argv[i] );
        }
    } else {
        main( WScript.StdIn );  // フィルター動作
    }
    function main( file ) {     // 主処理
        var result=[];
        if( typeof file==="string" ) {
            var f = fso.OpenTextFile( file, 1); // 1:ForReading
        } else {
            f = file;
        }
        while ( !f.AtEndOfStream) {
            var phrase = f.ReadLine();
            var yomi = ExcelApp.GetPhonetic(phrase);
            result.push( [ phrase, yomi ].join("\t") );
        }
        f.Close();
        console.log( result.join("\n") );
    }
}
finally {   // 後処理
    ExcelApp.Quit();
    ExcelApp = null;
}   // エラー時もExcelを終わらせるため、try~catch~finallyを使う。

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2