一直在使用 Google 表格从 CoinGecko API 收集比特币历史数据。手动方法,我必须打开工作表并复制和“仅粘贴值”以将数据粘贴到历史列表中。左边是历史,右边是数据收集。如果可能的话,我想自动化这个过程。当前代码提取了 10 行尚未出现在历史记录中的数据。历史记录行以“BOTTOM”字段结束以指示页面底部。脚本编辑器已设置为在午夜运行以收集数据。这是我的示例: https ://docs.google.com/spreadsheets/d/1kAcVtF2x9ox7gNCt5liQdhApQpGaBw1kl4I8PjKMfx8/edit?usp=sharing
问问题
700 次
2 回答
0
Answer
You have to make use of the Sheet and Range classes.
Code
In order to automate that process add the following code to your existing Apps script:
function Pull_History_bitcoin() {
//PREVIOUS CODE
var days = parseInt(targetSheet.getRange("J4").getValue().toString());
if (days > 0) {
var range = targetSheet.getRange("G6:J" + (5+days)).getValues();
var lastRowHistory = targetSheet.getRange("G1").getValue();
var nextLastRowHistory = parseInt(lastRowHistory[1]) + days;
var bottomCell = targetSheet.getRange("A" + nextLastRowHistory);
bottomCell.setValue("BOTTOM");
var nextRange = targetSheet.getRange(`${lastRowHistory}:D` + (nextLastRowHistory - 1));
nextRange.setValues(range);
}
}
Where days define the number of entries after calling the external API.
Don't worry about the values rounded because they are just showing rounded, the current values are like the original ones.
Reference
于 2020-09-30T10:57:29.333 回答
0
在下面找到我的完整 google-apps-script 作为任何希望从免费 CoinGecko Api 服务获取加密历史信息的人的参考。
function Pull_History_bitcoin() {
var targetSS = SpreadsheetApp.getActive();
var targetSheet = targetSS.getSheetByName("bitcoin");
// To force an update
targetSheet.insertRowBefore(3);
targetSheet.deleteRow(3);
// Copy newly acquired history into the history columns
var days = parseInt(targetSheet.getRange("J4").getValue().toString());
if (days > 0) {
var range = targetSheet.getRange("G6:J" + (5+days)).getValues();
var lastRowHistory = targetSheet.getRange("G1").getValue();
//var nextLastRowHistory = parseInt(lastRowHistory[1],4) + days;
var nextLastRowHistoryStr = lastRowHistory.slice(1);
var nextLastRowHistory = Number(nextLastRowHistoryStr) + days;
var nextRange = targetSheet.getRange(`${lastRowHistory}:D` + (nextLastRowHistory - 1));
nextRange.setValues(range);
var bottomCell = targetSheet.getRange("A" + nextLastRowHistory);
bottomCell.setValue("BOTTOM");
}
}
于 2020-10-04T18:40:00.813 回答