简体   繁体   English

Google Sheets (Google Finance) - 一次获取股票价格并跟踪

[英]Google Sheets (Google Finance) - Get stock price once and track

I would like to, upon data being entered into a new row (from external sheet - date, time, stock ticker), use google finance to get the current price of the stock into a cell and then no longer update the price in that cell.我想,在将数据输入新行(来自外部工作表 - 日期、时间、股票行情)后,使用谷歌金融将股票的当前价格放入一个单元格中,然后不再更新该单元格中的价格. In the cell next to it, I want to track the price and in the cell next to that, I want to track the high, since creation (the highest value of the 'current price' cell).在它旁边的单元格中,我想跟踪价格,在旁边的单元格中,我想跟踪自创建以来的最高价(“当前价格”单元格的最高值)。

Here is my sheet https://docs.google.com/spreadsheets/d/1EUU1bZnIfBatNI8H9pPk202PCD1g8wWXt5M0wx6S-jM/edit?usp=sharing这是我的工作表https://docs.google.com/spreadsheets/d/1EUU1bZnIfBatNI8H9pPk202PCD1g8wWXt5M0wx6S-jM/edit?usp=sharing

All I've got so far is a high value tracker that runs on the first row only for the right cells.到目前为止,我所拥有的只是一个高价值的跟踪器,它只针对正确的单元格在第一行运行。 Embarrasingly enough I can't figure out how to apply it to the entire columns.令人尴尬的是,我无法弄清楚如何将它应用于整个列。

So in summary, date time and stock will be entered into column AB and C. When that happens, I want to get the current price of the stock in C, and have that number no longer update.所以总而言之,日期时间和股票将被输入到 AB 和 C 列中。发生这种情况时,我想获得 C 中股票的当前价格,并且不再更新该数字。 In DI want the price to be tracked, like Google Finance normally functions, and in E, the highest value of the D, in the same row.在 DI 中想要跟踪价格,就像 Google Finance 通常的功能一样,在 E 中,D 的最高值,在同一行。

That's the goal.这就是目标。 Any help would be very much appreciated :)任何帮助将不胜感激:)

All I've got so far is a high value tracker that runs on the first row only for the right cells.到目前为止,我所拥有的只是一个高价值的跟踪器,它只针对正确的单元格在第一行运行。 Embarrasingly enough I can't figure out how to apply it to the entire columns.令人尴尬的是,我无法弄清楚如何将它应用于整个列。

The code bound to your spreadsheet is绑定到您的电子表格的代码是

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("D2:E2");
  var values = range.getValues()[0];
  range.setValues([[values[0], Math.max(values[0], values[1])]]);
}

What does this code do?这段代码有什么作用?

  • It sets the range to work with always to D2:E2 - no matter what the actual edited range is它将始终使用的范围设置为D2:E2 - 无论实际编辑的范围是什么
  • It works only with the first row of the range ( range.getValues()[0] )它仅适用于范围的第一行( range.getValues()[0]
  • It compares the 0 and 1 columns of the range (eg columns D and E ) and assigns the value of column D back to column D (is it necessary?) and the higher of the two values to column E它比较范围的01列(例如DE列)并将D列的值分配回D列(是否有必要?)并将两个值中较高的值分配给E

How to modify your code?如何修改你的代码?

It is not quite clear from your description how column D is populated and what you want to do with column F, but to give your general advice:从您的描述中不清楚 D 列是如何填充的以及您想对 F 列做什么,但提供您的一般建议:

  • If you want your function to run on all rows:如果您希望函数在所有行上运行:

Modify your range and expand it until the last row.修改您的范围并将其扩展到最后一行。 Subsequently loop through all rows:随后循环遍历所有行:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange("D2:E"+lastRow);
  for (var i = 0; i < lastRow-1; i++){
    var values = range.getValues()[i];
    range.setValues([[values[0], Math.max(values[0], values[1])]]);
  }
}
  • If you want you code to run only on the currently edited row: Use the event object e.range to find out which is the edited row and work on this row:如果您希望代码仅在当前编辑的行上运行:使用事件对象e.range找出哪个是编辑的行并处理该行:
function onEdit(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = e.range;
  var row = range.getRow();
  var values = sheet.getRange(row, 4, 1, 2).getValues()[0];
  range.setValues([[values[0], Math.max(values[0], values[1])]]);
}

Note: getRange(row, 4, 1, 2) is the notation to get the range starting with the defined row and column 4 ( D ), 1 row long and two columns wide, see here .注意: getRange(row, 4, 1, 2)是获取从定义的行和列 4 ( D ) 开始的范围的表示法,1 行长和 2 列宽,请参见此处

IMPORTANT: If your sheet is being populated automatically from an external sheet - the onEdit trigger will not work for you (it only fires on manual, human-made edits).重要提示:如果您的工作表是从外部工作表自动填充的 - onEdit触发器对您不起作用(它只会在手动、人为编辑时触发)。 In this case you will need a workaround as described here .在这种情况下,您将需要此处所述的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图在谷歌表格中使用 importxml 从谷歌金融获取股票价格,在 xml 元素和语法中苦苦挣扎 - Im trying to use importxml in google sheets to get a stock price from google finance, struggling with xml elements and syntax 谷歌金融股票价格的替代品 - Alternatives to google finance stock prices 如何从Google财经中将小数点后4位的股票价格检索到Google电子表格中? - How to retrieve stock price from Google Finance with 4 decimal places into google spreadsheet? 使用 Google 表格进行每日股票价格警报的 GAS 脚本 - GAS script for Daily Stock Price Alert with Google Sheets Google 表格中的 WTI 价格 - WTI price in Google Sheets 使用 Google Apps 脚本提取当前价格变化百分比(Google 财经) - Pulling current price change % (Google Finance) with Google Apps Script 如何从 coinmarketcap 获取 google 表上加密货币的当前价格? - How to get the current price of a cryptocurrency on google sheets from coinmarketcap? 谷歌金融货币上的谷歌表格数组 Function - Google Sheets Array Function over Google Finance Currencies Google 表格中有关货币汇率的 Google Finance 问题 - Issue with Google Finance in Google Sheets for Currency Exchange Rates 谷歌表格,更新信件库存数量 - Google Sheets, Updating Letter Stock Quantity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM