Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Created August 11, 2022 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/0eab3fcd2112e463fa61f34f11f1ce38 to your computer and use it in GitHub Desktop.
Save thomaswilburn/0eab3fcd2112e463fa61f34f11f1ce38 to your computer and use it in GitHub Desktop.
Download a Google sheet via Deno
import * as google from "https://esm.sh/@googleapis/sheets";
import { stringify } from "https://esm.sh/csv-stringify/sync";
var clientID = Deno.env.get("GOOGLE_OAUTH_CLIENT_ID");
var secret = Deno.env.get("GOOGLE_OAUTH_CONSUMER_SECRET");
var spreadsheetId = "1R78UCHNrc2VrTuObhduVZNpgSZJDV5-kuw8S4gzJV9k";
var HOME = Deno.env.get("HOME");
var tokenFile = Deno.readTextFileSync(HOME + "/.google_oauth_token");
var token = JSON.parse(tokenFile);
var headers = new Headers();
headers.set("Authorization", "Bearer " + token.access_token);
var endpoint = "https://sheets.googleapis.com/v4/spreadsheets/";
var sheet = await fetch(endpoint + spreadsheetId, {
headers
}).then(r => r.json());
for (var tab of sheet.sheets) {
var url = `${endpoint}${spreadsheetId}/values/'${encodeURIComponent(tab.properties.title)}'!A:Z`;
var response = await fetch(url, { headers }).then(r => r.json());
var { values } = response;
var csv = stringify(values);
Deno.writeTextFileSync(`./out/${tab.properties.title.replace(/(\/|\s)+/g, "_")}.deno.csv`, csv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment