Slides की बेहतर सेवा

बेहतर स्लाइड सेवा, आपको Apps Script का इस्तेमाल करके Slides API ऐक्सेस करने देती है. यह सेवा, स्क्रिप्ट को Google Slides में कॉन्टेंट पढ़ने और उसमें बदलाव करने की अनुमति देती है.

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानकारी पाने के लिए, Slides API का रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी बेहतर सेवाओं की तरह, बेहतर Slides की सेवा भी उन ही ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है जिनका इस्तेमाल सार्वजनिक API में होता है. ज़्यादा जानकारी के लिए, डिलीवरी के तरीके के हस्ताक्षर तय करने का तरीका देखें.

समस्याओं की शिकायत करने और अन्य मदद पाने के लिए, Slides से जुड़ी सहायता गाइड देखें.

नमूना कोड

नीचे दिया गया सैंपल कोड, एपीआई के वर्शन 1 का इस्तेमाल करता है.

नई प्रस्तुति बनाएं

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

advanced/slides.gs
/**
 * Create a new presentation.
 * @return {string} presentation Id.
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create
 */
function createPresentation() {
  try {
    const presentation =
      Slides.Presentations.create({'title': 'MyNewPresentation'});
    console.log('Created presentation with ID: ' + presentation.presentationId);
    return presentation.presentationId;
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

एक नई स्लाइड बनाएं

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

advanced/slides.gs
/**
 * Create a new slide.
 * @param {string} presentationId The presentation to add the slide to.
 * @return {Object} slide
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function createSlide(presentationId) {
  // You can specify the ID to use for the slide, as long as it's unique.
  const pageId = Utilities.getUuid();

  const requests = [{
    'createSlide': {
      'objectId': pageId,
      'insertionIndex': 1,
      'slideLayoutReference': {
        'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
      }
    }
  }];
  try {
    const slide =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    console.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);
    return slide;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

पेज तत्व ऑब्जेक्ट आईडी पढ़ें

इस उदाहरण में फ़ील्ड मास्क का इस्तेमाल करके, किसी स्लाइड पर हर पेज एलिमेंट के लिए ऑब्जेक्ट आईडी पाने का तरीका बताया गया है. यह किसी पेज के एलिमेंट ऑब्जेक्ट आईडी को पढ़ने की रेसिपी के सैंपल के बराबर होती है.

advanced/slides.gs
/**
 * Read page element IDs.
 * @param {string} presentationId The presentation to read from.
 * @param {string} pageId The page to read from.
 * @return {Object} response
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/get
 */
function readPageElementIds(presentationId, pageId) {
  // You can use a field mask to limit the data the API retrieves
  // in a get request, or what fields are updated in an batchUpdate.
  try {
    const response = Slides.Presentations.Pages.get(
        presentationId, pageId, {'fields': 'pageElements.objectId'});
    console.log(response);
    return response;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

एक नया टेक्स्ट बॉक्स जोड़ें

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

advanced/slides.gs
/**
 * Add a new text box with text to a page.
 * @param {string} presentationId The presentation ID.
 * @param {string} pageId The page ID.
 * @return {Object} response
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function addTextBox(presentationId, pageId) {
  // You can specify the ID to use for elements you create,
  // as long as the ID is unique.
  const pageElementId = Utilities.getUuid();

  const requests = [{
    'createShape': {
      'objectId': pageElementId,
      'shapeType': 'TEXT_BOX',
      'elementProperties': {
        'pageObjectId': pageId,
        'size': {
          'width': {
            'magnitude': 150,
            'unit': 'PT'
          },
          'height': {
            'magnitude': 50,
            'unit': 'PT'
          }
        },
        'transform': {
          'scaleX': 1,
          'scaleY': 1,
          'translateX': 200,
          'translateY': 100,
          'unit': 'PT'
        }
      }
    }
  }, {
    'insertText': {
      'objectId': pageElementId,
      'text': 'My Added Text Box',
      'insertionIndex': 0
    }
  }];
  try {
    const response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    console.log('Created Textbox with ID: ' +
      response.replies[0].createShape.objectId);
    return response;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

आकार टेक्स्ट फ़ॉर्मैट करें

नीचे दिए गए उदाहरण में किसी आकार के टेक्स्ट को फ़ॉर्मैट करने, उसका रंग, फ़ॉन्ट अपडेट करने, और टेक्स्ट को अंडरलाइन करने का तरीका बताया गया है. यह आकार या टेक्स्टबॉक्स में टेक्स्ट को फ़ॉर्मैट करें रेसिपी के सैंपल के बराबर होता है.

advanced/slides.gs
/**
 * Format the text in a shape.
 * @param {string} presentationId The presentation ID.
 * @param {string} shapeId The shape ID.
 * @return {Object} replies
 * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
 */
function formatShapeText(presentationId, shapeId) {
  const requests = [{
    'updateTextStyle': {
      'objectId': shapeId,
      'fields': 'foregroundColor,bold,italic,fontFamily,fontSize,underline',
      'style': {
        'foregroundColor': {
          'opaqueColor': {
            'themeColor': 'ACCENT5'
          }
        },
        'bold': true,
        'italic': true,
        'underline': true,
        'fontFamily': 'Corsiva',
        'fontSize': {
          'magnitude': 18,
          'unit': 'PT'
        }
      },
      'textRange': {
        'type': 'ALL'
      }
    }
  }];
  try {
    const response =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    return response.replies;
  } catch (e) {
    // TODO (developer) - Handle Exception
    console.log('Failed with error %s', e.message);
  }
}

सबसे सही तरीके

बैच अपडेट

Slides की ऐडवांस सेवा का इस्तेमाल करते समय, batchUpdate को लूप में कॉल करने के बजाय, एक से ज़्यादा अनुरोधों को कलेक्शन में जोड़ें.

ऐसा न करेंbatchUpdate को लूप में कॉल करें.

var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
  Slides.Presentations.batchUpdate(preso, {
    requests: [{
      createSlide: ...
    }]
  });
}

ऐसा करें — अपडेट के कलेक्शन के साथ batchUpdate को कॉल करें.

var requests = [];
var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
  requests.push({ createSlide: ... });
}

Slides.Presentations.batchUpdate(preso, {
  requests: requests
});