iT邦幫忙

2024 iThome 鐵人賽

DAY 13
0
IT 管理

30 天玩轉 GAS: 打造你的個人自動化助手系列 第 13

[Day 13] GAS - Google Slide 操作大全 - Part 1 簡報 Presentation &投影片 Slide

  • 分享至 

  • xImage
  •  

講完了 Google Sheet, 接下來就是來學會操作 Google Slide 啦
首先跟大家說明一下 Google Slides 的架構!

Google Slides 的架構

  • Presentation (簡報): 包含多個 Slide (幻燈片)。
  • Slide (投影片): 包含多個 PageElement (頁面元素),如形狀和圖片。
  • PageElement (頁面元素): 可以是 Shape (形狀)、Image (圖片) 等。
  • Shape (形狀): 可以包含文字和其他格式。文字用 TextRange (文字範圍) 來表示。
  • TextRange (文字範圍): 包含一段文字的內容,可以設置文字和段落樣式。
  • ParagraphStyle (段落樣式): 用於設置段落的格式(例如對齊方式)。

那先來講講 PresentationSlide 的用法與差異吧!

1. Presentation (簡報)

Presentation 代表整個 Google 投影片簡報,可使用這個物件來存取和管理整個 Presentation 簡報

那如何用 GAS 建立與開啟一個 google slide 簡報檔案呢?

創建一個 google slide create()

function createNewPresentation() {
  var presentation = SlidesApp.create("My New Presentation");
  Logger.log("New Presentation ID: " + presentation.getId());
}

打開現有的 google slide 檔案

1. 使用ID openById()

  var presentationId = "YOUR_PRESENTATION_ID"; // 替換為實際簡報ID (可從網址看到ID)
  var presentation = SlidesApp.openById(presentationId);

2. 使用 URL openByUrl()

var presentation = SlidesApp.openByUrl('https://docs.google.com/presentation/d/docId/edit');

3. 打開正繫結的簡報 getActivePresentation()

// Get the current presentation to which this script is bound.
var presentation = SlidesApp.getActivePresentation();

獲取簡報 Presentation 的標題

範例:建立一個簡報,標題為 GAS 鐵人賽

https://ithelp.ithome.com.tw/upload/images/20240926/20137680C0QakBD9L0.png

程式碼

function listSlidesInPresentation() {
  var presentation = SlidesApp.getActivePresentation(); // 獲取目前開啟的簡報
  var presentationTitle = presentation.getName(); // 獲取簡報的標題
}

Result

https://ithelp.ithome.com.tw/upload/images/20240926/20137680yNeoychiZb.png

在投影片 Slide 中插入一個標題和副標題

程式碼

function addTitleAndSubtitleToSlide() {
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSlides()[0]; // 取得第一張 Slide
  
  // 插入標題和副標題
  slide.insertTextBox('標題文字', 100, 100, 400, 50);
  slide.insertTextBox('副標題文字', 100, 200, 400, 50); 
  Logger.log('已在第一張 slide 新增標題和副標題');
}

Result

https://ithelp.ithome.com.tw/upload/images/20240902/201376807hAVdbU7oY.png

明天讓我們來詳細看看 PageElement (頁面元素) 和 Shape (形狀) 吧!


上一篇
[Day 12] GAS - Google Sheet 操作大全 Part 5 - RichTextValue 讓文字不只是文字
系列文
30 天玩轉 GAS: 打造你的個人自動化助手13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言