使用Google Script在Google表格中阅读JSON

时间:2018-03-13 21:27:29

标签: javascript json google-sheets-api

我目前正在将外部API中的数据提取到Google表格,并希望从JSON中获取某些数据。应添加到单元格的唯一部分是$440.00,但目前显示{"data":[{"cost":"$440.00"}],"success":true,"totalNumRows":1}。有没有办法拉这个?

// Add menu to run program
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Get Crobo Cost')
    .addItem('Display daily cost', 'callCrobo')
    .addToUi();
}


// Function to call Crobo
function callCrobo() {
// Call Crobo daily spend
var apiKey = '****omitted****'

var response = UrlFetchApp.fetch("http://cis.crobo.com/stats/stats.json?api_key=" + apiKey + "&start_date=2018-03-12&end_date=2018-03-12&field[]=Stat.revenue")
Logger.log(response.getContentText());

// Parse to JSON reply
var json = response.getContentText();
var obj = JSON.parse(json);

Logger.log(obj.data);

//Gets hold of ActiveSheet in current workbook
var sheet = SpreadsheetApp.getActiveSheet();

// Set value in cell A1
sheet.getRange(sheet.getLastRow() + 1,1).setValue([response]);
}

记录器的当前输出为:

[18-03-13 17:22:02:377 EDT] {"data":
[{"cost":"$440.00"}],"success":true,"totalNumRows":1}
[18-03-13 17:22:02:378 EDT] undefined

谢谢

0 个答案:

没有答案