接收股价下跌提醒
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
编码水平:初级
时长:5 分钟
项目类型:使用定时触发器实现自动化
目标
- 了解解决方案的功能。
- 了解 Apps 脚本服务在解决方案中的作用。
- 设置脚本。
- 运行脚本。
关于此解决方案
如果您购买的股票价值下跌,您可以出售该股票,购买另一只股票,并申请税收减免。这种做法称为“税收损失收割”。
在 Google 表格中列出您的股票,并在股票价格跌至低于买入价时收到电子邮件提醒。

运作方式
该电子表格使用 Google 表格中的 Google 财经内置函数来获取股票的当前价格。该脚本会将每只列出的股票的买入价与其当前价格进行比较。然后,它会通过电子邮件向您发送一份股价已跌至低于买入价的股票清单。您可以根据需要设置脚本的运行频率。
Apps 脚本服务
此解决方案使用以下服务:
- 电子表格服务 - 循环遍历每个列出的股票,并将股票价格与购买价格进行比较。
- Gmail 服务 - 创建并发送电子邮件,其中包含跌破买入价的股票。
前提条件
如需使用此示例,您需要满足以下前提条件:
- Google 账号(Google Workspace 账号可能需要管理员批准)。
- 可访问互联网的网络浏览器。
设置脚本
- 点击以下按钮,制作税收损失收割提醒示例电子表格的副本。此解决方案的 Apps 脚本项目已附加到电子表格。
制作副本
- 在复制的电子表格中,使用您自己的库存信息更新工作表,或使用提供的测试数据。
运行脚本
- 在复制的电子表格中,依次点击扩展
> Apps 脚本。
- 在函数下拉菜单中,选择 checkLosses。
- 点击运行。
根据提示为脚本授权。
如果 OAuth 权限请求页面显示警告此应用未经过验证,请继续操作,依次选择高级 >
前往“{项目名称}”(不安全)。
查看电子邮件,了解哪些股票的价格已跌至低于购买价格。
如果您未收到电子邮件,请检查您列表中的任何股票价格是否低于其买入价。
创建定时触发器
- 返回到脚本项目。
- 点击左侧的触发器图标 alarm。
- 点击右下角的添加触发器。
- 在选择要运行的函数部分,确保选中 checkLosses。
- 对于选择事件来源,选择时间驱动。
- 配置脚本的运行频率,然后点击保存。
查看代码
如需查看此解决方案的 Apps 脚本代码,请点击下方的查看源代码:
贡献者
此示例由产品管理和平台战略顾问 Jeremy Glassenberg 创建。您可以在 Twitter 上关注 Jeremy:@jglassenberg。
此示例由 Google 在 Google 开发者专家的帮助下维护。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis Google Apps Script solution automatically alerts you via email when your stocks drop below their purchase price, aiding in tax loss harvesting.\u003c/p\u003e\n"],["\u003cp\u003eThe script utilizes Google Sheets to track stock data and Google Finance functions to fetch current prices, comparing them to your purchase prices.\u003c/p\u003e\n"],["\u003cp\u003eYou can easily set up this solution by copying the provided spreadsheet, inputting your stock information, and running the script to receive email alerts.\u003c/p\u003e\n"],["\u003cp\u003eThe script can be automated with a time-driven trigger, enabling periodic checks and notifications without manual intervention.\u003c/p\u003e\n"],["\u003cp\u003eThe solution is beginner-friendly and requires minimal setup, providing a simple yet effective way to monitor your stock portfolio for potential tax loss harvesting opportunities.\u003c/p\u003e\n"]]],[],null,["# Get stock price drop alerts\n\n**Coding level** : Beginner \n\n**Duration** : 5 minutes \n\n**Project type** : Automation with a [time-driven trigger](/apps-script/guides/triggers/installable#time-driven_triggers)\n\nObjectives\n----------\n\n- Understand what the solution does.\n- Understand what the Apps Script services do within the solution.\n- Set up the script.\n- Run the script.\n\nAbout this solution\n-------------------\n\nIf you purchase a stock and the value drops, you can sell that stock, purchase\nanother, and claim a tax deduction. Doing so is known as a tax loss harvest.\nList your stocks in a Google Sheets spreadsheet and get email alerts if\na stock price drops below its purchase price.\n\n### How it works\n\nThe spreadsheet uses the\n[Google Finance built-in function](https://support.google.com/docs/answer/3093281)\nin Sheets to get the current prices of stocks. The script\ncompares the purchase\nprice of each listed stock with its current price. Then, it emails you a list of\nstocks that have fallen below their purchase price. You can set the script to\nrun as often as you want.\n\n### Apps Script services\n\nThis solution uses the following services:\n\n- [Spreadsheet service](/apps-script/reference/spreadsheet)--Loops through each listed stock and compares the stock price to the purchase price.\n- [Gmail service](/apps-script/reference/gmail)--Creates and sends an email of the stocks that have fallen below their purchase price.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\n1. Click the following button to make a copy of the **Tax loss harvest alerts** sample spreadsheet. The Apps Script project for this solution is attached to the spreadsheet. \n [Make a copy](https://docs.google.com/spreadsheets/d/1hYxWe9zku7XcY0JO3KCOABqkk03K9L0xDytnSm4v71Y/copy)\n2. In your copied spreadsheet, update the sheet with your own stock information, or use the provided test data.\n\nRun the script\n--------------\n\n1. In your copied spreadsheet, click **Extensions** \\\u003e **Apps Script**.\n2. In the function dropdown, select **checkLosses**.\n3. Click **Run**.\n4. When prompted, authorize the script.\n If the OAuth consent screen displays the warning, **This app isn't verified** ,\n continue by selecting **Advanced** \\\u003e\n **Go to {Project Name} (unsafe)**.\n\n5. Check your email for a list of stocks that fell below their purchase price.\n If you didn't receive an email, check to see if any of the stock prices in\n your list are lower than their purchase price.\n\n### Create a time-driven trigger\n\n1. Return to the script project.\n2. At the left, click **Triggers** alarm.\n3. At the bottom-right, click **Add trigger**.\n4. For **Choose which function to run** , make sure **checkLosses** is selected.\n5. For **Select event source** , select **Time-driven**.\n6. Configure how often you want the script to run and click **Save**.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click\n**View source code** below: \n\n#### View source code\n\n### Code.gs\n\n\u003cbr /\u003e\n\nsolutions/automations/tax-loss-harvest-alerts/Code.js \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/solutions/automations/tax-loss-harvest-alerts/Code.js) \n\n```javascript\n// To learn how to use this script, refer to the documentation:\n// https://developers.google.com/apps-script/samples/automations/tax-loss-harvest-alerts\n\n/*\nCopyright 2022 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/** \n* Checks for losses in the sheet.\n*/\nfunction checkLosses() {\n // Pulls data from the spreadsheet\n let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(\n \"Calculations\"\n );\n let source = sheet.getRange(\"A:G\");\n let data = source.getValues();\n\n //Prepares the email alert content\n let message = \"Stocks: \u003cbr\u003e\u003cbr\u003e\";\n\n let send_message = false;\n\n console.log(\"starting loop\");\n\n //Loops through the cells in the spreadsheet to find cells where the stock fell below purchase price\n let n = 0;\n for (let i in data) {\n //Skips the first row\n if (n++ == 0) continue;\n\n //Loads the current row\n let row = data[i];\n\n console.log(row[1]);\n console.log(row[6]);\n\n //Once at the end of the list, exits the loop\n if (row[1] == \"\") break;\n\n //If value is below purchase price, adds stock ticker and difference to list of tax loss opportunities\n if (row[6] \u003c 0) {\n message +=\n row[1] +\n \": \" +\n (parseFloat(row[6].toString()) * 100).toFixed(2).toString() +\n \"%\u003cbr\u003e\";\n send_message = true;\n }\n }\n if (!send_message) return;\n\n MailApp.sendEmail({\n to: SpreadsheetApp.getActiveSpreadsheet().getOwner().getEmail(),\n subject: \"Tax-loss harvest\",\n htmlBody: message,\n\n });\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nContributors\n------------\n\nThis sample was created by Jeremy Glassenberg, Product Management and Platform\nStrategy Consultant. Find Jeremy on Twitter\n[@jglassenberg](https://twitter.com/jglassenberg).\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Time-driven triggers](/apps-script/guides/triggers/installable#time-driven_triggers)\n- [Extend Google Sheets](/apps-script/guides/sheets)"]]