文字列の検索
This content is not available in your language yet.
ここではオフィススクリプトを使って、文字列に特定の文字が含まれているかを調べる方法と、見つかった位置を使って区切り文字の前後を切り出す方法を、サンプルコードと併せて解説します。
含まれているかどうかを調べる
Section titled “含まれているかどうかを調べる”利用するメソッド
Section titled “利用するメソッド”位置は不要で、含まれているかどうかだけを知りたい場合は、Stringオブジェクトに含まれているincludesメソッドを使用します。
/** * @param searchString 検索する文字列 * @param position 検索を始める位置(省略時は 0) * @return 含まれている場合は true */includes(searchString: string, position?: number): boolean;サンプルコード
Section titled “サンプルコード”function main(workbook: ExcelScript.Workbook) { /** 対象文字列 */ const mail = 'sato@example.jp';
if (mail.includes('@')) { console.log('@が含まれています'); }}見つかった位置を調べる
Section titled “見つかった位置を調べる”利用するメソッド
Section titled “利用するメソッド”見つかった位置を知りたい場合は、Stringオブジェクトに含まれているindexOfメソッドを使用します。
/** * @param searchString 検索する文字列 * @param position 検索を始める位置(省略時は 0) * @return 最初に見つかった位置(0 から始まる)。見つからない場合は -1 */indexOf(searchString: string, position?: number): number;複数の箇所で見つかる場合は、最初に見つかった位置が返されます。最後に見つかった位置を調べる場合は、lastIndexOfメソッドを使用します。
console.log('リンゴミカンバナナ'.indexOf('ミカン')); // 3console.log('リンゴミカンリンゴ'.indexOf('リンゴ', 1)); // 6(インデックス 1 = 2 文字目以降で検索)console.log('リンゴミカンバナナ'.indexOf('ブドウ')); // -1区切り文字の前後を切り出す
Section titled “区切り文字の前後を切り出す”indexOfで調べた位置をsliceメソッドに渡すと、区切り文字の前後を切り出せます。
例えばsato@example.jpの@は先頭から 5 文字目(インデックスは 4)にあるため、indexOf('@')は4を返します。
@より前:slice(0, 4)(終了位置の文字は含まれないため、@の手前までになる)@より後:slice(5)(@の位置に 1 を足した位置から最後まで)
開始位置・終了位置を変えて、切り出される範囲を確かめてみましょう。
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Character | s | a | t | o | @ | e | x | a | m | p | l | e | . | j | p |
| From end | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Resultmail.slice(0, 4)'sato'
Change the values to see which characters are extracted.
サンプルコード
Section titled “サンプルコード”メールアドレスを@の前後で、ユーザー名とドメインに分けるサンプルコードです。
function main(workbook: ExcelScript.Workbook) { /** 対象文字列 */ const mail = 'sato@example.jp';
/** @ の位置 */ const atIndex = mail.indexOf('@');
if (atIndex === -1) { throw `${mail}に@が含まれていません`; }
/** ユーザー名(@ より前) */ const user = mail.slice(0, atIndex); /** ドメイン(@ より後) */ const domain = mail.slice(atIndex + 1);
console.log(user); // sato console.log(domain); // example.jp}最後の区切り文字で分ける
Section titled “最後の区切り文字で分ける”ファイル名から拡張子を取り出す場合など、区切り文字が複数含まれていて最後の 1 つで分けたい場合は、lastIndexOfメソッドを使用します。
data.2026.xlsxに対してindexOf('.')を使うと最初の.の位置である4が返されますが、lastIndexOf('.')を使うと最後の.の位置である9が返されます。
拡張子は.の次の文字から始まるため、切り出しの開始位置は9 + 1で10になります。
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Character | d | a | t | a | . | 2 | 0 | 2 | 6 | . | x | l | s | x |
| From end | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Resultfilename.slice(10)'xlsx'
Change the values to see which characters are extracted.
function main(workbook: ExcelScript.Workbook) { /** 対象のファイル名 */ const filename = 'data.2026.xlsx';
/** 最後の . の位置 */ const dotIndex = filename.lastIndexOf('.');
if (dotIndex === -1) { throw `${filename}には拡張子がありません`; }
console.log(filename.slice(0, dotIndex)); // data.2026 console.log(filename.slice(dotIndex + 1)); // xlsx}