我正在向成功的API发出请求,但我需要获取返回的数组数据,下面我将输入数组的外观,以便您可以帮助我提取数据

{ total_grand: 30600000,
  total_billable: null,
  total_currencies: [ { currency: null, amount: null } ],
  total_count: 5,
  per_page: 50,
  data:
   [ { id: 13998122,
       pid: 1570982183,
       tid: null,
       uid: 5386231,
       description: 'Finish the first part of the RCP mockup',
       start: '2020-03-26T13:00:00-04:00',
       end: '2020-03-26T16:00:00-04:00',
       updated: '2020-04-02T13:25:15-04:00',
       dur: 10800000,
       user: 'Jose',
       use_stop: true,
       client: 'PLA',
       project: 'Training',
       project_color: '0',
       project_hex_color: '#3750b5',
       task: null,
       billable: null,
       is_billable: false,
       cur: null,
       tags: []
   } ]
}


我想访问用户,项目,标签,客户端,开始,结束和描述,因此可以将其放在SpreadSheet中。我怎样才能做到这一点?

这是我执行请求的方式以及尝试访问变量togglData中数组中数据的方式

for (var i = 0; i < projects.length; i++) {
    var listProjects = projects[i];
    var reportURL = baseURL + '/reports/api/v2/details' + params;
    var reportFetch = UrlFetchApp.fetch(reportURL, options);
    var togglReport = JSON.parse(reportFetch.getContentText());
    var togglData = togglReport["data"]["user"];
    Logger.log(togglReport);
  }

最佳答案

Range.setValues()用于将数据设置为图纸的二维数组。使用destructuring assignmentfor...of loop,可以将数据模制成2D数组。



const togglReport = {
  total_grand: 30600000,
  total_billable: null,
  total_currencies: [{ currency: null, amount: null }],
  total_count: 5,
  per_page: 50,
  data: [
    {
      id: 13998122,
      pid: 1570982183,
      tid: null,
      uid: 5386231,
      description: 'Finish the first part of the RCP mockup',
      start: '2020-03-26T13:00:00-04:00',
      end: '2020-03-26T16:00:00-04:00',
      updated: '2020-04-02T13:25:15-04:00',
      dur: 10800000,
      user: 'Jose',
      use_stop: true,
      client: 'PLA',
      project: 'Training',
      project_color: '0',
      project_hex_color: '#3750b5',
      task: null,
      billable: null,
      is_billable: false,
      cur: null,
      tags: [],
    },
  ],
};
const out = [];
for (const {
  user,
  project,
  tags,
  client,
  start,
  end,
  description,
} of togglReport.data) {
  //We're looping over togglReport.data and not togglReport
  out.push([user, project, tags.join(), client, start, end, description]);
}
console.log(out);
//SpreadsheetApp.getActive().getSheets[0].getRange(1,1, out.length, out[0].length).setValues(out);

10-05 21:06
查看更多