प्रज़ेंटेशन बनाएं और उन्हें मैनेज करें

इस पेज पर बताया गया है कि आम तौर पर, किस तरह के बड़े काम प्रज़ेंटेशन, जैसे:

  • प्रज़ेंटेशन बनाएं
  • किसी मौजूदा प्रज़ेंटेशन को कॉपी करना

नीचे दिए गए पैराग्राफ़ में इन कामों के बारे में पूरी जानकारी दी गई है.

खाली प्रज़ेंटेशन बनाएं

प्रज़ेंटेशन बनाने के लिए, create तरीके का इस्तेमाल करके प्रज़ेंटेशन का कलेक्शन, जैसा कि दिखाया गया है नीचे दिए गए उदाहरण में देखें.

यह उदाहरण, बताए गए टाइटल के साथ एक खाली प्रज़ेंटेशन बनाता है.

Apps Script

slides/api/Snippets.gs
/**
 * Creates a presentation
 * @returns {*} the created presentation
 */
function createPresentation() {
  try {
    const presentation = Slides.Presentations.create({
      title: title
    });
    console.log('Created presentation with ID: %s', presentation.presentationId);

    return presentation;
  } catch (err) {
    // TODO (Developer) - Handle exception
    console.log('Failed with error: %s', err.error);
  }
};

शुरू करें

slides/snippets/presentations.go
// Create a presentation and request a PresentationId.
p := &slides.Presentation{
	Title: "Title",
}
presentation, err := slidesService.Presentations.Create(p).Fields(
	"presentationId",
).Do()
if err != nil {
	log.Fatalf("Unable to create presentation. %v", err)
}
fmt.Printf("Created presentation with ID: %s", presentation.PresentationId)

Java

slides/snippets/src/main/java/CreatePresentation.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.slides.v1.Slides;
import com.google.api.services.slides.v1.SlidesScopes;
import com.google.api.services.slides.v1.model.Presentation;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.util.Collections;

/* Class to demonstrate the use of Slides Create Presentation API */
public class CreatePresentation {
  /**
   * Creates a new presentation.
   *
   * @param title - the name of the presentation to be created
   * @return presentation id
   * @throws IOException - if credentials file not found.
   */
  public static String createPresentation(String title) throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Collections.singleton(SlidesScopes.PRESENTATIONS));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the slides API client
    Slides service = new Slides.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Slides samples")
        .build();

    // Creates a blank presentation with a specified title.
    Presentation presentation = new Presentation()
        .setTitle(title);
    presentation = service.presentations().create(presentation)
        .setFields("presentationId")
        .execute();
    // Prints the newly created presentation id.
    System.out.println("Created presentation with ID: " + presentation.getPresentationId());
    return presentation.getPresentationId();
  }
}

JavaScript

slides/snippets/slides_create_presentation.js
function createPresentation(title, callback) {
  try {
    gapi.client.slides.presentations.create({
      title: title,
    }).then((response) => {
      console.log(`Created presentation with ID: ${response.result.presentationId}`);
      if (callback) callback(response);
    });
  } catch (err) {
    document.getElementById('content').innerText = err.message;
    return;
  }
}

Node.js

slides/snippets/slides_create_presentation.js
/**
 * Creates a Google Slide presentation.
 * @param {string} title The presentation title.
 */
async function createPresentation(title) {
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/presentations',
  });

  const service = google.slides({version: 'v1', auth});
  try {
    const presentation = await service.presentations.create({
      title,
    });
    console.log(
        `Created presentation with ID: ${presentation.data.presentationId}`,
    );
    return presentation;
  } catch (err) {
    // TODO (developer) - Handle exception
    throw err;
  }
}

PHP

slides/snippets/src/SlidesCreatePresentation.php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Slides;
use Google\Service\Slides\Request;

function createPresentation($title)
{
    /* Load pre-authorized user credentials from the environment.
       TODO(developer) - See https://developers.google.com/identity for
        guides on implementing OAuth2 for your application. */
    $client = new Google\Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google\Service\Drive::DRIVE);
    $service = new Google_Service_Slides($client);
    try {
        $presentation = new Google_Service_Slides_Presentation($title);
        //creating a presentation
        $presentation = $service->presentations->create($presentation);
        printf("Created presentation with ID: %s\n", $presentation->presentationId);
        return $presentation;
    } catch (Exception $e) {
        echo 'Message: ' . $e->getMessage();
    }
}

Python

slides/snippets/slides_create_presentation.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def create_presentation(title):
  """
  Creates the Presentation the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  try:
    service = build("slides", "v1", credentials=creds)

    body = {"title": title}
    presentation = service.presentations().create(body=body).execute()
    print(
        f"Created presentation with ID:{(presentation.get('presentationId'))}"
    )
    return presentation

  except HttpError as error:
    print(f"An error occurred: {error}")
    print("presentation not created")
    return error


if __name__ == "__main__":
  # Put the title of the presentation

  create_presentation("finalp")

Ruby

slides/snippets/lib/file_snippets.rb
body = Google::Apis::SlidesV1::Presentation.new
body.title = title
presentation = slides_service.create_presentation(body)
puts "Created presentation with ID: #{presentation.presentation_id}"

Google Drive फ़ोल्डर के साथ काम करना

Google Slides API का इस्तेमाल करके, Drive फ़ोल्डर में सीधे तौर पर प्रज़ेंटेशन बनाने का कोई विकल्प नहीं है. डिफ़ॉल्ट रूप से, बनाया गया प्रज़ेंटेशन, Drive में उपयोगकर्ता के रूट फ़ोल्डर में सेव होता है.

हालांकि, Drive फ़ोल्डर में किसी फ़ाइल को सेव करने के दो विकल्प हैं:

  • प्रज़ेंटेशन बनाने के बाद, Drive API के files.update तरीके का इस्तेमाल करके उसे किसी खास फ़ोल्डर में ले जाएं. फ़ाइलों को एक से दूसरे फ़ोल्डर में ले जाने के बारे में ज़्यादा जानने के लिए, यह लेख पढ़ें: फ़ाइलों को एक से दूसरे फ़ोल्डर में ले जाना.
  • Drive API के files.create तरीके का इस्तेमाल करके किसी फ़ोल्डर में कोई खाली प्रज़ेंटेशन जोड़ें. साथ ही, application/vnd.google-apps.presentation को mimeType के तौर पर बताएं. फ़ाइलें बनाने के बारे में ज़्यादा जानकारी के लिए, फ़ोल्डर में फ़ाइल बनाना लेख पढ़ें.

इनमें से किसी भी विकल्प के लिए, कॉल को अनुमति देने के लिए आपको Drive API के सही दायरे जोड़ने होंगे.

फ़ाइल को शेयर की गई ड्राइव में ले जाने या बनाने के लिए, शेयर की गई ड्राइव से जुड़ी सहायता लागू करना लेख पढ़ें.

किसी मौजूदा प्रज़ेंटेशन को कॉपी करना

किसी प्रज़ेंटेशन को कॉपी करने के लिए, Drive API की files().copy तरीके से कॉपी करना होगा.

नीचे दिए गए उदाहरण में, किसी मौजूदा प्रज़ेंटेशन को कॉपी किया गया है. इसके लिए, प्रज़ेंटेशन के टाइटल और Drive की नई फ़ाइल के नाम, दोनों के लिए स्ट्रिंग.

Apps Script

slides/api/Snippets.gs
/**
 * create a presentation and copy it
 * @param {string} presentationId - ID of presentation to copy
 * @returns {*} the copy's presentation id
 */
function copyPresentation(presentationId) {
  const copyTitle = 'Copy Title';

  let copyFile = {
    title: copyTitle,
    parents: [{id: 'root'}]
  };
  try {
    copyFile = Drive.Files.copy(copyFile, presentationId);
    // (optional) copyFile.id can be returned directly
    const presentationCopyId = copyFile.id;

    return presentationCopyId;
  } catch (err) {
    // TODO (Developer) - Handle exception
    console.log('Failed with error: %s', err.error);
  }
};

शुरू करें

slides/snippets/presentations.go
// Copy a presentation.
file := drive.File{
	Title: title,
}
presentationCopyFile, err := driveService.Files.Copy(id, &file).Do()
if err != nil {
	log.Fatalf("Unable to copy presentation. %v", err)
}
presentationCopyId := presentationCopyFile.Id

Java

slides/snippets/src/main/java/CopyPresentation.java
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.slides.v1.SlidesScopes;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;

import java.io.IOException;
import java.util.Collections;

/* Class to demonstrate the use of Slides Copy Presentation API */
public class CopyPresentation {
  /**
   * Copy an existing presentation.
   *
   * @param presentationId - id of the presentation.
   * @param copyTitle      - New title of the presentation.
   * @return presentation id
   * @throws IOException - if credentials file not found.
   */
  public static String copyPresentation(String presentationId, String copyTitle)
      throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Collections.singleton(SlidesScopes.DRIVE));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the drive API client
    Drive driveService = new Drive.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Slides samples")
        .build();

    String presentationCopyId = null;
    try {
      // Copies an existing presentation using specified presentation title.
      File copyMetadata = new File().setName(copyTitle);
      File presentationCopyFile =
          driveService.files().copy(presentationId, copyMetadata).execute();
      presentationCopyId = presentationCopyFile.getId();
      // Prints the new copied presentation id.
      System.out.println("New copied presentation id " + presentationCopyId);
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 404) {
        System.out.printf("Presentation not found with id '%s'.\n", presentationId);
      } else {
        throw e;
      }
    }
    return presentationCopyId;
  }
}

JavaScript

slides/snippets/slides_copy_presentation.js
function copyPresentation(presentationId, copyTitle, callback) {
  const request = {
    name: copyTitle,
  };
  try {
    gapi.client.drive.files.copy({
      fileId: presentationId,
      resource: request,
    }).then((driveResponse) => {
      const presentationCopyId = driveResponse.result.id;
      if (callback) callback(presentationCopyId);
      console.log('create copy_presentation with id', presentationCopyId);
    });
  } catch (err) {
    document.getElementById('content').innerText = err.message;
    return;
  }
}

Node.js

slides/snippets/slides_copy_presentation.js
/**
 * Copys a Google Slide presentation.
 * @param {string} presentationId The presentation to copy.
 * @param {string} copyTitle The new title.
 */
async function copyPresentation(presentationId, copyTitle) {
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/drive',
  });

  const service = google.drive({version: 'v2', auth});
  const request = {
    name: copyTitle,
  };

  try {
    const driveResponse = await service.files.copy({
      fileId: presentationId,
      resource: request,
    });
    const presentationCopyId = driveResponse.data.id;
    console.log('Created copied presentation with ID: ' + presentationCopyId);
    return driveResponse;
  } catch (err) {
    // TODO (developer) - handle exception
    throw err;
  }
}

PHP

slides/snippets/src/SlidesCopyPresentation.php
use Google\Service\Drive;
use Google\Client;
use Google\Service\Drive\DriveFile;



function copyPresentation($presentationId, $copyTitle)
{
    /* Load pre-authorized user credentials from the environment.
   TODO(developer) - See https://developers.google.com/identity for
    guides on implementing OAuth2 for your application. */
    $client = new Google\Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google\Service\Drive::DRIVE);
    $driveService = new Google_Service_Drive($client);
    try {

        $copy = new Google_Service_Drive_DriveFile(array(
            'name' => $copyTitle
        ));
        $driveResponse = $driveService->files->copy($presentationId, $copy);
        $presentationCopyId = $driveResponse->id;
        printf("copyCreated at:%s\n ", $presentationCopyId);
        return $presentationCopyId;
    } catch (Exception $e) {
        echo 'Message: ' . $e->getMessage();
    }
}

Python

slides/snippets/slides_copy_presentation.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def copy_presentation(presentation_id, copy_title):
  """
  Creates the copy Presentation the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """

  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  try:
    drive_service = build("drive", "v3", credentials=creds)
    body = {"name": copy_title}
    drive_response = (
        drive_service.files().copy(fileId=presentation_id, body=body).execute()
    )
    presentation_copy_id = drive_response.get("id")

  except HttpError as error:
    print(f"An error occurred: {error}")
    print("Presentations  not copied")
    return error

  return presentation_copy_id

Ruby

slides/snippets/lib/file_snippets.rb
body = Google::Apis::SlidesV1::Presentation.new
body.title = copy_title
drive_response = drive_service.copy_file(presentation_id, body)
puts drive_response
presentation_copy_id = drive_response.id

ध्यान दें कि आपको कॉल को अनुमति देने के लिए, Drive API का दायरा.