0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Googleドキュメントで上付文字を作る

Last updated at Posted at 2025-07-23

1.最初に

  • 論文的なものを書いていて、投稿規定を見ると、参考文献部分は上付きで n)という形にしなさい、とのこと。
PythonのMatplotlib 15), Seaborn 16), Plotly 17) などを用いて、これらの統合的な分析結果を分かりやすいグラフやチャート(バブルチャート、ヒートマップ、複合グラフなど)として可視化する。
  • 気づかずに参考文献が50個くらいになっており、手作業がすごく面倒(こんなこと毎回したくない)だったので、ちょうど書いていたGoogleドキュメントならAppScriptが使える、ということでスクリプトを作成することに。

2.コード

  • 途中までgeminiに相談しながら、やったが、どうしても
    if (textElement) {
      textElement.setSuperscript(start, end, true);
    }

部分でエラーになってしまう(そんな関数ない!)ので、ウェブ情報を探して以下の情報をゲット
これを組み合わせて完成

/**
 * ドキュメント内の「数字)」という形式のテキストを検索し、
 * 文字列全体を上付き文字に設定します。
 */
function formatNumberParenthesisSuperscript() {
  const body = DocumentApp.getActiveDocument().getBody();
  
  // 「1桁以上の数字」と「閉じ括弧」の組み合わせを検索します。
  // 例: "5)", "12)", "123)"
  const searchPattern = '(\\d+)\\)';
  
  // ドキュメント内の全ての一致箇所を検索して配列に格納します。
  const ranges = [];
  let searchResult = body.findText(searchPattern);
  while (searchResult) {
    ranges.push(searchResult);
    searchResult = body.findText(searchPattern, searchResult);
  }

  // 一致するテキストが見つからなかった場合、メッセージを表示して終了します。
  if (ranges.length === 0) {
    DocumentApp.getUi().alert('対象となる「数字)」の形式のテキストが見つかりませんでした。');
    return;
  }

  // 配列を逆順に処理して、安全に書式を適用します。
  for (let i = ranges.length - 1; i >= 0; i--) {
    const range = ranges[i];
    const textElement = range.getElement().asText();
    const start = range.getStartOffset();
    const end = range.getEndOffsetInclusive();

    // 見つかった範囲全体(数字と括弧の両方)に上付き書式を適用します。
    if (textElement) {
      textElement.setTextAlignment(start, end, DocumentApp.TextAlignment.SUPERSCRIPT);
      
    }
  }
  
  // 完了メッセージを表示します。
  DocumentApp.getUi().alert(ranges.length + '件の変換が完了しました。');
}

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?