LoginSignup
5
7

More than 1 year has passed since last update.

Google Docsのカーソル位置に日付を挿入するGoogle Apps Script

Posted at

Google Docsの、Tools内、Script editorを選択します。スクリプトエディターが開きます。

Google Apps Script (GAS)

image.png

以下をスクリプトとして保存したら、Google Docs自身を一旦閉じ、再度開きます。

/**
 * onOpen functionは Google Docsを開いた際に自動的に実行される。
 * 詳細は以下
 * 
 * Extending Google Docs developer guide:
 *     https://developers.google.com/apps-script/guides/docs
 *
 * Document service reference documentation:
 *     https://developers.google.com/apps-script/reference/document/
 */
function onOpen() {
  // 「ユーティリティ」メニューを追加する。サブメニューには「Insert Date」
  DocumentApp.getUi().createMenu('ユーティリティ')
      .addItem('Insert Date', 'insertAtCursor')
      .addToUi();
}

/**
 * 現在のカーソルに日付を挿入する
 */
function insertAtCursor() {
  var cursor = DocumentApp.getActiveDocument().getCursor();

  if (cursor) {
    // カーソル位置へのテキストの挿入。挿入が null を返した場合は
    // カーソルのある要素がテキストの挿入を許可していないことになる
    var date = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
    var element = cursor.insertText(date);
    if (element) {
      element.setBold(true);
    } else {
      DocumentApp.getUi().alert('このカーソル位置にテキストを挿入できません。');
    }
  } else {
    DocumentApp.getUi().alert('ドキュメント内のカーソルが見つかりません。');
  }
}

メニューに ユーティリティ > Insert Date が表示されていれば完成です。

image.png

参考

上記を参考にしています。

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