0

Googleスプレッドプレゼンテーションには、特定のGoogleスプレッドシートにリンクされているチャートがあります。Googleスライド - Gスプレッドシートにリンクされたチャートを自動的に更新する方法

プレゼンテーションには多くの図があるので、これらのリンクされた図をすべて自動的に更新する方法、または少なくともすべてを一度に更新する方法を探しています。

これを行うにはどのような方法が最適ですか?

多くの感謝!

答えて

1

APIについての公式文書(別の言語用)で見つけることができます。 https://developers.google.com/slides/how-tos/add-chart#refreshing_a_chart

このスクリプトを作成し、スケジュールまたは手動で実行する必要があります。

自分のコードがうまく機能しています。

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

カスタム機能をスライドUIのドロップダウンメニューに追加するには、次のスクリプトを使用します。これは、現在のプレゼンテーションからスライドを取得し、それらをループし、各スライド内の任意のチャートを取得し、それらをリフレッシュ(更新)します。

function onOpen() { 
    var ui = SlidesApp.getUi(); 
    ui.createMenu('Custom Menu') 
    .addItem('Batch Update Charts', 'batchUpdate') 
    .addToUi(); 
} 

function batchUpdate(){ 

    var gotSlides = SlidesApp.getActivePresentation().getSlides(); 

    for (var i = 0; i < gotSlides.length; i++) { 
    var slide = gotSlides[i]; 
    var sheetsCharts = slide.getSheetsCharts(); 
    for (var k = 0; k < sheetsCharts.length; k++) { 
     var shChart = sheetsCharts[k]; 
     shChart.refresh(); 
    } 
    } 
} 

注:この応答の時点で、リンクされたスライドを更新/更新する機能は存在しません。

関連する問題