简体   繁体   English

使用 v 4 API 从公共 Google 电子表格中获取单元格值

[英]Get a cell value from a public Google spreadsheet using v 4 API

I'm using this as a sample - https://docs.google.com/spreadsheets/d/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/edit#gid=0我将此用作示例 - https://docs.google.com/spreadsheets/d/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/edit#gid=0

How do I get a cell value, say A2 form this using API v4?如何获取单元格值,例如使用 API v4 形成的 A2?

 function myCallback(spreadsheetdata) { console.log(spreadsheetdata); // spreadsheetdata.A2 }
 <script src="https://spreadsheets.google.com/feeds/cells/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/od6/public/values?alt=json-in-script&callback=myCallback"></script>

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the value of the cell "A2" from the Google Spreadsheet of https://docs.google.com/spreadsheets/d/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/edit#gid=0 using Javascript.您想从https://docs.google.com/spreadsheets/d/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/edit#gid=0使用 Z9E13Z0269D1D24DA93271 的 Google 电子表格中检索单元格“A2”的值。

For this, how about this answer?为此,这个答案怎么样?

When the Spreadsheet is not publicly shared and not published to Web, the access token retrieved by OAuth2 and/or the service account is required to be used.当电子表格未公开共享且未发布到 Web 时,需要使用 OAuth2 检索到的访问令牌和/或服务帐户。 In this case, the script becomes a bit complicated.在这种情况下,脚本变得有点复杂。 But when I saw your Spreadsheet, it seems that the Spreadsheet is shared publicly and published to Web.但是当我看到您的电子表格时,似乎电子表格已公开共享并发布到 Web。 In this case, the value can be retrieved without the access token.在这种情况下,可以在没有访问令牌的情况下检索该值。 So the script becomes simpler.所以脚本变得更简单了。

Pattern 1:模式一:

In this pattern, the value of cell "A2" is retrieved without the authorization.在此模式中,未经授权就检索单元格“A2”的值。

Sample script:示例脚本:

const url = "https://docs.google.com/spreadsheet/pub?key=1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA&range=A2&output=csv";
fetch(url)
.then(response => response.text())
.then(text => {
  console.log(text)
});

or或者

const url = "https://docs.google.com/spreadsheet/pub?key=1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA&output=csv";
fetch(url)
.then(response => response.text())
.then(text => {
  const ar = text.split("\n").map(r => r.split(","));
  console.log(ar[2][0]);
});
  • When above scripts are used for your Spreadsheet, a of the cell "A2" is returned.当上述脚本用于您的电子表格时, a

Pattern 2:模式二:

In this pattern, the value of cell "A2" is retrieved using the method of spreadsheets.values.get in Sheets API with the API key.在此模式中,使用表格 API 中的电子表格.values.get 方法检索单元格“A2”的值,并使用 API 键。 In this case, please retrieve your API key, and set to apiKey .在这种情况下,请检索您的 API 密钥,并设置为apiKey

Sample script:示例脚本:

<script async defer src="https://apis.google.com/js/api.js" onload="handleClientLoad()"></script>
<script>
function handleClientLoad() {
  const apiKey = "###";  // Please set your API key.

  gapi.load('client', () => {
    gapi.client.setApiKey(apiKey);
    gapi.client.load('sheets', 'v4', () => {
      gapi.client.sheets.spreadsheets.values.get({
        spreadsheetId: "1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA",
        range: 'Sheet1!A2',
      }).then(res => {
        const value = res.result.values[0][0];
        console.log(value);
      });
    });
  });
}
</script>
  • When above script is used for your Spreadsheet, a of the cell "A2" is returned.当上述脚本用于您的电子表格时, a

References:参考:

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

相关问题 无法使用谷歌电子表格 api v4.0 更新单元格的谷歌工作表值 - Unable to update google sheet value of a cell using google spreadsheet api v4.0 如何在提交表单时从Google电子表格的单元格中获取有效值? - How to get the active value from a cell in a Google spreadsheet on form submission? Google Sheets API:从公共工作表中读取单个单元格值 - Google Sheets API: Read single cell value from public sheet 是否可以使用 JavaScript 从 Excel 电子表格中获取特定的单元格值? - Is it possible to get a specific cell value from an Excel spreadsheet using JavaScript? 如何使用Google可视化API检索Google电子表格单元格值? - How to use Google visualization API to retrieve google spreadsheet cell value? 我可以从网络服务值中填充谷歌电子表格单元格吗? - Can I fill a google spreadsheet cell from a webservice Value? 从google docs电子表格获取单元格的价值到javascript - Getting value of a cell from google docs spreadsheet into javascript 如何使用Google App脚本中的JavaScript从Google电子表格数据库中获取日期值 - How do i get the date value from google spreadsheet database using javascript in google app script 尝试使用Google API v3从地址获取位置 - trying to get location from address using google api v3 在 Chrome 扩展程序中使用 Google API Spreadsheet JS 删除值 - Remove value using Google API Spreadsheet JS in Chrome Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM