本文介绍了Google幻灯片 - 如何自动更新链接到G电子表格的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google幻灯片演示文稿,其中包含链接到特定Google电子表格的图表。

I have a Google Slides presentation with charts that are linked to a specific Google Spreadsheet.

由于演示文稿中有很多图表,我正在寻找方式来自动更新所有这些链接的图表,或者至少全部更新。

As there are many charts in the presentation, I'm looking for a way to update all these linked charts automatically, or at least all of them at once.

最好的方法是什么?

非常感谢!

推荐答案

您可以在官方文档中找到关于API的信息(适用于不同的lang)。

You can find it in official documentation about API (for different lang).https://developers.google.com/slides/how-tos/add-chart#refreshing_a_chart

您需要为此编写脚本并按计划或手动运行它。

You need to write a script for this and run it by schedule or manually.

我发现自己的代码非常棒。

I have found my own code that worked great.

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/slides.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Slides API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'slides.googleapis.com-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    """Shows basic usage of the Slides API.

    Creates a Slides API service object and prints the number of slides and
    elements in a sample presentation:
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('slides', 'v1', http=http)

    # Here past your presentation id
    presentationId = '1Owma9l9Z0Xjm1OPp-fcchdcxc1ImBPY2j9QH1LBDxtk'
    presentation = service.presentations().get(
        presentationId=presentationId).execute()
    slides = presentation.get('slides')

    print ('The presentation contains {} slides:'.format(len(slides)))
    for slide in slides:
        for element in slide['pageElements']:

            presentation_chart_id = element['objectId']

            # Execute the request.
            try:
                requests = [{'refreshSheetsChart': {'objectId': presentation_chart_id}}]
                body = {'requests': requests}

                #print(element)
                requests = service.presentations().batchUpdate(
                    presentationId=presentationId, body=body).execute()
                print('Refreshed a linked Sheets chart with ID: {0}'.format(presentation_chart_id))

            except Exception:
                pass

if __name__ == '__main__':
    main()

这篇关于Google幻灯片 - 如何自动更新链接到G电子表格的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:38