Last active
October 29, 2022 00:54
-
-
Save hi3103/c2f1f986d78d98a64df6205982b7051f to your computer and use it in GitHub Desktop.
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
/* | |
Googleスプレッドシートで半角カタカナを全角カタカナに変換する関数「f7conv」をGASで作る | |
https://hi3103.net/notes/google/1394 | |
*/ | |
//全角カタカナ一覧 | |
const zenkata = [ | |
'ア', 'イ', 'ウ', 'エ', 'オ', | |
'カ', 'キ', 'ク', 'ケ', 'コ', | |
'サ', 'シ', 'ス', 'セ', 'ソ', | |
'タ', 'チ', 'ツ', 'テ', 'ト', | |
'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', | |
'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', | |
'マ', 'ミ', 'ム', 'メ', 'モ', | |
'ヤ', 'ユ', 'ヨ', | |
'ラ', 'リ', 'ル', 'レ', 'ロ', | |
'ワ', 'ヲ', 'ン', | |
'ガ', 'ギ', 'グ', 'ゲ', 'ゴ', | |
'ザ', 'ジ', 'ズ', 'ゼ', 'ゾ', | |
'ダ', 'ヂ', 'ヅ', 'デ', 'ド', | |
'バ', 'ビ', 'ブ', 'ベ', 'ボ', | |
'パ', 'ピ', 'プ', 'ペ', 'ポ', | |
'ァ', 'ィ', 'ゥ', 'ェ', 'ォ', | |
'ャ', 'ュ', 'ョ', | |
'ッ', 'ヴ', | |
'ー', '・', '。', '、']; | |
//半角カタカナ一覧 | |
const hankata = [ | |
'ア', 'イ', 'ウ', 'エ', 'オ', | |
'カ', 'キ', 'ク', 'ケ', 'コ', | |
'サ', 'シ', 'ス', 'セ', 'ソ', | |
'タ', 'チ', 'ツ', 'テ', 'ト', | |
'ナ', 'ニ', 'ヌ', 'ネ', 'ノ', | |
'ハ', 'ヒ', 'フ', 'ヘ', 'ホ', | |
'マ', 'ミ', 'ム', 'メ', 'モ', | |
'ヤ', 'ユ', 'ヨ', | |
'ラ', 'リ', 'ル', 'レ', 'ロ', | |
'ワ', 'ヲ', 'ン', | |
'ガ', 'ギ', 'グ', 'ゲ', 'ゴ', | |
'ザ', 'ジ', 'ズ', 'ゼ', 'ゾ', | |
'ダ', 'ヂ', 'ヅ', 'デ', 'ド', | |
'バ', 'ビ', 'ブ', 'ベ', 'ボ', | |
'パ', 'ピ', 'プ', 'ペ', 'ポ', | |
'ァ', 'ィ', 'ゥ', 'ェ', 'ォ', | |
'ャ', 'ュ', 'ョ', | |
'ッ', 'ヴ', | |
'ー', '・', '。', '、']; | |
//関数を作成 | |
function f7conv(text) { | |
//最終的に返す変数を定義 | |
let result = ''; | |
//変換する対象文字列をセット | |
const input = hankata; | |
const output = zenkata; | |
//引数に値が格納されているかチェック | |
if(typeof text === 'undefined') { | |
result = 'エラー:全角カタカナに変換する文字列が指定されていません。'; | |
}else{ | |
//引数で渡された文字列を定数にセット | |
const textStr = text; | |
//1文字ずつ分割して配列に格納 | |
const textArr = textStr.split(''); | |
//文字数が0でなければ実行 | |
if(textArr.length !== 0){ | |
//文字を再格納する配列を定義 | |
let array = []; | |
//分割した文字の数だけループを回し、もし濁点・半濁点だった場合は1つ前の配列の中身とセットにして array に格納 | |
for (let i = 0; i < textArr.length; i++) { | |
if (textArr[i] == '゙' || textArr[i] == '゚') { | |
array[array.length - 1] = (textArr[i - 1] + textArr[i]); | |
} else { | |
array.push(textArr[i]); | |
} | |
} | |
//再格納した文字の数だけループを回し、もし半角カナがあったら全角カナに直して result へ格納 | |
for (let j = 0; j < array.length; j++) { | |
let index = input.indexOf(array[j]); | |
if (index == -1) { | |
result = result + array[j]; | |
} else { | |
result = result + output[index]; | |
} | |
} | |
} | |
} | |
//結果を返す | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment