简体   繁体   English

使用Google Apps脚本将最新日期(从API)分配给标识符(在电子表格中)

[英]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.

相关问题 使用日期选择器输入Google Apps脚本的电子表格日期 - Google Apps Script for Spreadsheet date input using date picker 使用Apps脚本从Google电子表格添加到Google文档 - Adding to a Google Doc from a Google Spreadsheet Using Apps Script 将 json/csv 文件(从 API 获取)保存到电子表格文档而不使用循环 - Google Apps 脚本 - Save a json/csv file (fetched from an API) to a spreadsheet document without using a loop - Google Apps Script 使用Google Apps脚本作为服务从电子表格中检索数据 - Using Google Apps Script as service to retrieve data from spreadsheet 使用应用程序脚本将表格图表从谷歌电子表格发送到松弛 - Sending table charts from google spreadsheet to slack using apps script 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script Google Apps脚本未从电子表格中检索信息 - Google Apps Script not retrieving information from spreadsheet 在 Google Apps 脚本中从另一个电子表格编辑一个电子表格 - Editing one Spreadsheet from another Spreadsheet in Google Apps Script 从导入日期来自电子表格创建Google日历事件时出现setHours()错误 - Apps脚本 - setHours() Error when Creating a Google Calendar Event from Imported Date Coming From Spreadsheet - Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM