且构网

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

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

更新时间:2021-08-04 23:09:20

(2017年2月)截至2016年11月,使用发行版 > Google Slides API ,可以替代OP要求的解决方案,尽管该API不能直接使用.解决方法是,要复制的一张幻灯片位于其自己的独立文件中.然后,您可以使用两个 Google API来实现:幻灯片API(用于执行文档级功能,例如添加新幻灯片)以及 Google云端硬盘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).

听起来像您正在使用用于Ruby的Google API客户端库 ,因此您已在那里.下一步是使用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快速入门示例,这是 Slides API的Ruby快速入门.如果您不对Python过敏(如果是的话,就假装它是伪代码;)),那么我已经使

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.

下面是另一个Python示例,您可以移植到Ruby,它完全按照OP的要求进行操作,假设您的模板文件是SLIDE_TEMPLATE_FILEDRIVE是您的Drive API服务端点,而SLIDES是该API的端点幻灯片API.如果有人PMS给我一个Ruby端口,我将使用它来更新此答案.

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()

最后,是的,您还可以使用

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.