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

コーディング レベル: 初級
所要時間: 5 分
プロジェクトの種類: 時間ドリブンのトリガーを使用した自動化

目標

  • ソリューションの機能を理解します。
  • Apps Script サービスがソリューション内でどのように機能するかを理解します。
  • スクリプトを設定します。
  • スクリプトを実行します。

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

株式を購入して価値が下がった場合は、その株式を売却して別の株を購入し、税金控除を申請できます。そのような行為は「減税収穫」と呼ばれます。 Google スプレッドシートのスプレッドシートに株式の一覧を作成し、株価が購入価格を下回った場合にメール通知アラートを受け取ることができます。

株価と Gmail のメール通知アラートを含む Google スプレッドシートのスクリーンショット。

仕組み

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

Apps Script サービス

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

前提条件

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

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

スクリプトを設定する

  1. 次のボタンをクリックして、減税アラートのサンプル スプレッドシートのコピーを作成します。このソリューションの Apps Script プロジェクトは、スプレッドシートに添付されています。
    コピーを作成
  2. コピーしたスプレッドシートで、独自の株価情報でシートを更新するか、提供されたテストデータを使用します。

スクリプトを実行する

  1. コピーしたスプレッドシートで、[拡張機能] > [Apps Script] をクリックします。
  2. 関数のプルダウンで [checkLosses] を選択します。
  3. [実行] をクリックします。
  4. プロンプトが表示されたら、スクリプトを承認します。OAuth 同意画面に「このアプリは確認されていません」という警告が表示された場合は、[詳細設定] > [{プロジェクト名}(安全でない)に移動] を選択します。

  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
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(
    "Calculations"
  );
  let source = sheet.getRange("A:G");
  let 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 (let i in data) {
    //Skips the first row
    if (n++ == 0) continue;

    //Loads the current row
    let 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] +
        ": " +
        (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 が作成しました。Twitter で Jeremy(@jglassenberg)を見つけてください。

このサンプルは、Google Developer Experts の協力により Google が保守しています。

次のステップ