I'm wanting to, preferably using the Google Slides API, programatically generate a new Google Slides document which pulls in some form submitted variables and various pages from other Google Slides documents.
What I'm aiming to do is create a simple HTML form which asks a series of questions such as:
- Client Name (text field)
- Project Title (text field)
- Include case studies? (yes/no)
The person generating the proposal would complete the answers above and then submit the form.
At this point, and this is what I need help with, I want to:
- Duplicate an existing Google Slides proposal template (which we already have)
- Add in the Client Name and Project Title into text fields on the first slide
- Import slides from another Google Slides document (a case studies document - which we already have) into this newly generated document (if the user answered Yes on the form)
I don't even know where to begin with this as I'm a newbie to the Google Docs API and JavaScript.
I've tried the following but it's not listing the files:
const path = require("path");
// Require the fastify framework and instantiate it
const fastify = require("fastify")({
// set this to true for detailed logging:
logger: false
});
// Setup our static files
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "public"),
prefix: "/" // optional: default '/'
});
// fastify-formbody lets us parse incoming forms
fastify.register(require("fastify-formbody"));
// point-of-view is a templating manager for fastify
fastify.register(require("point-of-view"), {
engine: {
handlebars: require("handlebars")
}
});
// Our main GET home page route, pulls from src/pages/index.hbs
fastify.get("/", function(request, reply) {
// params is an object we'll pass to our handlebars template
let params = {
greeting: "Hello Node!"
};
// request.query.paramName <-- a querystring example
reply.view("/src/pages/index.hbs", params);
});
// A POST route to handle form submissions
fastify.post("/", function(request, reply) {
let params = {
greeting: "Hello Form!"
};
// request.body.paramName <-- a form post example
reply.view("/src/pages/index.hbs", params);
});
// Run the server and report out to the logs
fastify.listen(process.env.PORT, '0.0.0.0', function(err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Your app is listening on ${address}`);
fastify.log.info(`server listening on ${address}`);
});
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/presentations.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Slides API.
authorize(JSON.parse(content), listSlides);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.web;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
/**
* Prints the number of slides and elements in a sample presentation:
* https://docs.google.com/presentation/d/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc/edit
* @param {google.auth.OAuth2} auth The authenticated Google OAuth client.
*/
function listSlides(auth) {
const slides = google.slides({version: 'v1', auth});
slides.presentations.get({
presentationId: '1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const length = res.data.slides.length;
console.log('The presentation contains %s slides:', length);
res.data.slides.map((slide, i) => {
console.log(`- Slide #${i + 1} contains ${slide.pageElements.length} elements.`);
});
});
}
