| 參數 | 說明 |
|---|---|
| presentationId | Slide ID |
| pageObjectId | Slide 內每頁投影片 ID |
| thumbnailProperties | 縮圖屬性 |
| mimeType | 未指定的話,預設為 PNG |
| thumbnailSize | 未指定的話,伺服器會選擇圖片的預設大小。有 LARGE (1600 像素)、MEDIUM (800 像素)、SMALL (200 像素) 可以選擇 |
Response
| 參數 | 說明 |
|---|---|
| width | 縮圖圖片的正向寬度 (以像素為單位) |
| height | 縮圖圖片的正向高度 (以像素為單位) |
| contentUrl |
縮圖伺服器網址,該縮圖只會留存 30 分鐘且只有請求者可以存取該圖片 |
限制
IntelliSense 或是文件上都會提到 Presentations.Pages.getThumbnail 是高成本讀取操作,在 Usage limits 有特別列出存取限制
| 限制 | 次數 | 說明 |
|---|---|---|
| Per minute per project | 300 次/分鐘 | 所有執行該 Script 使用者加總次數,每分鐘不能超過 300 次 |
| Per minute per user per project | 60 次/分鐘 | 一位使用者每分鐘最多只能請求 60 次 |
gs Code
透過 Presentations.Pages.getThumbnail 產生的 Slide 每頁縮圖會存放在伺服器上,必須透過 response.contentUrl 取回圖片
function runSlideExportAndShare() {
// 從 PropertiesService 取出 presentationId 和 folderId
const scriptProperties = PropertiesService.getScriptProperties();
const properties = scriptProperties.getProperties();
const presentationId = properties["PresentationID"];
const folderId = GetProperties("FolderID");
convertSlidesToImagesAndShare(presentationId, folderId);
}
/**
* 將 Google Slide 內的每一頁轉換為圖片,存儲於 Google Drive 上,並將圖片設定為「知道連結者皆可檢視」
* * @param {string} presentationId 簡報 ID
* @param {string} folderId 圖片存放資料夾 ID
*/
function convertSlidesToImagesAndShare(presentationId, folderId) {
const presentation = SlidesApp.openById(presentationId);
const slides = presentation.getSlides();
const folder = DriveApp.getFolderById(folderId);
slides.forEach((slide, index) => {
const pageObjectId = slide.getObjectId();
// 呼叫 Slides API 取得縮圖 URL
const thumbnailResponse = Slides.Presentations.Pages.getThumbnail(
presentationId,
pageObjectId,
{
'thumbnailProperties.mimeType': 'PNG',
'thumbnailProperties.thumbnailSize': 'LARGE'
}
);
const imageUrl = thumbnailResponse.contentUrl;
const imageName = `Slide_${index + 1}.png`;
try {
// 透過 UrlFetchApp 取回圖片
const response = UrlFetchApp.fetch(imageUrl);
const blob = response.getBlob().setName(imageName);
// 在指定資料夾建立檔案
const file = folder.createFile(blob);
// 設定權限:知道連結的任何人 (ANYONE_WITH_LINK) 都可以檢視 (VIEW)
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
} catch (e) {
console.error(`處理第 ${index + 1} 頁時發生錯誤: ${e.message}`);
}
});
}
執行結果
Slide2Imge 內有兩頁投影片,轉出兩張圖片來

沒有留言:
張貼留言