[英]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.我相信你的目标如下。
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.
所以脚本变得更简单了。
In this pattern, the value of cell "A2" is retrieved without the authorization.在此模式中,未经授权就检索单元格“A2”的值。
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]);
});
a
of the cell "A2" is returned.a
。 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
。
<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>
a
of the cell "A2" is returned.a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.