【问题标题】:How can i get data from a JSON array in google app script?如何从谷歌应用脚​​本中的 JSON 数组获取数据?
【发布时间】:2020-04-11 16:03:57
【问题描述】:

我正在向一个成功的 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: [] 
   } ]
}

我想访问用户、项目、标签、客户端、开始、结束和描述,以便我可以将其放在我的电子表格中。我该怎么做?

这就是我执行请求的方式以及我尝试访问变量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);
  }

【问题讨论】:

  • 1. 改为提供console.log(togglReport)console.log(JSON.stringify(togglReport))。记录器不提供准确的日志(= 不是有效的 json;应引用字符串)。 2. 确保你的 json 是有效的。您可以通过更改而不是添加... 来隐藏敏感细节。 3. 您是否尝试过将数据塑造成二维数组,即 Google 表格的setValues 要求的格式?
  • 例如见this question
  • 是什么让你这么想? console.log 多年来一直在应用程序脚本中得到支持。
  • 因为console.log 在我输入脚本时从未打印任何内容,所以我不知道是否需要做其他事情才能使其正常工作,这就是为什么当我在 GAS 中编程时我避免在我的脚本中使用console.log 并使用Logger.log 而不是@TheMaster
  • @TheMaster 我只有一个月的 GAS 编程,我对这方面很陌生,你能看到我更新的问题吗?

标签: javascript google-apps-script


【解决方案1】:

Range.setValues() 用于将数据设置为二维数组到工作表。使用destructuring assignmentfor...of loop,可以将数据塑造成二维数组。

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);

【讨论】:

  • 多谢不便
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
相关资源
最近更新 更多