取得股價降價快訊

程式碼程度:初學者
時間長度:5 分鐘
專案類型:使用時間觸發條件進行自動化

目標

  • 瞭解解決方案的功能。
  • 瞭解解決方案中的 Apps Script 服務功能。
  • 設定指令碼。
  • 執行指令碼。

認識這項解決方案

如果你購買股票後價值下跌,可以賣出該股票,然後購買另一支股票,並申請扣抵稅額。這種做法稱為「稅損收成」。在 Google 試算表列出股票,如果股價跌破買入價格,就會收到電子郵件快訊。

Google 試算表的螢幕截圖,顯示股票價格和 Gmail 電子郵件快訊。

運作方式

這份試算表使用 Google 試算表的內建 Google 財經函式,取得目前的股票價格。這項指令碼會比較清單中每檔股票的購買價格與目前價格。然後,系統會透過電子郵件傳送清單,列出價格低於購買價格的股票。您可以視需要設定指令碼的執行頻率。

Apps Script 服務

這項解決方案會使用下列服務:

  • 試算表服務:逐一列出股票,並比較股票價格與購買價格。
  • Gmail 服務:建立並傳送電子郵件,通知使用者哪些股票的價格已低於買入價格。

必要條件

如要使用這個範例,您必須符合下列先決條件:

  • Google 帳戶 (Google Workspace 帳戶可能需要管理員核准)。
  • 可連上網際網路的網路瀏覽器。

設定指令碼

  1. 按一下下列按鈕,複製稅損收成快訊試算表範本。這項解決方案的 Apps Script 專案已附加至試算表。
    建立副本
  2. 在複製的試算表中,使用你自己的庫存資訊更新工作表,或使用提供的測試資料。

執行指令碼

  1. 在複製的試算表中,依序點按「擴充功能」>「Apps Script」
  2. 在函式下拉式選單中,選取「checkLosses」checkLosses
  3. 按一下「執行」
  4. 出現提示訊息時,請授權執行指令碼。如果 OAuth 同意畫面顯示「這個應用程式未經驗證」警告,請依序選取「進階」>「前往『{專案名稱}』(不安全)」,繼續操作。

  5. 請查看電子郵件,瞭解哪些股票的價格低於購買價格。如果沒有收到電子郵件,請檢查清單中的股票價格是否低於購買價格。

建立時間驅動型觸發條件

  1. 返回腳本專案。
  2. 按一下左側的「觸發條件」圖示
  3. 按一下右下方的「新增觸發條件」
  4. 在「Choose which function to run」(選擇要執行的函式) 中,請務必選取「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 維護,並由 Google 開發人員專家協助。

後續步驟