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

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

目標

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

前提条件

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

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

スクリプトを設定する

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

  1. Apps Script エディタを開くには、 script.google.com に移動します。script.google.com に初めてアクセスする場合は、[View Dashboard] をクリックします。
  2. [新しいプロジェクト] をクリックします。
  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. プロンプトが表示されたら、スクリプトを承認します。 OAuth 同意画面に「このアプリは確認されていません」という警告が表示された場合は、[詳細設定] > [{プロジェクト名} に移動(安全でない)] を選択します。

  3. スクリプトの実行が完了したら、Gmail の受信トレイでメールを確認します。

  4. メールを開き、リンクをクリックして作成したドキュメントを開きます。

次のステップ