Google 个电子表格数组

Google spreadsheets in array

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

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

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() 中检索值。
  • 您想以数组形式检索值。
  • 您已经能够使用表格 API 和 gapi.client.sheets.spreadsheets.values.batchGet().
  • 检索值

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

模式 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 检索值。

模式二:

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

修改后的脚本:

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 中检索值。

参考:

如果我误解了你的问题并且这些不是你想要的方向,我很抱歉。