【问题标题】:is there any way to edit google slides presentation from c#有没有办法从 C# 编辑谷歌幻灯片演示文稿
【发布时间】:2019-07-09 13:53:38
【问题描述】:

我正在尝试让 C# 应用程序将数据输入谷歌幻灯片应用程序,有没有办法使用谷歌幻灯片 api 或 api 将文本插入幻灯片

这是一个带有现场演示的演示文稿,我尝试查看谷歌开发者网站,我已经访问了演示文稿,但无法获得更多内容

  using System;
    using System.IO.Ports;
    using System.Threading;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Slides.v1;
    using Google.Apis.Slides.v1.Data;
    using Google.Apis.Services;
    using Google.Apis.Util.Store;
    using System.Collections.Generic;
    using System.IO;
    using Google.Apis.Auth;
    using Google.Apis.Discovery;
    using Google.Apis.Drive;
    using Google.Apis.Drive.v3;
    using Google.Apis.Http;
    using Google.Apis.Logging;
    using Google.Apis.Requests;
    using Google.Apis.Slides;
    using Google.Apis.Testing;
    using Google.Apis.Upload;
    using Google.Apis.Util;
    using Google.Apis;


    public class PortChat
    {
        string tempatefile = "Section header";
        static bool _continue;
        static SerialPort _serialPort;

        static string[] Scopes = {DriveService.Scope.Drive, SlidesService.Scope.Drive};
        static string[] scope2 = { };
        static string ApplicationName = "Google Slides API .NET Quickstart";

        public static void Main()
        {
            UserCredential credential;

            using (var stream =

                new FileStream("C:/credentials/credentials.json", FileMode.Open, FileAccess.Read))

            {
                // The file token.json stores the user's access and refresh tokens, and is created 
                // automatically when the authorization flow completes for the first time. 
                string credPath = "token.json";

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(

                    GoogleClientSecrets.Load(stream).Secrets,

                    Scopes,

                    "user",

                    CancellationToken.None,

                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Slides API service. 
            var service = new SlidesService(new BaseClientService.Initializer()

            {

                HttpClientInitializer = credential,

                ApplicationName = ApplicationName,

            });

            // Define request parameters. 
            String presentationId = "1qmbh4y5Zfzxo_mq5l8SzgLuKmKaC4DQzEib88a45js4";
            PresentationsResource.GetRequest request = service.Presentations.Get(presentationId);

            // Prints the number of slides and elements in a sample presentation: 
            // https://docs.google.com/presentation/d/1EAYk18WDjIG-zp_0vLm3CsfQh_i8eXc67Jo2O9C6Vuc/edit 
            Presentation presentation = request.Execute();
            IList<Page> slides = presentation.Slides;
            Console.WriteLine("The presentation contains {0} slides:", slides.Count);

            for (var i = 0; i < slides.Count; i++)
            {

                var slide = slides[i];
                Console.WriteLine("- Slide #{0} contains {1} elements.", i + 1, slide.PageElements.Count);

            }

        }

    }

【问题讨论】:

  • 您已经发布了 很多 代码,很难确定哪些与问题相关或不相关。请使用最少的代码创建minimal reproducible example,以便我们更轻松地提供帮助。

标签: c# google-api google-slides-api


【解决方案1】:

幻灯片/文本的创建或任何更改都适用于请求。

  1. 您创建一个请求
  2. 您将其添加到列表中
  3. 您批量执行请求

以下是创建幻灯片的示例:

        List<Request> requests = new List<Request>();
        String slideId = "MyNewSlide_001";

        Request rq = new Request();
        rq.CreateSlide = new CreateSlideRequest();
        rq.CreateSlide.ObjectId = slideId;
        rq.CreateSlide.InsertionIndex = 1;

        requests.Add(rq);

        // Execute the request.
        BatchUpdatePresentationRequest body = new BatchUpdatePresentationRequest();
        body.Requests = requests;
        BatchUpdatePresentationResponse response = service.Presentations.BatchUpdate(body, presentationId).Execute();
        CreateSlideResponse createSlideResponse = response.Replies[0].CreateSlide;

google API 页面上没有 .net 的示例,但最简单的方法是使用 java examples 并针对 c# 进行修改

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多