如何无需手动打开即可更新Google Slides中的值?
问题:Google Slides占位符更新后PDF预览不生效,需手动打开才显示
我有一个绑定Google Apps Script的Google表格,触发脚本时预期完成以下操作:
- 打开指定的Google Slides模板文件
- 将模板复制到特定子文件夹
- 修改副本内的占位符数值
- 将新生成的Google Slides以PDF格式通过邮件发送
除步骤3外,其余操作均正常运行。但占位符仅在手动打开新文件后才会更新,邮件收到的PDF及Drive中的预览仍显示原始占位符,打开文件后则自动呈现更新后的值,无法定位问题原因。
已尝试的方案:
- 添加
Utilities.sleep()等待值更新,无效 - 再次调用
.openById()尝试强制打开文件,无效 - 添加循环刷新每张幻灯片
最小复现代码
const idTemplate = "1BugZRdW8aOzgPOZun-j_U60lC-yXQFXug3R2-MR7AFs"; function generatePresentationData() { var s = SpreadsheetApp.getActive(); //Data is in A2:B2 of "Records" sheet. var ss = s.getSheetByName("Records"); var data = ss.getRange("A2:B2").getValues(); //Get the Google Slides template var file = DriveApp.getFileById(idTemplate); //Create a copy of the template into a specific folder, and get the ID of this copy var newParte = file.makeCopy("New Parte", DriveApp.getFolderById("1rCEYVilGiOlxh03aFANzD4qgKXKBmODh")); var newParteId = newParte.getId(); //Get the slides of this file. var slides = getSlides_(newParteId); //Replace the placeholders with the value in A2:B2. This is the part that doesn't actually "shows up" on this new file until I manually open it from my Drive. It does work correctly, so I don't think the issue lies here writeDataToPlayersSlide_(slides, data); //I have tried adding a wait period but didn't work Utilities.sleep(60000); //I also tried defining the file again before proceeding to send it via email, but again didn't work var newParte = DriveApp.getFileById(newParteId); //I also tried refreshing each slide refreshSlides(SlidesApp.openById(newParteId)); //Get this new file as a Blob var theBlob = newParte.getBlob().getAs('application/pdf'); var nameFile = 'Test.pdf'; var email = "mail@gmail.com"; var subject = 'Test Subject'; var body = 'Hi!, <br/>Here is your file.'; GmailApp.sendEmail(email, subject, body, { htmlBody: body, attachments: [{ fileName: nameFile, content: theBlob.getBytes(), mimeType: "application/pdf" }] }); } function getSlides_(newParteId){ let presentation = SlidesApp.openById(newParteId); let slides = presentation.getSlides(); return slides; } function writeDataToPlayersSlide_(slides, data){ let parte = slides[0]; for (let index=0;index<data[0].length;index++){ parte.replaceAllText(`[var_${index}]`,data[0][index]); } } function refreshSlides(presentation){ // loop through all slides and refresh them numSlides = presentation.getSlides().length; for (i = 0; i < numSlides; i++) { presentation.getSlides()[i].refreshSlide(); } }
解决方案
核心问题是修改占位符后未显式保存演示文稿的更改:SlidesApp的操作在内存中完成,若不调用saveAndClose(),更改不会同步到Drive,导致后续获取的Blob仍是旧版本内容。
修改步骤
- 调整
getSlides_函数,同时返回演示文稿对象和幻灯片集合,方便后续保存:
function getSlides_(newParteId){ let presentation = SlidesApp.openById(newParteId); let slides = presentation.getSlides(); return {presentation, slides}; }
- 在
generatePresentationData函数中,修改占位符后立即调用saveAndClose()同步更改:
// 获取幻灯片和演示文稿对象 let {presentation, slides} = getSlides_(newParteId); // 替换占位符 writeDataToPlayersSlide_(slides, data); // 显式保存并关闭演示文稿,确保更改同步到Drive presentation.saveAndClose();
- 移除无效的
Utilities.sleep()和refreshSlides()调用(后者仅用于刷新链接图表,对文本占位符无效)。
修改后,Drive中的文件会立即同步更新内容,生成PDF时就能获取到替换后的占位符值,无需手动打开文件。
内容的提问来源于stack exchange,提问作者Xavier
相关产品推荐
相关产品推荐

