Readme
Wrapper around Google Sheets API v4.
- service_account: JSON string containing Google Service Account key
- sheet_id: Google Sheet ID
- method: HTTP method to use
- action: Full URL with
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/
removed - data: HTTP request body
- a Google Cloud service account
- the Google Sheets API v4 enabled in your Google Cloud project
- the spreadsheet ID (provide it in the sheet_id parameter)
- Share the spreadsheet with the service account
- Make a JSON key for the service account, then set it as a secret. Use the secret for the service_account parameter.
- Figure out the action you want to perform. You will need to provide everything that comes after {spreadsheetId}/ as the action parameter. For example:
values/A1:C1:append?valueInputOption=RAW
- Figure out the request body. For example:
{values: [["foo", "bar", "baz"]]}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const gsheet_call = async (service_account, sheet_id, method, action, data) => {
const { getToken } = await import(
"https://deno.land/x/google_jwt_sa@v0.2.3/mod.ts"
);
const googleAuthOptions = {
scope: ["https://www.googleapis.com/auth/spreadsheets"],
};
const token = await getToken(service_account, googleAuthOptions);
const result = fetchJSON(
`https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/${action}`,
{
method,
headers: {
Authorization: `Bearer ${token.access_token}`,
},
body: (method == "GET" || method == "HEAD") ? undefined : JSON.stringify(data),
},
);
return result;
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.