[英]Assign latest date (from API) to identifier (in spreadsheet) using Google Apps Script
I have a Google Sheets spreadsheet with a list of identifiers in the first column (eg COLTS or RAMS). 我有一个Google表格电子表格,第一列中包含标识符列表(例如COLTS或RAMS)。 Using Google Apps Script I then fetch transaction data for these identifiers from an API (multiple transactions per identifier).
然后,我使用Google Apps脚本从API中获取这些标识符的交易数据(每个标识符多次交易)。 That data looks like this:
该数据如下所示:
{
"sportsfolio_transactions":[
{
"trans_date":"06/26/2017 9:58:00 AM",
"amount":-10.30,
"trans_type_id":1,
"trans_type_name":"Buy",
"stock_id":"COLTS",
"shares":3
},
{
"trans_date":"04/25/2017 2:58:00 AM",
"amount":-3.22,
"trans_type_id":1,
"trans_type_name":"Buy",
"stock_id":"COLTS",
"shares":1
}
{
"trans_date":"05/20/2017 2:58:00 AM",
"amount":-30.22,
"trans_type_id":1,
"trans_type_name":"Buy",
"stock_id":"RAMS",
"shares":9
}]}
The code I have so far filters for buy transactions and looks like this: 到目前为止,我拥有用于购买交易的过滤器的代码如下:
var response = UrlFetchApp.fetch(url, options);
var json = response.getContentText();
var data = JSON.parse(json);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('apidata'); active spreadsheet
var output = [];
data["sportsfolio_transactions"].forEach(function(elem,i) {
output.push([elem["stock_id"],elem["shares"],elem["trans_type_name"],elem["trans_date"],elem["amount"]]);
});
var outputbuy = [];
data["sportsfolio_transactions"].forEach(function(element,i) {
if(element["trans_type_name"] == "Buy") {
outputbuy.push([element["stock_id"],element["shares"],element["trans_type_name"],element["trans_date"],element["amount"]]);
}
});
Now I would like to put the date of the latest transaction next to each identifier. 现在,我想将最新交易的日期放在每个标识符旁边。 Eg according to the above data, next to COLTS should be the date 06/26/2017
例如,根据上述数据,COLTS旁边的日期应为06/26/2017
After hours of trying (math.max, sorting, etc.) I'm out of ideas and hope that you can help me out here. 经过数小时的尝试(math.max,排序等),我失去了主意,希望您能在这里为我提供帮助。 Oh, and if you see any possible improvements for the posted code, of course that is also much appreciated.
哦,如果您发现所发布代码的任何可能的改进,当然也非常感谢。
Managed to get the problem solved, sort of. 设法使问题得以解决。 Can only look into "Buy" transactions, was not able to get "Buy" ||
只能查看“购买”交易,却无法获得“购买” "Sell" to work properly.
“出售”即可正常工作。
Also, this code seems awefully inefficient, but it's the best I was able to produce. 另外,这段代码似乎效率很低,但是这是我能产生的最好的代码。 Any ideas on how to make it more efficient?
关于如何提高效率的任何想法?
Here's the code: 这是代码:
function getLastTrade() {
var url= 'https://asmwebdev1.azurewebsites.net/api/get_sportsfolio_transactions/'
var today = new Date()
var enddate = Utilities.formatDate(today,"GMT", "MM/dd/YYYY");
var formData = {
'email': 'xxxxx',
'password': 'x',
'sportsfolio_key': '9657',
'start_date': '01/01/2017',
'end_date': enddate,
};
var options = {
'method' : 'post',
'payload' : formData
};
var response = UrlFetchApp.fetch(url, options);
var json = response.getContentText();
var data = JSON.parse(json);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('apidata');
var Avals = ss.getRange("A2:A50").getValues();
var Alast = Avals.filter(String).length;
for(var row=2;row<Alast+2;row++){
var oldestdate = new Date(0)
data["sportsfolio_transactions"].forEach(function(element,i) {
var lastid = sheet.getRange(row, 1).getValues();
if(element["stock_id"] == lastid && element["trans_type_name"] == "Buy"){
var newestdate = new Date(element["trans_date"])
if(newestdate>oldestdate) {
oldestdate = newestdate
}
sheet.getRange(row,6).setValue(oldestdate)
}
});
}
}
The data that the API returns contains hundreds of transactions, a small sample can be seen in the post above. API返回的数据包含数百个事务,可以在上面的帖子中看到一个小的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.