Reputation: 626
So I was trying to read values from a spreadsheet in JS (not Node), and I came across https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get. This looks like the right thing for me, but what if I'm trying to read values from different sheets?
For example, how would I use spreadsheets.values.get to read A1:D1 in Sheet 2 in https://docs.google.com/spreadsheets/d/e/2PACX-1vQLrcAW_bL5HSVHorBJisdwv8S5_6Th9EP2wiYLmJKktj41uXVepUNOx4USNGdsVAuDOH_qknWs3pGa/pubhtml Or is there another method I'm missing out on?
Upvotes: 0
Views: 1883
Reputation: 201358
I believe your goal as follows.
A1:D1
in the sheet of Sheet2
.For this, how about this answer?
In this pattern, the function of makeApiCall()
of the sample script (Browser) of this official document is used.
var params = {
spreadsheetId: '###', // Please set the Spreadsheet ID.
range: 'Sheet2!A1:D1', // Please set the range as a1Notation.
};
var request = gapi.client.sheets.spreadsheets.values.get(params);
request.then(function(response) {
console.log(response.result.values);
}, function(reason) {
console.error('error: ' + reason.result.error.message);
});
2PACX-...
. So please be careful this. Please check this official document.In this pattern, your URL like https://docs.google.com/spreadsheets/d/e/2PACX-...
is used. From your URL in your question, it is found that the Spreadsheet is published to Web. In this case, only when the values are retrieved from the web published Spreadsheet, you can also use the following script.
const url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQLrcAW_bL5HSVHorBJisdwv8S5_6Th9EP2wiYLmJKktj41uXVepUNOx4USNGdsVAuDOH_qknWs3pGa/pub?gid=###&range=A1%3AD1&output=csv";
fetch(url).then(res => res.text()).then(txt => console.log(txt));
###
of gid=###
.Upvotes: 1