Reputation: 177
I'm using this as a sample - 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 ?
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>
Upvotes: 1
Views: 3226
Reputation: 201438
I believe your goal as follows.
https://docs.google.com/spreadsheets/d/1hbHqrnYa4oMUquZcq8WkTJk0kI0t9scGBwro-EH-ALA/edit#gid=0
using Javascript.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. 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. 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.
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.In this pattern, the value of cell "A2" is retrieved using the method of spreadsheets.values.get in Sheets API with the API key. In this case, please retrieve your API key, and set to 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.Upvotes: 2