且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将使用API​​的Google演示文稿中的一张幻灯片复制到新的Google演示文稿中

更新时间:2023-02-13 22:58:34

(2017年2月)截至2016年11月,发布 Google幻灯片API ,现在可以选择OP所要求的解决方案,尽管不是直接与API。该解决方法是您要复制的一张幻灯片位于其自己的独立文件中。然后,您可以使用两个 Google API来实现它:幻灯片API(用于执行文档级别的功能,如添加新幻灯片)以及 Google Drive API (用于文件的复制等面向访问)。

(Feb 2017) As of Nov 2016 with the release of the Google Slides API, an alternative to the solution the OP is asking for is now possible although not directly with the API. That workaround is that the ONE SLIDE you want copied is in its own standalone file. Then you can use two Google APIs to make it happen: the Slides API (to perform the document-level functions like adding new slides) as well as the Google Drive API (for file-oriented access like copying).

听起来就像您使用 Google API客户端库对于Ruby ,所以你就在那里设置。下一步是使用Google云端硬盘和Google幻灯片API在 Google Developers Console 中创建一个项目,然后在创建OAuth2凭证后下载 client_secret * .json 文件。

Sounds like you're using the Google APIs Client Library for Ruby, so you're set there. The next step is to create a project in the Google Developers Console with both the Google Drive and Google Slides APIs enabled, then download the client_secret*.json file after you create your OAuth2 credentials.

为了帮助您开始使用,这里是适用于Drive API的 Ruby快速入门示例,以下是为幻灯片API Ruby快速入门。如果你对Python没有过敏(如果你是,只是假装它是伪代码;)),我已经制作了多个视频,并提供更多真实世界的示例,使用可以学习并迁移到Ruby的幻灯片API。

To help you get started, here's the Ruby quickstart sample for the Drive API, and here's the Ruby quickstart for the Slides API. If you're not "allergic" to Python (if you are, just pretend it's pseudocode ;) ), I've made several videos with more "real-world" examples of using the Slides API you can learn from and migrate to Ruby if desired.

以下是另一个可以移植到Ruby的Python示例,该示例假定您的模板文件是 SLIDE_TEMPLATE_FILE DRIVE 是您的Drive API服务端点,而 SLIDES 是幻灯片API的端点。

Below is another Python example which you can port to Ruby that does exactly what the OP asked, assuming your template file is SLIDE_TEMPLATE_FILE, DRIVE is your Drive API service endpoint, and SLIDES is the endpoint for the Slides API. If someone PMs me a Ruby port, I'll update this answer with it.

# query for template file with one slide
TMPLFILE = SLIDE_TEMPLATE_FILE
rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]

# copy template file
DATA = {'name': 'Google Drive & Slides API template DEMO'}
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id')

# create 2 more (1 title-only & 1 blank) slides in copied file
reqs = [
    {'createSlide': {'slideLayoutReference': {'predefinedLayout': 'TITLE_ONLY'}}},
    {'createSlide': {'slideLayoutReference': {'predefinedLayout': 'BLANK'}}},
]
SLIDES.presentations().batchUpdate(body={'requests': reqs},
        presentationId=DECK_ID).execute()

,您还可以使用 deleteObject请求

Finally, yes, you can also delete slides from presentations with the deleteObject request, passing in the ID of the slide/page you want removed. Another workaround if you can't isolate the ONE SLIDE: copy the entire presentation & delete all pages except the ONE SLIDE, then start adding new slides. Hopefully the API will eventually get "the right solution" so we don't have to play these games.