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

Google ドキュメントのドキュメントとドキュメントを作成し、 ドキュメントへのリンクがメールで送信されます。

目標

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

前提条件

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

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

スクリプトを設定する

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

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

    テンプレート/スタンドアロン/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. メールを開き、リンクをクリックして作成したドキュメントを開きます。

次のステップ