Hey there, fellow Ruby enthusiast! Ready to spice up your presentations programmatically? Let's dive into the world of Google Slides API using the nifty google-apis-slides_v1
package. This guide will walk you through the process of creating, modifying, and managing Google Slides presentations with Ruby. Buckle up!
Before we jump in, make sure you've got these bases covered:
First things first, let's get that gem installed:
gem install google-apis-slides_v1
Easy peasy!
Time to get cozy with OAuth 2.0. Here's a quick snippet to get you started:
require 'google/apis/slides_v1' require 'googleauth' require 'googleauth/stores/file_token_store' OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze scope = 'https://www.googleapis.com/auth/presentations' client_id = Google::Auth::ClientId.from_file('path/to/client_secrets.json') token_store = Google::Auth::Stores::FileTokenStore.new(file: 'path/to/tokens.yaml') authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store) credentials = authorizer.get_credentials('default') if credentials.nil? url = authorizer.get_authorization_url(base_url: OOB_URI) puts "Open this URL in your browser and enter the resulting code:" puts url code = gets credentials = authorizer.get_and_store_credentials_from_code( user_id: 'default', code: code, base_url: OOB_URI ) end
Now that we're authenticated, let's create our Slides service object:
service = Google::Apis::SlidesV1::SlidesService.new service.authorization = credentials
Let's make some slides!
presentation = Google::Apis::SlidesV1::Presentation.new(title: 'My Awesome Presentation') presentation = service.create_presentation(presentation) puts "Created presentation with ID: #{presentation.presentation_id}"
Already have a presentation? No problem:
presentation_id = 'your-presentation-id' presentation = service.get_presentation(presentation_id)
Let's add a slide to our presentation:
requests = [ { create_slide: { object_id_prop: 'my-unique-slide-id', insertion_index: 1, slide_layout_reference: { predefined_layout: 'TITLE_AND_TWO_COLUMNS' } } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))
Time to add some text:
requests = [ { create_shape: { object_id_prop: 'my-text-box', shape_type: 'TEXT_BOX', element_properties: { page_object_id: 'my-unique-slide-id', size: { width: { magnitude: 300, unit: 'PT' }, height: { magnitude: 100, unit: 'PT' } }, transform: { scale_x: 1, scale_y: 1, translate_x: 350, translate_y: 100, unit: 'PT' } } } }, { insert_text: { object_id_prop: 'my-text-box', text: 'Hello, Google Slides API!' } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))
Let's add some visual flair:
requests = [ { create_image: { url: 'https://example.com/image.jpg', element_properties: { page_object_id: 'my-unique-slide-id', size: { width: { magnitude: 300, unit: 'PT' }, height: { magnitude: 300, unit: 'PT' } }, transform: { scale_x: 1, scale_y: 1, translate_x: 350, translate_y: 100, unit: 'PT' } } } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))
Need to change that text? No sweat:
requests = [ { delete_text: { object_id_prop: 'my-text-box', text_range: { type: 'ALL' } } }, { insert_text: { object_id_prop: 'my-text-box', text: 'Updated text!' } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))
Want to use a template? Here's how:
template_presentation_id = 'your-template-id' requests = [ { apply_presentation_theme: { theme_id: template_presentation_id } } ] service.batch_update_presentation(presentation_id, Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests))
Always wrap your API calls in error handling:
begin # Your API call here rescue Google::Apis::Error => e puts "An error occurred: #{e.message}" end
And remember, batch your requests when possible to improve performance!
There you have it! You're now equipped to create stunning presentations programmatically with Ruby and the Google Slides API. The possibilities are endless, so go forth and automate those slides!
For more in-depth info, check out the official Google Slides API documentation. Happy coding!