从响应JavaScript获取JSON数据

时间:2019-04-03 14:35:17

标签: javascript json google-sheets-api

我正在使用google-sheets-api从google电子表格中获取数据。我想使用从控制台获取的api数据,并在我的网站/网络仪表板中使用它。而且我想知道如何从控制台(或该数据所在的任何其他地方)获取此数据

我在控制台中得到以下结果:

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) ["", "",

我已经搜索了文件中的JSON GET和在线api。 查看了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);
      });

由于我找不到有关它如何工作或如何搜索的任何信息,因此无法举一个我应该如何实现此目标的示例。

1 个答案:

答案 0 :(得分:1)

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