値下げアラートを受け取る

コーディング レベル: 初級
期間: 5 分
プロジェクト タイプ: 時間主導型トリガーによる自動化

目標

  • ソリューションの機能について理解する。
  • ソリューション内での Apps Script サービスの機能について理解する。
  • スクリプトを設定する。
  • スクリプトを実行する。

このソリューションについて

株を購入して価値が下落した場合、その株を売却して別の株を購入し、税控除を申請できます。これは税金損失の実現と呼ばれます。 Google スプレッドシートに株をリストアップすると、株価が購入価格を下回った場合にメールアラートを受け取ることができます。

株価と Gmail のメールアラートを含む Google スプレッドシート。

仕組み

スプレッドシートでは、スプレッドシートの Google Finance 組み込み関数を使用して、株の現在の価格を取得します。スクリプトは、リストに記載されている各株の購入価格と現在の価格を比較します。その後、購入価格を下回った株のリストがメールで送信されます。スクリプトは、任意の頻度で実行するように設定できます。

Apps Script サービス

このソリューションでは、次のサービスを使用します。

前提条件

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

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

スクリプトを設定する

  1. 次のボタンをクリックして、税金損失の実現アラート のサンプル スプレッドシートのコピーを作成します。このソリューションの Apps Script プロジェクトは、スプレッドシートに添付されています。

    コピーを作成

  2. コピーしたスプレッドシートで、独自の株の情報でシートを更新するか、提供されているテストデータを使用します。

スクリプトを実行する

  1. コピーしたスプレッドシートで、[拡張機能] [>] [Apps Script] を選択します。
  2. [関数] プルダウンで [checkLosses] を選択します。
  3. [実行] をクリックします。
  4. メッセージが表示されたら、スクリプトを承認します。 <<../_snippets/oauth.md>>
  5. 購入価格を下回った株のリストがメールで届いていることを確認します。 メールが届いていない場合は、リスト内の株価が購入価格を下回っているかどうかを確認してください。

時間主導型トリガーを作成する

  1. スクリプト プロジェクトに戻ります。
  2. 左側の [トリガー] をクリックします。
  3. 右下の [トリガーを追加] をクリックします。
  4. [実行する関数を選択] で、[checkLosses] が選択されていることを確認します。
  5. [イベントソースを選択] で [時間主導型] を選択します。
  6. スクリプトの実行頻度を設定し、[保存] をクリックします。

コードを確認する

このソリューションの Apps Script コードを確認するには、 [ソースコードを表示]をクリックします:

ソースコードを表示

Code.gs

solutions/automations/tax-loss-harvest-alerts/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/tax-loss-harvest-alerts

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Checks for losses in the sheet.
 */
function checkLosses() {
  // Pulls data from the spreadsheet
  const sheet =
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculations");
  const source = sheet.getRange("A:G");
  const data = source.getValues();

  //Prepares the email alert content
  let message = "Stocks: <br><br>";

  let send_message = false;

  console.log("starting loop");

  //Loops through the cells in the spreadsheet to find cells where the stock fell below purchase price
  let n = 0;
  for (const i in data) {
    //Skips the first row
    if (n++ === 0) continue;

    //Loads the current row
    const row = data[i];

    console.log(row[1]);
    console.log(row[6]);

    //Once at the end of the list, exits the loop
    if (row[1] === "") break;

    //If value is below purchase price, adds stock ticker and difference to list of tax loss opportunities
    if (row[6] < 0) {
      message += `${row[1]}: ${(Number.parseFloat(row[6].toString()) * 100).toFixed(2).toString()}%<br>`;
      send_message = true;
    }
  }
  if (!send_message) return;

  MailApp.sendEmail({
    to: SpreadsheetApp.getActiveSpreadsheet().getOwner().getEmail(),
    subject: "Tax-loss harvest",
    htmlBody: message,
  });
}

寄稿者

このサンプルは、プロダクト マネジメントおよびプラットフォーム戦略コンサルタントの Jeremy Glassenberg が作成しました。Jeremy の Twitter アカウントは @jglassenberg です。

このサンプルは、Google デベロッパー エキスパートの協力のもと、Google によって管理されています。

次のステップ