自動化のクイックスタート

Google ドキュメントを作成し、ドキュメントへのリンクをメールで送信する自動化を構築して実行します。

目標

  • スクリプトを設定します。
  • スクリプトを実行します。

前提条件

このサンプルを使用するには、次の前提条件を満たしている必要があります。

  • Google アカウント(Google Workspace アカウントの場合、管理者の承認が必要となる可能性があります)。
  • インターネットにアクセスできるウェブブラウザ。

スクリプトを設定する

自動化を構築する手順は次のとおりです。

  1. Google Apps Script エディタを開くには、script.google.com にアクセスします。script.google.com に初めてアクセスする場合は、[ダッシュボードを表示] をクリックします。
  2. [New project](新しいプロジェクト)をクリックします。
  3. スクリプト エディタ内のコードを削除して、以下のコードを貼り付けます。

    templates/standalone/helloWorld.gs
    /**
     * Creates a Google Doc and sends an email to the current user with a link to the doc.
     */
    function createAndSendDocument() {
      try {
        // Create a new Google Doc named 'Hello, world!'
        const doc = DocumentApp.create("Hello, world!");
    
        // Access the body of the document, then add a paragraph.
        doc
          .getBody()
          .appendParagraph("This document was created by Google Apps Script.");
    
        // Get the URL of the document.
        const url = doc.getUrl();
    
        // Get the email address of the active user - that's you.
        const email = Session.getActiveUser().getEmail();
    
        // Get the name of the document to use as an email subject line.
        const subject = doc.getName();
    
        // Append a new string to the "url" variable to use as an email body.
        const body = `Link to your doc: ${url}`;
    
        // Send yourself an email with a link to the document.
        GmailApp.sendEmail(email, subject, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log("Failed with error %s", err.message);
      }
    }
  4. [保存] 保存アクションを表すアイコン をクリックします。

  5. [無題のプロジェクト] をクリックします。

  6. スクリプトの名前を入力して、[名前を変更] をクリックします。

スクリプトを実行する

スクリプトを実行するには、次の操作を行います。

  1. [実行] をクリックします。
  2. メッセージが表示されたら、スクリプトを承認します。<<../samples/_snippets/oauth.md>>
  3. スクリプトの実行が完了したら、Gmail の受信トレイ でメールを確認します。
  4. メールを開き、リンクをクリックして作成したドキュメントを開きます。

次のステップ