This is a sample script for retrieving the values from a publicly shared Google Spreadsheet using an API key with Javascript.
Sample script
In this sample script, googleapis for Javascript is used.
<script async defer src="https://apis.google.com/js/api.js" onload="handleClientLoad()"></script>
<script>
function handleClientLoad() {
const apiKey = "###"; // Please set your API key.
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
gapi.load('client', async () => {
await gapi.client.init({ apiKey, discoveryDocs: ["https://sheets.googleapis.com/$discovery/rest?version=v4"] });
const { result } = await gapi.client.sheets.spreadsheets.values.get({ spreadsheetId, range: "Sheet1" });
console.log(result);
});
}
-
In this sample, I used Method: spreadsheets.values.get. Of course, in this case, you can use the methods of Method: spreadsheets.get, Method: spreadsheets.values.batchGet.
-
In this script, when your Spreadsheet is not publicly shared, the API key cannot access it. Please be careful about this.
-
But, the API key can be used for only the GET method. So, you can edit Spreadsheet using an API key. Please be careful about this.