简体   繁体   English

从响应JavaScript获取JSON数据

[英]Getting JSON data from respons javascript

I'm working with the google-sheets-api to get data from a google spreadsheet. 我正在使用google-sheets-api从Google电子表格中获取数据。 And I want to use the api data I get in my console and use it in my website/webdashboard. 我想使用从控制台获取的api数据,并在我的网站/网络仪表板中使用它。 And I'm wondering how I can fetch this data from the console (or any other place where this data also resides) 而且我想知道如何从控制台(或该数据所在的任何其他地方)获取此数据。

I get this result in my console: 我在控制台中得到以下结果:

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

I've searched for JSON GET in files and for online api's. 我已经在文件和在线api中搜索了JSON GET。 Looked at the google-sheets-api documentation for a solution. 查看了google-sheets-api文档以获取解决方案。

Looked at other stack-overflow question where they put the data in a file locally, but this seems like a workaround. 看了其他将数据放在本地文件中的堆栈溢出问题,但这似乎是一种解决方法。 I should be able to get the data directly from api-pull. 我应该能够直接从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>

I should be able to do something with this line of code no? 我应该可以使用此行代码来执行某些操作吗?

ex: response.result.array.0.1 = given cell in the sheet. 例如: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);
      });

As I don't find any info on how this works or on how to search for this i'm unable to give an example of how I should achieve this. 由于我找不到有关此工作原理或如何搜索此问题的任何信息,因此我无法举一个我应如何实现此目标的示例。

The data of the result is in response.result.values which is an array of arrays. 结果数据在response.result.values ,它是一个数组数组。 To get the value of cell B1 use response.result.values[1][0] (A=0, B=1, C=2, ...; subtract 1 from the numeric cell index because the first element in an array was index 0) 若要获取单元格B1的值,请使用response.result.values[1][0] (A = 0,B = 1,C = 2,...;从数字单元格索引中减去1,因为数组中的第一个元素是索引0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM