I recently had to work with Google Sheets in NodeJS. It was a fun challenge for the day and surprisingly was not too much work. Here is the final “demo” we gave to the client in its entirety, afterwards I’ll break it down section by section.
import * as googleAuth from 'googleapis';
import { google } from 'googleapis';
const creds = require('./credentials.json');
void (async () => {
const auth = new googleAuth.Auth.GoogleAuth({
: [
scopes'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
,
]: creds,
credentials;
})
.options({ auth: await auth.getClient() });
google
.options({ auth: await auth.getClient() });
googleconst sheets = google.sheets('v4');
const drive = google.drive('v3');
const copyRes = await drive.files.copy({
: 'templateSheetId',
fileId: {
requestBody: 'Copy of sheet',
name,
};
})const sheetId = copyRes.data.id;
await sheets.spreadsheets.values.batchUpdate({
: sheetId,
spreadsheetId: {
requestBody: [
data: "A1", values: [[9999]] },
{ range: "A2", values: [[123456]] },
{ range,
]: 'USER_ENTERED',
valueInputOption,
};
})
await drive.permissions.create({
: {
requestBody: 'user',
type: 'writer',
role: 'example@example.com',
emailAddress,
}: sheetId,
fileId: 'id',
fields;
}) )}()
Firstly we import the required packages, ironically, figuring out which package to import was difficult as the documentation is surprisingly not very good
import * as googleAuth from 'googleapis';
import { google } from 'googleapis';
const creds = require('./credentials.json');
Next, we Authorize with google using a credential file generated in the google cloud dashboard. We need to ensure we provide all the scopes
we’ll be using
const auth = new googleAuth.Auth.GoogleAuth({
: [
scopes'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
,
]: creds,
credentials;
})
.options({ auth: await auth.getClient() }); google
We then initialise the sheets
and drive
objects which allow us to interact with the respective services
const sheets = google.sheets('v4');
const drive = google.drive('v3');
We can then use drive
to create a copy of the template sheet
const copyRes = await drive.files.copy({
: 'templateSheetId',
fileId: {
requestBody: 'Copy of sheet',
name,
};
})const sheetId = copyRes.data.id;
Using the returned sheetId
we can then use sheets
to mutate the data
await sheets.spreadsheets.values.batchUpdate({
: sheetId,
spreadsheetId: {
requestBody: [
data: "A1", values: [[9999]] },
{ range: "A2", values: [[123456]] },
{ range,
]: 'USER_ENTERED',
valueInputOption,
}; })
Finally to ensure the user we’re creating this sheet for can access it, we use drive
to create a permission for them.
await drive.permissions.create({
: {
requestBody: 'user',
type: 'writer',
role: 'example@example.com',
emailAddress,
}: sheetId,
fileId: 'id',
fields; })
The user example@example.com
will shortly receive an email informing them that the file has been shared with them.