取得股價降價快訊

程式設計層級:新手
時間長度:5 分鐘
專案類型:透過時間導向的觸發條件自動化

目標

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

認識這項解決方案

如果您購買一個股票,而商品價值下降,您可以銷售該股票、購買其他股票並申請稅額減免。這種行為稱為減稅。 使用 Google 試算表列出您的股票,並在股價低於購買價格時接收電子郵件快訊。

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

運作方式

此試算表使用試算表中的 Google 財經內建函式取得股票目前的價格。指令碼會比較每支所列股票的購買價格與其目前價格。然後,它會以電子郵件將低於購買價格的股票清單傳送給您。您可以設定指令碼的執行頻率不限。

Apps Script 服務

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

  • 試算表服務 - 循環播放每個上架的股票,並將股價與購買價格進行比較。
  • Gmail 服務:建立並傳送低於購買價格的股票電子郵件。

必要條件

如要使用這個範例,您必須具備以下先決條件:

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

設定指令碼

  1. 點選下方按鈕,複製「減免稅額快訊」範例試算表。這個解決方案的 Apps Script 專案已附加至試算表。
    建立副本
  2. 在複製的試算表中,使用您自己的股票資訊更新工作表,或使用提供的測試資料。

執行指令碼

  1. 在複製的試算表中,依序按一下「擴充功能」>「Apps Script」
  2. 在函式下拉式選單中,選取「checkLosses」checkLosses
  3. 按一下「執行」
  4. 出現提示訊息時,請授權指令碼。如果 OAuth 同意畫面顯示警告,請依序選取「Advanced」>「Go to {Project Name} (unsafe)」繼續操作。

  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 (@j Glassenberg)。

這個範例由 Google 開發人員專家協助維護。

後續步驟