gamma-core-workflow-b

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Gamma Core Workflow B: Editing and Export

Gamma核心工作流B:编辑与导出

Overview

概述

Implement workflows for editing existing presentations and exporting to various formats.
实现用于编辑现有演示文稿并导出为多种格式的工作流。

Prerequisites

前提条件

  • Completed
    gamma-core-workflow-a
    setup
  • Existing presentation to work with
  • Understanding of export formats
  • 已完成
    gamma-core-workflow-a
    的设置
  • 有可操作的现有演示文稿
  • 了解导出格式

Instructions

操作步骤

Step 1: Retrieve and Edit Presentation

步骤1:获取并编辑演示文稿

typescript
import { GammaClient } from '@gamma/sdk';

const gamma = new GammaClient({ apiKey: process.env.GAMMA_API_KEY });

async function editPresentation(presentationId: string) {
  // Retrieve existing presentation
  const presentation = await gamma.presentations.get(presentationId);

  // Update title and style
  const updated = await gamma.presentations.update(presentationId, {
    title: 'Updated: ' + presentation.title,
    style: 'modern',
  });

  return updated;
}
typescript
import { GammaClient } from '@gamma/sdk';

const gamma = new GammaClient({ apiKey: process.env.GAMMA_API_KEY });

async function editPresentation(presentationId: string) {
  // Retrieve existing presentation
  const presentation = await gamma.presentations.get(presentationId);

  // Update title and style
  const updated = await gamma.presentations.update(presentationId, {
    title: 'Updated: ' + presentation.title,
    style: 'modern',
  });

  return updated;
}

Step 2: Slide-Level Editing

步骤2:幻灯片级编辑

typescript
async function editSlide(presentationId: string, slideIndex: number, content: object) {
  const presentation = await gamma.presentations.get(presentationId);

  // Update specific slide
  const updatedSlide = await gamma.slides.update(
    presentationId,
    slideIndex,
    {
      title: content.title,
      content: content.body,
      layout: content.layout || 'content',
    }
  );

  return updatedSlide;
}

async function addSlide(presentationId: string, position: number, content: object) {
  return gamma.slides.insert(presentationId, position, {
    title: content.title,
    content: content.body,
    generateImage: content.imagePrompt,
  });
}

async function deleteSlide(presentationId: string, slideIndex: number) {
  return gamma.slides.delete(presentationId, slideIndex);
}
typescript
async function editSlide(presentationId: string, slideIndex: number, content: object) {
  const presentation = await gamma.presentations.get(presentationId);

  // Update specific slide
  const updatedSlide = await gamma.slides.update(
    presentationId,
    slideIndex,
    {
      title: content.title,
      content: content.body,
      layout: content.layout || 'content',
    }
  );

  return updatedSlide;
}

async function addSlide(presentationId: string, position: number, content: object) {
  return gamma.slides.insert(presentationId, position, {
    title: content.title,
    content: content.body,
    generateImage: content.imagePrompt,
  });
}

async function deleteSlide(presentationId: string, slideIndex: number) {
  return gamma.slides.delete(presentationId, slideIndex);
}

Step 3: Export to Various Formats

步骤3:导出为多种格式

typescript
type ExportFormat = 'pdf' | 'pptx' | 'png' | 'html';

async function exportPresentation(
  presentationId: string,
  format: ExportFormat,
  options: object = {}
) {
  const exportJob = await gamma.exports.create(presentationId, {
    format,
    quality: options.quality || 'high',
    includeNotes: options.includeNotes ?? true,
    ...options,
  });

  // Wait for export to complete
  const result = await gamma.exports.wait(exportJob.id, {
    timeout: 60000,
    pollInterval: 2000,
  });

  return result.downloadUrl;
}

// Usage examples
const pdfUrl = await exportPresentation('pres-123', 'pdf');
const pptxUrl = await exportPresentation('pres-123', 'pptx', { includeNotes: false });
const pngUrl = await exportPresentation('pres-123', 'png', { slideIndex: 0 }); // First slide only
typescript
type ExportFormat = 'pdf' | 'pptx' | 'png' | 'html';

async function exportPresentation(
  presentationId: string,
  format: ExportFormat,
  options: object = {}
) {
  const exportJob = await gamma.exports.create(presentationId, {
    format,
    quality: options.quality || 'high',
    includeNotes: options.includeNotes ?? true,
    ...options,
  });

  // Wait for export to complete
  const result = await gamma.exports.wait(exportJob.id, {
    timeout: 60000,
    pollInterval: 2000,
  });

  return result.downloadUrl;
}

// Usage examples
const pdfUrl = await exportPresentation('pres-123', 'pdf');
const pptxUrl = await exportPresentation('pres-123', 'pptx', { includeNotes: false });
const pngUrl = await exportPresentation('pres-123', 'png', { slideIndex: 0 }); // First slide only

Step 4: Asset Management

步骤4:资源管理

typescript
async function uploadAsset(presentationId: string, filePath: string) {
  const fileBuffer = await fs.readFile(filePath);

  const asset = await gamma.assets.upload(presentationId, {
    file: fileBuffer,
    filename: path.basename(filePath),
    type: 'image',
  });

  return asset.url;
}

async function listAssets(presentationId: string) {
  return gamma.assets.list(presentationId);
}
typescript
async function uploadAsset(presentationId: string, filePath: string) {
  const fileBuffer = await fs.readFile(filePath);

  const asset = await gamma.assets.upload(presentationId, {
    file: fileBuffer,
    filename: path.basename(filePath),
    type: 'image',
  });

  return asset.url;
}

async function listAssets(presentationId: string) {
  return gamma.assets.list(presentationId);
}

Output

输出

  • Updated presentation with modifications
  • Exported files in various formats
  • Managed presentation assets
  • Download URLs for exports
  • 已完成修改的演示文稿
  • 多种格式的导出文件
  • 已管理的演示文稿资源
  • 导出文件的下载链接

Error Handling

错误处理

ErrorCauseSolution
Export TimeoutLarge presentationIncrease timeout or reduce slides
Format Not SupportedInvalid export formatCheck supported formats
Asset Too LargeFile exceeds limitCompress or resize image
Slide Not FoundInvalid indexVerify slide exists
错误原因解决方案
导出超时演示文稿过大增加超时时间或减少幻灯片数量
格式不支持导出格式无效检查支持的格式
资源过大文件超出限制压缩或调整图片大小
幻灯片未找到索引无效验证幻灯片是否存在

Resources

参考资源

Next Steps

下一步

Proceed to
gamma-common-errors
for error handling patterns.
继续学习
gamma-common-errors
以了解错误处理模式。