【问题标题】:Getting JSON data from respons javascript从响应 javascript 中获取 JSON 数据
【发布时间】:2019-04-03 14:35:16
【问题描述】:

我正在使用 google-sheets-api 从 google 电子表格中获取数据。我想使用我在控制台中获得的 api 数据并在我的网站/webdashboard 中使用它。我想知道如何从控制台(或任何其他也存在这些数据的地方)获取这些数据

我在控制台中得到这个结果:

Object
spreadsheetId: "1dbRAXK6EVbSIFWB5mKleh2ziutwavrRLvs07uoBPJbk"
valueRanges: Array(1)
0:
majorDimension: "ROWS"
range: "'ma1'!A1:J38"
values: (38) [Array(10), Array(10), Array(10), Array(9), Array(9), Array(7), Array(7), Array(9), Array(7), Array(10), Array(10), Array(9), Array(9), Array(9), Array(9), Array(8), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9)]
__proto__: Object
length: 1
__proto__: Array(0)
__proto__: Object
temp1
(38) [Array(10), Array(10), Array(10), Array(9), Array(9), Array(7), Array(7), Array(9), Array(7), Array(10), Array(10), Array(9), Array(9), Array(9), Array(9), Array(8), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9), Array(9)]
0: (10) ["maandag 1 april 2019", "", "", "", "", "", "", "", "", "week IV"]
1: (10) ["kafee", "naam", "functie", "van", "tot", "van", "tot", "Subtotaal", "totaal", "Info"]
2: (10) ["", "Ine", "", "9:30", "", "", "18:00", "8,5", "8", "Reservaties/opmerkingen"]
3: (9) ["", "Michiel", "", "9:30", "", "", "17:30", "8", "7,5"]
4: (9) ["", "Nayimi", "BT", "12:00", "", "", "21:00", "9", "8,5"]
5: (7) ["", "Gijs", "", "12:00", "", "", "18:00"]
6: (7) ["", "Flavio", "", "12:00", "", "", "21:00"]
7: (9) ["", "", "", "", "", "", "", "0", "0"]
8: (7) ["", "Tim", "V", "9:30", "", "", "18:00"]
9: (10) ["", "Griet", "V", "9:30", "", "", "18:00", "8,5", "8", "Activiteiten/voorstellingen"]
10: (10) ["", "Silvia", "V", "9:30", "", "", "18:00", "8,5", "8", "Gent Quizt"]
11: (9) ["", "Roeland", "", "17:30", "", "", "2:00", "8,5", "8"]
12: (9) ["", "Hannah", "", "17:30", "", "", "2:00", "8,5", "8"]
13: (9) ["", "Ali", "", "17:30", "", "", "2:00", "8,5", "8"]
14: (9) ["", "Emma", "", "18:00", "", "", "2:00", "8", "7,5"]
15: (8) ["", "", "", "", "", "", " ", "#VALUE!"]
16: (9) ["", "", "", "", "", "", "", "0", "0"]
17: (9) ["", "", "", "", "", "", "", "0", "0"]
18: (9) ["", "", "", "", "", "", "", "0", "0"]
19: (9) ["", "", "", "", "", "", "", "0", "0"]
20: (9) ["balzaal", "", "perm.", "", "", "", "", "0", "0"]
21: (9) ["", "", "", "", "", "", "", "0", "0"]
22: (9) ["", "", "", "", "", "", "", "0", "0"]
23: (9) ["", "", "", "", "", "", "", "0", "0"]
24: (9) ["", "", "", "", "", "", "", "0", "0"]
25: (9) ["", "", "", "", "", "", "", "0", "0"]
26: (9) ["", "", "", "", "", "", "", "0", "0"]
27: (9) ["concertzaal", "", "perm.", "", "", "", "", "0", "0"]
28: (9) ["", "", "", "", "", "", "", "0", "0"]
29: (9) ["", "", "", "", "", "", "", "0", "0"]
30: (9) ["", "", "", "", "", "", "", "0", "0"]
31: (9) ["", "", "", "", "", "", "", "0", "0"]
32: (9) ["", "", "", "", "", "", "", "0", "0"]
33: (9) ["", "", "", "", "", "", "", "0", "0"]
34: (9) ["majolica", "", "perm.", "", "", "", "", "0", "0"]
35: (9) ["", "", "", "", "", "", "", "0", "0"]
36: (9) ["", "", "", "", "", "", "", "0", "0"]
37: (9) ["", "",

我在文件和在线 api 中搜索了 JSON GET。 查看了 google-sheets-api 文档以获取解决方案。

查看了其他堆栈溢出问题,他们将数据放在本地文件中,但这似乎是一种解决方法。我应该可以直接从 api-pull 获取数据。

<html>
  <head></head>
  <body>
    <!--
    BEFORE RUNNING:
    ---------------
    1. If not already done, enable the Google Sheets API
       and check the quota for your project at
       https://console.developers.google.com/apis/api/sheets
    2. Get access keys for your application. See
       https://developers.google.com/api-client-library/javascript/start/start-js#get-access-keys-for-your-application
    3. For additional information on authentication, see
       https://developers.google.com/sheets/api/quickstart/js#step_2_set_up_the_sample
    -->
    <script>
    function makeApiCall() {
      var params = {
        // The ID of the spreadsheet to retrieve data from.
        spreadsheetId: 'my-spreadsheet-id',  // TODO: Update placeholder value.

        // The A1 notation of the values to retrieve.
        range: 'my-range',  // TODO: Update placeholder value.

        // How values should be represented in the output.
        // The default render option is ValueRenderOption.FORMATTED_VALUE.
        valueRenderOption: '',  // TODO: Update placeholder value.

        // How dates, times, and durations should be represented in the output.
        // This is ignored if value_render_option is
        // FORMATTED_VALUE.
        // The default dateTime render option is [DateTimeRenderOption.SERIAL_NUMBER].
        dateTimeRenderOption: '',  // TODO: Update placeholder value.
      };

      var request = gapi.client.sheets.spreadsheets.values.get(params);
      request.then(function(response) {
        // TODO: Change code below to process the `response` object:
        console.log(response.result);
      }, function(reason) {
        console.error('error: ' + reason.result.error.message);
      });
    }

    function initClient() {
      var API_KEY = '';  // TODO: Update placeholder with desired API key.

      var CLIENT_ID = '';  // TODO: Update placeholder with desired client ID.

      // TODO: Authorize using one of the following scopes:
      //   'https://www.googleapis.com/auth/drive'
      //   'https://www.googleapis.com/auth/drive.file'
      //   'https://www.googleapis.com/auth/drive.readonly'
      //   'https://www.googleapis.com/auth/spreadsheets'
      //   'https://www.googleapis.com/auth/spreadsheets.readonly'
      var SCOPE = '';

      gapi.client.init({
        'apiKey': API_KEY,
        'clientId': CLIENT_ID,
        'scope': SCOPE,
        'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
      }).then(function() {
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
        updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
      });
    }

    function handleClientLoad() {
      gapi.load('client:auth2', initClient);
    }

    function updateSignInStatus(isSignedIn) {
      if (isSignedIn) {
        makeApiCall();
      }
    }

    function handleSignInClick(event) {
      gapi.auth2.getAuthInstance().signIn();
    }

    function handleSignOutClick(event) {
      gapi.auth2.getAuthInstance().signOut();
    }
    </script>
    <script async defer src="https://apis.google.com/js/api.js"
      onload="this.onload=function(){};handleClientLoad()"
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
    <button id="signin-button" onclick="handleSignInClick()">Sign in</button>
    <button id="signout-button" onclick="handleSignOutClick()">Sign out</button>
  </body>
</html>

我应该可以用这行代码做点什么?

例如:response.result.array.0.1 = 工作表中的给定单元格。

      var request = gapi.client.sheets.spreadsheets.values.get(params);
      request.then(function(response) {
        // TODO: Change code below to process the `response` object:
        console.log(response.result);
      }, function(reason) {
        console.error('error: ' + reason.result.error.message);
      });

由于我没有找到任何关于它如何工作或如何搜索它的信息,我无法举例说明我应该如何实现这一目标。

【问题讨论】:

  • 你到底在问什么......如何将这些数据转换成 html 表格?

标签: javascript json google-sheets-api


【解决方案1】:

结果的数据在response.result.values 中,这是一个数组数组。要获取单元格 B1 的值,请使用 response.result.values[1][0] (A=0, B=1, C=2, ...; 从数字单元格索引中减去 1,因为数组中的第一个元素是索引 0)

【讨论】:

  • 确实我知道我拥有它们,但无法按我的意愿使用它们。我很高兴我在正确的对象上做对了 - (response.result)。谢谢!
  • 对不起,我误会了。请参阅我的新答案。
  • 正是我需要的。我得到了你的第一个答案的结果。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2017-07-02
  • 2022-06-28
相关资源
最近更新 更多