บริการสไลด์ขั้นสูง

บริการสไลด์ขั้นสูงช่วยให้คุณเข้าถึง Slide API ได้โดยใช้ Apps Script บริการนี้อนุญาตให้สคริปต์อ่านและแก้ไขเนื้อหาใน Google สไลด์

ข้อมูลอ้างอิง

หากต้องการข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ โปรดดูเอกสารอ้างอิงสำหรับ Slides API บริการสไลด์ขั้นสูงจะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script โปรดดูข้อมูลเพิ่มเติมที่หัวข้อวิธีกำหนดลายเซ็นของเมธอด

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุนสไลด์

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ API เวอร์ชัน 1

สร้างงานนำเสนอใหม่

ตัวอย่างต่อไปนี้สาธิตวิธีสร้างงานนำเสนอใหม่โดยใช้บริการขั้นสูงของสไลด์ ซึ่งจะเหมือนกับตัวอย่างสูตรอาหาร สร้างงานนำเสนอใหม่

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);
  }
}

แนวทางปฏิบัติแนะนำ

การอัปเดตเป็นกลุ่ม

เมื่อใช้บริการขั้นสูงของสไลด์ ให้รวมคำขอหลายรายการไว้ในอาร์เรย์แทนการเรียก 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
});