[英]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])]]);
}
D2:E2
- no matter what the actual edited range isD2:E2
- 无论实际编辑的范围是什么range.getValues()[0]
)range.getValues()[0]
)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
0
和1
列(例如D
和E
列)并将D
列的值分配回D
列(是否有必要?)并将两个值中较高的值分配给E
列 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 列做什么,但提供您的一般建议:
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])]]);
}
}
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.