[英]How to parse JSON in Google Apps Script for Google Spreadsheet
我想从 Bittrex API 中提取数据。 我得到了数据,但我找不到在我的电子表格上显示它们的方法。 错误不断出现,这让我很生气!
我敢肯定这真的很简单,但我放弃了:)
这是代码:
function main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var balancesSheet = ss.getSheetByName("BITTREX 2");
var balancesRange = balancesSheet.getRange("A1:X");
var balancesListValues = balancesRange.getValues();
// Add those values to the active sheet in the current
// spreadsheet. This overwrites any values that already
// exist there.
balancesSheet.getRange(1, 1, balancesRange.getHeight(), balancesRange.getWidth())
.setValues(balancesListValues);
var apikey = 'My APY Key is here'; // Please input your key.
var apisecret = 'My API Secret is here'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var response = UrlFetchApp.fetch(uri, params);
var getContext= response.getContentText();
var parsedata = JSON.parse(getContext);
//ss.getRange(1,1).setValue(parsedata.foreach)
parsedata.data.forEach(function(result, index) {
balancesListValues.getRange(+3, 1).setValue(result.quantity)
balancesListValues.getRange(+3, 2).setValue(result.price)
})
Logger.log(parsedata.result);
}
这是错误:
TypeError: Cannot read property 'forEach' of undefined on line 31 (this one: parsedata.data.forEach(function(result, index))
我的目标是在一个漂亮的电子表格中转换原始 JSON 数据。 我愿意接受建议。 我用大量的复制/粘贴来做到这一点,所以不要打扰整洁!
谢谢你的帮助,NiphtiAe。
编辑
太棒了:我以该代码结束,以更新数据而不是成行堆积:
function main() {
// Retrieve values from API.
var apikey = '*********'; // Please input your key.
var apisecret = '**********'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/account/getbalances?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var response = UrlFetchApp.fetch(uri, params);
var getContext= response.getContentText();
var parsedata = JSON.parse(getContext);
console.log(getContext) // Here, the response value can be confirmed at the log.
// Use Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var balancesSheet = ss.getSheetByName("BITTREX 2");
var balancesRange = balancesSheet.getRange("A1:X");
var balancesListValues = balancesRange.getValues();
balancesSheet.getRange(1, 1, balancesRange.getHeight(), balancesRange.getWidth()).setValues(balancesListValues);
var values = parsedata.result.map(({Currency, Available}) => [Currency, Available]);
balancesSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
只需要从 CMC 或 Coingecko 或其他地方拨打价格,瞧!
非常感谢@Tanaike!
TypeError: Cannot read property 'forEach' of undefined on line 31 (this one: parsedata.data.forEach(function(result, index))
,在这种情况下,我认为parsedata.data
可能会返回null
。因为当data
的属性作为数组以外的值存在时,就会出现TypeError: parsedata.data.forEach is not a function
类的错误。
Quantity
和Price
的值包含在result
数组中。 参考我认为这可能是您的问题的原因。Quantity
和Price
的关键似乎不是quantity
和price
。balancesListValues
是从var balancesListValues = balancesRange.getValues()
检索到的二维数组。 所以当balancesListValues.getRange(+3, 1).setValue(result.quantity)
运行时,会出现错误。setValue
在循环中使用。 在这种情况下,处理成本会变高。当以上几点反映到您的脚本时,它变成如下。
function main() {
// Retrieve values from API.
var apikey = 'My APY Key is here'; // Please input your key.
var apisecret = 'My API Secret is here'; // Please input your secret.
var nonce = Number(new Date().getTime() / 1000).toFixed(0);
var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
var params = {
method: "get",
headers: {Apisign: sign}
}
var response = UrlFetchApp.fetch(uri, params);
var getContext= response.getContentText();
var parsedata = JSON.parse(getContext);
console.log(getContext) // Here, the response value can be confirmed at the log.
// Use Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var balancesSheet = ss.getSheetByName("BITTREX 2");
var balancesRange = balancesSheet.getRange("A1:X");
var balancesListValues = balancesRange.getValues();
balancesSheet.getRange(1, 1, balancesRange.getHeight(), balancesRange.getWidth()).setValues(balancesListValues);
var values = parsedata.result.map(({Quantity, Price}) => [Quantity, Price]);
balancesSheet.getRange(balancesSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
console.log(getContext)
的示例值? 通过这个,我想检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.