如何使用Google Apps Script仅生成Google演示文稿第一页的缩略图
解决方案
你原来的代码中presentation.getSlides()会返回按播放顺序排列的所有幻灯片数组,数组下标从0开始计数,下标为0的元素就是第一页幻灯片。
方案1(更高效,推荐)
直接删除原来的forEach遍历逻辑,替换为仅提取第一页的代码即可:
// 原有代码保留到var screenshots = []; 之后替换下面的遍历部分 // 取第一页幻灯片 const firstSlide = presentation.getSlides()[0]; const url = baseUrl .replace("{presentationId}", presentationId) .replace("{pageObjectId}", firstSlide.getObjectId()); const response = JSON.parse(UrlFetchApp.fetch(url, parameters)); // 上传第一页缩略图到Google Drive const blob = UrlFetchApp.fetch(response.contentUrl).getBlob(); DriveApp.createFile(blob).setName("Image 1.png"); screenshots.push(response.contentUrl);
方案2(仅修改你指定的单行代码,兼容原有结构)
只需给遍历逻辑加第一页判断,把你提到的代码行改为:
presentation.getSlides().forEach(function(slide, index) { if (index === 0) {
同时需要在原来forEach的闭合花括号之前额外加一个闭合花括号,对应判断语句的收尾即可。这个方案逻辑可以实现需求,但会遍历所有幻灯片,执行效率不如方案1。
内容的提问来源于stack exchange,提问作者Henry2004
相关产品推荐
相关产品推荐

