・同じ処理を繰り返す場合、エクセルを使って短時間にコードを生成できるようになります
・半角↔︎全角変換はエクセルの関数を使うことで効果的にコーディングができます
PowerQueryのコード生成にエクセルを使う事例として、半角↔︎全角変換のコード生成を例に説明します。
サンプルとして、取り込むデータは次のような、産地と品名がリストになっているエクセルファイルを準備しました。
コードの繰り返し要素を特定する
全角数字を半角数字に変換するコードは次のようなものになります。
このコード例は、数字だけを変換していますが、全角から半角に変換する必要があるのは、数字だけでなく、カナや記号などもあります。
(TargetText as nullable text)=> let num0 = Replacer.ReplaceText(TargetText,"0","0"), num1 = Replacer.ReplaceText(num0,"1","1"), num2 = Replacer.ReplaceText(num1,"2","2"), num3 = Replacer.ReplaceText(num2,"3","3"), num4 = Replacer.ReplaceText(num3,"4","4"), num5 = Replacer.ReplaceText(num4,"5","5"), num6 = Replacer.ReplaceText(num5,"6","6"), num7 = Replacer.ReplaceText(num6,"7","7"), num8 = Replacer.ReplaceText(num7,"8","8"), num9 = Replacer.ReplaceText(num8,"9","9"), result = num9 in result
ここで、繰り返し要素を特定するとこの部分が、共通しててい繰り返されていることがわかります。
"X"の部分が変化しており、それ以外は同じ記述になっています。
numX = Replacer.ReplaceText(numX,"X","X"),
まず、値が変化する "X" を含む文字の前後を分けるようにセルに記述していきます。
次に、CONCAT()を使って、B〜I列の文字を連結させていきます。
次に、F列の全角文字を直接入力するのではなく、JISコードで入力します。
CHAR()を使い()には、JISコード表の対応する値を入力します。加工しやすいようにJISコードをJ列に入力します。このとき、全角の "0" は9008で表せます。
そして、H列には半角文字を入力しますが、エクセルの関数には、全角を半角に変換するASC()関数があるため、F列の値を半角に変換するようにASC()を記述します。
入力した関数をコピーして量産する!
B、DおよびJ列の数字を増加させるようにして行をコピーするため、A〜J列選択した状態とした後、選択セルの右下を下にドラッグしてコピーしていきます。
次のようにB、D、J列の数字が増加していくようにコピーされると思います。
J列が9590になるまでコピーすると数字、英字、記号、カナの全てが表示されます。
次に途中に該当する文字がないJISコードがあるため、その行を削除していきます。
最後に、濁点、破裂音の行を、上の方の行になるよう移動させます。これは、半角の濁点や破裂音は、濁点や破裂音がない文字と濁点(゙)や破裂音(゚)の文字の2文字で構成されているため、濁点や破裂音を1文字で表現する全角に変換しようとすると、濁点や破裂音がない文字部分だけを先に全角に変換してしまい、濁点や破裂音が余ってしまうためです。
コードの完成
完成した全角を半角にするコードサンプルを次に示します。
(TargetText as nullable text)=> let char0 = Replacer.ReplaceText(TargetText,"0","0"), char1 = Replacer.ReplaceText( char0,"1","1"), char2 = Replacer.ReplaceText( char1,"2","2"), char3 = Replacer.ReplaceText( char2,"3","3"), char4 = Replacer.ReplaceText( char3,"4","4"), char5 = Replacer.ReplaceText( char4,"5","5"), char6 = Replacer.ReplaceText( char5,"6","6"), char7 = Replacer.ReplaceText( char6,"7","7"), char8 = Replacer.ReplaceText( char7,"8","8"), char9 = Replacer.ReplaceText( char8,"9","9"), char10 = Replacer.ReplaceText( char9,"Y","Y"), char11 = Replacer.ReplaceText( char10,"Z","Z"), char12 = Replacer.ReplaceText( char11,"[","["), char13 = Replacer.ReplaceText( char12,"¥","¥"), char14 = Replacer.ReplaceText( char13,"]","]"), char15 = Replacer.ReplaceText( char14,"^","^"), char16 = Replacer.ReplaceText( char15,"_","_"), char17 = Replacer.ReplaceText( char16,"A","A"), char18 = Replacer.ReplaceText( char17,"B","B"), char19 = Replacer.ReplaceText( char18,"C","C"), char20 = Replacer.ReplaceText( char19,"D","D"), char21 = Replacer.ReplaceText( char20,"E","E"), char22 = Replacer.ReplaceText( char21,"F","F"), char23 = Replacer.ReplaceText( char22,"G","G"), char24 = Replacer.ReplaceText( char23,"H","H"), char25 = Replacer.ReplaceText( char24,"I","I"), char26 = Replacer.ReplaceText( char25,"J","J"), char27 = Replacer.ReplaceText( char26,"K","K"), char28 = Replacer.ReplaceText( char27,"L","L"), char29 = Replacer.ReplaceText( char28,"M","M"), char30 = Replacer.ReplaceText( char29,"N","N"), char31 = Replacer.ReplaceText( char30,"O","O"), char32 = Replacer.ReplaceText( char31,"P","P"), char33 = Replacer.ReplaceText( char32,"Q","Q"), char34 = Replacer.ReplaceText( char33,"R","R"), char35 = Replacer.ReplaceText( char34,"S","S"), char36 = Replacer.ReplaceText( char35,"T","T"), char37 = Replacer.ReplaceText( char36,"U","U"), char38 = Replacer.ReplaceText( char37,"V","V"), char39 = Replacer.ReplaceText( char38,"W","W"), char40 = Replacer.ReplaceText( char39,"X","X"), char41 = Replacer.ReplaceText( char40,"Y","Y"), char42 = Replacer.ReplaceText( char41,"Z","Z"), char43 = Replacer.ReplaceText( char42,"z","z"), char44 = Replacer.ReplaceText( char43,"{","{"), char45 = Replacer.ReplaceText( char44,"|","|"), char46 = Replacer.ReplaceText( char45,"}","}"), char47 = Replacer.ReplaceText( char46,"~","~"), char48 = Replacer.ReplaceText( char47,"\","\"), char49 = Replacer.ReplaceText( char48,"a","a"), char50 = Replacer.ReplaceText( char49,"b","b"), char51 = Replacer.ReplaceText( char50,"c","c"), char52 = Replacer.ReplaceText( char51,"d","d"), char53 = Replacer.ReplaceText( char52,"e","e"), char54 = Replacer.ReplaceText( char53,"f","f"), char55 = Replacer.ReplaceText( char54,"g","g"), char56 = Replacer.ReplaceText( char55,"h","h"), char57 = Replacer.ReplaceText( char56,"i","i"), char58 = Replacer.ReplaceText( char57,"j","j"), char59 = Replacer.ReplaceText( char58,"k","k"), char60 = Replacer.ReplaceText( char59,"l","l"), char61 = Replacer.ReplaceText( char60,"m","m"), char62 = Replacer.ReplaceText( char61,"n","n"), char63 = Replacer.ReplaceText( char62,"o","o"), char64 = Replacer.ReplaceText( char63,"p","p"), char65 = Replacer.ReplaceText( char64,"q","q"), char66 = Replacer.ReplaceText( char65,"r","r"), char67 = Replacer.ReplaceText( char66,"s","s"), char68 = Replacer.ReplaceText( char67,"t","t"), char69 = Replacer.ReplaceText( char68,"u","u"), char70 = Replacer.ReplaceText( char69,"v","v"), char71 = Replacer.ReplaceText( char70,"w","w"), char72 = Replacer.ReplaceText( char71,"x","x"), char73 = Replacer.ReplaceText( char72,"y","y"), char74 = Replacer.ReplaceText( char73,"z","z"), char75 = Replacer.ReplaceText( char74,"ガ","ガ"), char76 = Replacer.ReplaceText( char75,"ギ","ギ"), char77 = Replacer.ReplaceText( char76,"グ","グ"), char78 = Replacer.ReplaceText( char77,"ゲ","ゲ"), char79 = Replacer.ReplaceText( char78,"ゴ","ゴ"), char80 = Replacer.ReplaceText( char79,"ザ","ザ"), char81 = Replacer.ReplaceText( char80,"ジ","ジ"), char82 = Replacer.ReplaceText( char81,"ズ","ズ"), char83 = Replacer.ReplaceText( char82,"ゼ","ゼ"), char84 = Replacer.ReplaceText( char83,"ゾ","ゾ"), char85 = Replacer.ReplaceText( char84,"ダ","ダ"), char86 = Replacer.ReplaceText( char85,"ヂ","ヂ"), char87 = Replacer.ReplaceText( char86,"ヅ","ヅ"), char88 = Replacer.ReplaceText( char87,"デ","デ"), char89 = Replacer.ReplaceText( char88,"ド","ド"), char90 = Replacer.ReplaceText( char89,"バ","バ"), char91 = Replacer.ReplaceText( char90,"パ","パ"), char92 = Replacer.ReplaceText( char91,"ビ","ビ"), char93 = Replacer.ReplaceText( char92,"ピ","ピ"), char94 = Replacer.ReplaceText( char93,"ブ","ブ"), char95 = Replacer.ReplaceText( char94,"プ","プ"), char96 = Replacer.ReplaceText( char95,"ベ","ベ"), char97 = Replacer.ReplaceText( char96,"ペ","ペ"), char98 = Replacer.ReplaceText( char97,"ボ","ボ"), char99 = Replacer.ReplaceText( char98,"ポ","ポ"), char100 = Replacer.ReplaceText( char99,"ヴ","ヴ"), char101 = Replacer.ReplaceText( char100,"ァ","ァ"), char102 = Replacer.ReplaceText( char101,"ア","ア"), char103 = Replacer.ReplaceText( char102,"ィ","ィ"), char104 = Replacer.ReplaceText( char103,"イ","イ"), char105 = Replacer.ReplaceText( char104,"ゥ","ゥ"), char106 = Replacer.ReplaceText( char105,"ウ","ウ"), char107 = Replacer.ReplaceText( char106,"ェ","ェ"), char108 = Replacer.ReplaceText( char107,"エ","エ"), char109 = Replacer.ReplaceText( char108,"ォ","ォ"), char110 = Replacer.ReplaceText( char109,"オ","オ"), char111 = Replacer.ReplaceText( char110,"カ","カ"), char112 = Replacer.ReplaceText( char111,"キ","キ"), char113 = Replacer.ReplaceText( char112,"ク","ク"), char114 = Replacer.ReplaceText( char113,"ケ","ケ"), char115 = Replacer.ReplaceText( char114,"コ","コ"), char116 = Replacer.ReplaceText( char115,"サ","サ"), char117 = Replacer.ReplaceText( char116,"シ","シ"), char118 = Replacer.ReplaceText( char117,"ス","ス"), char119 = Replacer.ReplaceText( char118,"セ","セ"), char120 = Replacer.ReplaceText( char119,"ソ","ソ"), char121 = Replacer.ReplaceText( char120,"タ","タ"), char122 = Replacer.ReplaceText( char121,"チ","チ"), char123 = Replacer.ReplaceText( char122,"ッ","ッ"), char124 = Replacer.ReplaceText( char123,"ツ","ツ"), char125 = Replacer.ReplaceText( char124,"テ","テ"), char126 = Replacer.ReplaceText( char125,"ト","ト"), char127 = Replacer.ReplaceText( char126,"ナ","ナ"), char128 = Replacer.ReplaceText( char127,"ニ","ニ"), char129 = Replacer.ReplaceText( char128,"ヌ","ヌ"), char130 = Replacer.ReplaceText( char129,"ネ","ネ"), char131 = Replacer.ReplaceText( char130,"ノ","ノ"), char132 = Replacer.ReplaceText( char131,"ハ","ハ"), char133 = Replacer.ReplaceText( char132,"ヒ","ヒ"), char134 = Replacer.ReplaceText( char133,"フ","フ"), char135 = Replacer.ReplaceText( char134,"ヘ","ヘ"), char136 = Replacer.ReplaceText( char135,"ホ","ホ"), char137 = Replacer.ReplaceText( char136,"マ","マ"), char138 = Replacer.ReplaceText( char137,"ミ","ミ"), char139 = Replacer.ReplaceText( char138,"ム","ム"), char140 = Replacer.ReplaceText( char139,"メ","メ"), char141 = Replacer.ReplaceText( char140,"モ","モ"), char142 = Replacer.ReplaceText( char141,"ャ","ャ"), char143 = Replacer.ReplaceText( char142,"ヤ","ヤ"), char144 = Replacer.ReplaceText( char143,"ュ","ュ"), char145 = Replacer.ReplaceText( char144,"ユ","ユ"), char146 = Replacer.ReplaceText( char145,"ョ","ョ"), char147 = Replacer.ReplaceText( char146,"ヨ","ヨ"), char148 = Replacer.ReplaceText( char147,"ラ","ラ"), char149 = Replacer.ReplaceText( char148,"リ","リ"), char150 = Replacer.ReplaceText( char149,"ル","ル"), char151 = Replacer.ReplaceText( char150,"レ","レ"), char152 = Replacer.ReplaceText( char151,"ロ","ロ"), char153 = Replacer.ReplaceText( char152,"ヮ","ヮ"), char154 = Replacer.ReplaceText( char153,"ワ","ワ"), char155 = Replacer.ReplaceText( char154,"ヰ","ヰ"), char156 = Replacer.ReplaceText( char155,"ヱ","ヱ"), char157 = Replacer.ReplaceText( char156,"ヲ","ヲ"), char158 = Replacer.ReplaceText( char157,"ン","ン"), char159 = Replacer.ReplaceText( char158,"ヵ","ヵ"), char160 = Replacer.ReplaceText( char159,"ヶ","ヶ") result = char160 in result
また、半角を全角に変換するコードサンプルを次に示します。
(TargetText as nullable text)=> let char0 = Replacer.ReplaceText(TargetText,"0","0"), char1 = Replacer.ReplaceText( char0,"1","1"), char2 = Replacer.ReplaceText( char1,"2","2"), char3 = Replacer.ReplaceText( char2,"3","3"), char4 = Replacer.ReplaceText( char3,"4","4"), char5 = Replacer.ReplaceText( char4,"5","5"), char6 = Replacer.ReplaceText( char5,"6","6"), char7 = Replacer.ReplaceText( char6,"7","7"), char8 = Replacer.ReplaceText( char7,"8","8"), char9 = Replacer.ReplaceText( char8,"9","9"), char10 = Replacer.ReplaceText( char9,"Y","Y"), char11 = Replacer.ReplaceText( char10,"Z","Z"), char12 = Replacer.ReplaceText( char11,"[","["), char13 = Replacer.ReplaceText( char12,"¥","¥"), char14 = Replacer.ReplaceText( char13,"]","]"), char15 = Replacer.ReplaceText( char14,"^","^"), char16 = Replacer.ReplaceText( char15,"_","_"), char17 = Replacer.ReplaceText( char16,"A","A"), char18 = Replacer.ReplaceText( char17,"B","B"), char19 = Replacer.ReplaceText( char18,"C","C"), char20 = Replacer.ReplaceText( char19,"D","D"), char21 = Replacer.ReplaceText( char20,"E","E"), char22 = Replacer.ReplaceText( char21,"F","F"), char23 = Replacer.ReplaceText( char22,"G","G"), char24 = Replacer.ReplaceText( char23,"H","H"), char25 = Replacer.ReplaceText( char24,"I","I"), char26 = Replacer.ReplaceText( char25,"J","J"), char27 = Replacer.ReplaceText( char26,"K","K"), char28 = Replacer.ReplaceText( char27,"L","L"), char29 = Replacer.ReplaceText( char28,"M","M"), char30 = Replacer.ReplaceText( char29,"N","N"), char31 = Replacer.ReplaceText( char30,"O","O"), char32 = Replacer.ReplaceText( char31,"P","P"), char33 = Replacer.ReplaceText( char32,"Q","Q"), char34 = Replacer.ReplaceText( char33,"R","R"), char35 = Replacer.ReplaceText( char34,"S","S"), char36 = Replacer.ReplaceText( char35,"T","T"), char37 = Replacer.ReplaceText( char36,"U","U"), char38 = Replacer.ReplaceText( char37,"V","V"), char39 = Replacer.ReplaceText( char38,"W","W"), char40 = Replacer.ReplaceText( char39,"X","X"), char41 = Replacer.ReplaceText( char40,"Y","Y"), char42 = Replacer.ReplaceText( char41,"Z","Z"), char43 = Replacer.ReplaceText( char42,"z","z"), char44 = Replacer.ReplaceText( char43,"{","{"), char45 = Replacer.ReplaceText( char44,"|","|"), char46 = Replacer.ReplaceText( char45,"}","}"), char47 = Replacer.ReplaceText( char46,"~","~"), char48 = Replacer.ReplaceText( char47,"\","¥"), char49 = Replacer.ReplaceText( char48,"a","a"), char50 = Replacer.ReplaceText( char49,"b","b"), char51 = Replacer.ReplaceText( char50,"c","c"), char52 = Replacer.ReplaceText( char51,"d","d"), char53 = Replacer.ReplaceText( char52,"e","e"), char54 = Replacer.ReplaceText( char53,"f","f"), char55 = Replacer.ReplaceText( char54,"g","g"), char56 = Replacer.ReplaceText( char55,"h","h"), char57 = Replacer.ReplaceText( char56,"i","i"), char58 = Replacer.ReplaceText( char57,"j","j"), char59 = Replacer.ReplaceText( char58,"k","k"), char60 = Replacer.ReplaceText( char59,"l","l"), char61 = Replacer.ReplaceText( char60,"m","m"), char62 = Replacer.ReplaceText( char61,"n","n"), char63 = Replacer.ReplaceText( char62,"o","o"), char64 = Replacer.ReplaceText( char63,"p","p"), char65 = Replacer.ReplaceText( char64,"q","q"), char66 = Replacer.ReplaceText( char65,"r","r"), char67 = Replacer.ReplaceText( char66,"s","s"), char68 = Replacer.ReplaceText( char67,"t","t"), char69 = Replacer.ReplaceText( char68,"u","u"), char70 = Replacer.ReplaceText( char69,"v","v"), char71 = Replacer.ReplaceText( char70,"w","w"), char72 = Replacer.ReplaceText( char71,"x","x"), char73 = Replacer.ReplaceText( char72,"y","y"), char74 = Replacer.ReplaceText( char73,"z","z"), char75 = Replacer.ReplaceText( char74,"ガ","ガ"), char76 = Replacer.ReplaceText( char75,"ギ","ギ"), char77 = Replacer.ReplaceText( char76,"グ","グ"), char78 = Replacer.ReplaceText( char77,"ゲ","ゲ"), char79 = Replacer.ReplaceText( char78,"ゴ","ゴ"), char80 = Replacer.ReplaceText( char79,"ザ","ザ"), char81 = Replacer.ReplaceText( char80,"ジ","ジ"), char82 = Replacer.ReplaceText( char81,"ズ","ズ"), char83 = Replacer.ReplaceText( char82,"ゼ","ゼ"), char84 = Replacer.ReplaceText( char83,"ゾ","ゾ"), char85 = Replacer.ReplaceText( char84,"ダ","ダ"), char86 = Replacer.ReplaceText( char85,"ヂ","ヂ"), char87 = Replacer.ReplaceText( char86,"ヅ","ヅ"), char88 = Replacer.ReplaceText( char87,"デ","デ"), char89 = Replacer.ReplaceText( char88,"ド","ド"), char90 = Replacer.ReplaceText( char89,"バ","バ"), char91 = Replacer.ReplaceText( char90,"パ","パ"), char92 = Replacer.ReplaceText( char91,"ビ","ビ"), char93 = Replacer.ReplaceText( char92,"ピ","ピ"), char94 = Replacer.ReplaceText( char93,"ブ","ブ"), char95 = Replacer.ReplaceText( char94,"プ","プ"), char96 = Replacer.ReplaceText( char95,"ベ","ベ"), char97 = Replacer.ReplaceText( char96,"ペ","ペ"), char98 = Replacer.ReplaceText( char97,"ボ","ボ"), char99 = Replacer.ReplaceText( char98,"ポ","ポ"), char100 = Replacer.ReplaceText( char99,"ヴ","ヴ"), char101 = Replacer.ReplaceText( char100,"ァ","ァ"), char102 = Replacer.ReplaceText( char101,"ア","ア"), char103 = Replacer.ReplaceText( char102,"ィ","ィ"), char104 = Replacer.ReplaceText( char103,"イ","イ"), char105 = Replacer.ReplaceText( char104,"ゥ","ゥ"), char106 = Replacer.ReplaceText( char105,"ウ","ウ"), char107 = Replacer.ReplaceText( char106,"ェ","ェ"), char108 = Replacer.ReplaceText( char107,"エ","エ"), char109 = Replacer.ReplaceText( char108,"ォ","ォ"), char110 = Replacer.ReplaceText( char109,"オ","オ"), char111 = Replacer.ReplaceText( char110,"カ","カ"), char112 = Replacer.ReplaceText( char111,"キ","キ"), char113 = Replacer.ReplaceText( char112,"ク","ク"), char114 = Replacer.ReplaceText( char113,"ケ","ケ"), char115 = Replacer.ReplaceText( char114,"コ","コ"), char116 = Replacer.ReplaceText( char115,"サ","サ"), char117 = Replacer.ReplaceText( char116,"シ","シ"), char118 = Replacer.ReplaceText( char117,"ス","ス"), char119 = Replacer.ReplaceText( char118,"セ","セ"), char120 = Replacer.ReplaceText( char119,"ソ","ソ"), char121 = Replacer.ReplaceText( char120,"タ","タ"), char122 = Replacer.ReplaceText( char121,"チ","チ"), char123 = Replacer.ReplaceText( char122,"ッ","ッ"), char124 = Replacer.ReplaceText( char123,"ツ","ツ"), char125 = Replacer.ReplaceText( char124,"テ","テ"), char126 = Replacer.ReplaceText( char125,"ト","ト"), char127 = Replacer.ReplaceText( char126,"ナ","ナ"), char128 = Replacer.ReplaceText( char127,"ニ","ニ"), char129 = Replacer.ReplaceText( char128,"ヌ","ヌ"), char130 = Replacer.ReplaceText( char129,"ネ","ネ"), char131 = Replacer.ReplaceText( char130,"ノ","ノ"), char132 = Replacer.ReplaceText( char131,"ハ","ハ"), char133 = Replacer.ReplaceText( char132,"ヒ","ヒ"), char134 = Replacer.ReplaceText( char133,"フ","フ"), char135 = Replacer.ReplaceText( char134,"ヘ","ヘ"), char136 = Replacer.ReplaceText( char135,"ホ","ホ"), char137 = Replacer.ReplaceText( char136,"マ","マ"), char138 = Replacer.ReplaceText( char137,"ミ","ミ"), char139 = Replacer.ReplaceText( char138,"ム","ム"), char140 = Replacer.ReplaceText( char139,"メ","メ"), char141 = Replacer.ReplaceText( char140,"モ","モ"), char142 = Replacer.ReplaceText( char141,"ャ","ャ"), char143 = Replacer.ReplaceText( char142,"ヤ","ヤ"), char144 = Replacer.ReplaceText( char143,"ュ","ュ"), char145 = Replacer.ReplaceText( char144,"ユ","ユ"), char146 = Replacer.ReplaceText( char145,"ョ","ョ"), char147 = Replacer.ReplaceText( char146,"ヨ","ヨ"), char148 = Replacer.ReplaceText( char147,"ラ","ラ"), char149 = Replacer.ReplaceText( char148,"リ","リ"), char150 = Replacer.ReplaceText( char149,"ル","ル"), char151 = Replacer.ReplaceText( char150,"レ","レ"), char152 = Replacer.ReplaceText( char151,"ロ","ロ"), char153 = Replacer.ReplaceText( char152,"ヮ","ヮ"), char154 = Replacer.ReplaceText( char153,"ワ","ワ"), char155 = Replacer.ReplaceText( char154,"ヰ","ヰ"), char156 = Replacer.ReplaceText( char155,"ヱ","ヱ"), char157 = Replacer.ReplaceText( char156,"ヲ","ヲ"), char158 = Replacer.ReplaceText( char157,"ン","ン"), char159 = Replacer.ReplaceText( char158,"ヵ","ヵ"), char160 = Replacer.ReplaceText( char159,"ヶ","ヶ") result = char160 in result