javascript - 数组中的 Google 电子表格

标签 javascript arrays google-sheets-api google-api-js-client

如何将收集到的值存储在数组中?

我已经可以收集值,但我不知道如何存储它们

function onGAPILoad() {
gapi.client.init({
  // Don't pass client nor scope as these will init auth2, which we don't want
  apiKey: API_KEY,
  discoveryDocs: DISCOVERY_DOCS,
}).then(function () {
  console.log('gapi initialized')
  chrome.identity.getAuthToken({interactive: true}, function(token) {
    gapi.auth.setToken({
      'access_token': token,
    });
    gapi.client.sheets.spreadsheets.values.batchGet({
      spreadsheetId: SPREADSHEET_ID,
      majorDimension: "COLUMNS",
        ranges: [
            "A2:A"
        ]

    }).then(function(values) {
         console.log(values)
    });
  })

}, function(error) {
  console.log('error', error)
});
}

控制台响应:

{result: {…}, body: "{↵  "spreadsheetId": "1f99uX3zCeqF5Nlu4LJVQ_uEd9T4…↵          "BC639"↵        ]↵      ]↵    }↵  ]↵}↵", headers: {…}, status: 200, statusText: null}result: {spreadsheetId: "1f99uX3zCeqF5Nlu4LJVQ_uEd9T4Mao8r32eAHsijnjw", valueRanges: Array(1)}spreadsheetId: "1f99uX3zCeqF5Nlu4LJVQ_uEd9T4Mao8r32eAHsijnjw"valueRanges: [{…}]0: {range: "main!A2:A1000", majorDimension: "COLUMNS", values: Array(1)}length: 1__proto__: Array(0)__proto__: constructor: ƒ Object()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()hasOwnProperty: ƒ hasOwnProperty()arguments: (...)caller: (...)length: 1name: "hasOwnProperty"__proto__: ƒ ()[[Scopes]]: Scopes[0]__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toString: ƒ toString()valueOf: ƒ valueOf()toLocaleString: ƒ toLocaleString()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()body: "{↵  "spreadsheetId": "1f99uX3zCeqF5Nlu4LJVQ_uEd9T4Mao8r32eAHsijnjw",↵  "valueRanges": [↵    {↵      "range": "main!A2:A1000",↵      "majorDimension": "COLUMNS",↵      "values": [↵        [↵          "TC123",↵          "BC632",↵          "TC124",↵          "BC633",↵          "TC125",↵          "BC634",↵          "TC126",↵          "BC635",↵          "TC127",↵          "BC636",↵          "TC128",↵          "BC637",↵          "TC129",↵          "BC638",↵          "TC130",↵          "BC639"↵        ]↵      ]↵    }↵  ]↵}↵"headers: cache-control: "private"content-encoding: "gzip"content-length: "230"content-type: "application/json; charset=UTF-8"date: "Sat, 29 Feb 2020 18:16:00 GMT"server: "ESF"vary: "Origin, X-Origin, Referer"__proto__: Objectstatus: 200statusText: null__proto__: Object

最佳答案

  • 您想要从 gapi.client.sheets.spreadsheets.values.batchGet() 检索值。
  • 您想要以数组形式检索值。
  • 您已经能够通过 gapi.client.sheets.spreadsheets.values.batchGet() 使用 Sheets API 检索值。

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。

模式 1:

在此模式中,检索到的值在 gapi.client.init() 中以数组形式检索。

修改后的脚本:

unction onGAPILoad() {
  gapi.client.init({
    // Don't pass client nor scope as these will init auth2, which we don't want
    apiKey: API_KEY,
    discoveryDocs: DISCOVERY_DOCS,
  }).then(function () {
    console.log('gapi initialized')
    chrome.identity.getAuthToken({interactive: true}, function(token) {
      gapi.auth.setToken({
        'access_token': token,
      });
      gapi.client.sheets.spreadsheets.values.batchGet({
        spreadsheetId: SPREADSHEET_ID,
        majorDimension: "COLUMNS",
          ranges: ["A2:A"]
      }).then(function(values) {
        const ar = values.result.valueRanges[0].values;  // <--- Modified
        console.log(ar);  // <--- Modified
      });
    })
  }, function(error) {
    console.log('error', error)
  });
}
  • 您可以使用 const ar =values.result.valueRanges[0].values 检索值。

模式 2:

在此模式中,检索到的值将作为参数发送到其他函数。

修改后的脚本:

function onGAPILoad() {
  gapi.client.init({
    // Don't pass client nor scope as these will init auth2, which we don't want
    apiKey: API_KEY,
    discoveryDocs: DISCOVERY_DOCS,
  }).then(function () {
    console.log('gapi initialized')
    chrome.identity.getAuthToken({interactive: true}, function(token) {
      gapi.auth.setToken({
        'access_token': token,
      });
      gapi.client.sheets.spreadsheets.values.batchGet({
        spreadsheetId: SPREADSHEET_ID,
        majorDimension: "COLUMNS",
          ranges: ["A2:A"]
      }).then(function(values) {
        getValues(values);
      });
    })
  }, function(error) {
    console.log('error', error)
  });
}

function getValues(e) {
  const values = e.result.valueRanges[0].values;
  console.log(values);
}
  • 您可以通过getValues函数检索值。

模式 3:

在此模式中,检索到的值可以在 onGAPILoad 函数中使用。

修改后的脚本:

async function onGAPILoad() {
  const getValues = () => {
    return new Promise((resolve, reject) => {
      gapi.client.init({
        // Don't pass client nor scope as these will init auth2, which we don't want
        apiKey: API_KEY,
        discoveryDocs: DISCOVERY_DOCS,
      }).then(function () {
        console.log('gapi initialized')
        chrome.identity.getAuthToken({interactive: true}, function(token) {
          gapi.auth.setToken({
            'access_token': token,
          });
          gapi.client.sheets.spreadsheets.values.batchGet({
            spreadsheetId: SPREADSHEET_ID,
            majorDimension: "COLUMNS",
              ranges: ["A2:A"]
          }).then(function(values) {
            resolve(values);
          });
        })
      }, function(error) {
        reject(error);
      });
    });
  }

  const res = await getValues().catch(e => console.log(e));
  const values = res.result.valueRanges[0].values;
  console.log(values);
}
  • 您可以检索最后一行的值。

注意:

  • 您的请求中仅使用了一个范围。所以我使用了 res.result.valueRanges[0].values。当您想要使用多个范围时,请使用循环从 res.result.valueRanges 检索值。

引用:

如果我误解了您的问题并且这些不是您想要的方向,我深表歉意。

关于javascript - 数组中的 Google 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60468098/

相关文章:

javascript - 将应用程序 URL 从 http 重定向到 https 后,自定义光标图像不起作用

javascript - Google 站点中 HTML Box 的大小

c# - 序列数组

python - 如何使用 google API for python 在特定文件夹下创建工作表?

javascript - 如何有条件地在 ng 样式中应用动态值?

javascript - 过滤 <td> 元素中带有空格的匹配文本

在 C 中将字符串转换为字符串数组

python - 将 numpy.array a 与 numpy.array b 的前导维度相乘

google-sheets - 使用 Sheets API v4 (Java) 获取 Google Sheets 上次编辑日期

google-apps-script - 获取 "people chip"的电子邮件地址?