You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Google幻灯片存为图片并写入ID/URL至Google表格的代码报错问题

Google Apps Script 定制邀请函生成故障排查

我需要实现以下定制邀请函流程:

  • 从Google Sheets文件中读取宾客姓名与邮箱列表
  • 将宾客姓名插入Google Slides模板生成定制邀请函缩略图
  • 把缩略图作为邮件附件发送给对应宾客
  • 将生成的图片保存至Drive指定文件夹
  • 将图片的ID/URL写入表格C列的对应行

修改前的可运行代码

function changeSlideTextAndDuplicate() {
  var presentationId = "11i9F3qH-xX1c4FGO6cSSAaTGNwTRowk-6W183lntLRA"; // 请替换为你的Google Slides演示文稿ID

  var sheet = SpreadsheetApp.getActiveSheet(); // 使用当前活动工作表,如需指定工作表可改为 SpreadsheetApp.getActiveSpreadsheet().getSheetByName("###工作表名称###")
  var range = sheet.getRange("A2:B" + sheet.getLastRow());
  var presentation = SlidesApp.openById(presentationId);
  var templateSlide = presentation.getSlides()[0];
  range.getDisplayValues().forEach(([name, email]) => {
    if (!name || !email) return;
    var newSlide = presentation.appendSlide(templateSlide);
    newSlide.getShapes()[0].getText().setText(name);
    presentation.saveAndClose();
    var { contentUrl } = Slides.Presentations.Pages.getThumbnail(presentationId, newSlide.getObjectId(), { "thumbnailProperties.thumbnailSize": "LARGE" });
    
    MailApp.sendEmail({
      to: email,
      subject: "Invitation to the walima of Ameen to Aqeela", // 请设置邮件标题
      htmlBody: "Assalamu Alaikum Dear" +" " + name + "." + "\n" + "\n" + "We would love for you to attend the walima of Ameen to Aqeela. Please see the invitation attached",
      inlineImages: { image1: UrlFetchApp.fetch(contentUrl).getBlob() }
    });
    presentation = SlidesApp.openById(presentationId);
  });
}

修改后出现报错的代码

执行此代码时,提示获取缩略图出错:

function changeSlideTextAndDuplicate2() {
  var presentationId = "11i9F3qH-xX1c4FGO6cSSAaTGNwTRowk-6W183lntLRA";
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("A2:B" + sheet.getLastRow());
  var presentation = SlidesApp.openById(presentationId);
  var templateSlide = presentation.getSlides()[0];

  range.getDisplayValues().forEach(([name, email]) => {
    if (!name || !email) return;
    var newSlide = presentation.appendSlide(templateSlide);
    newSlide.getShapes()[0].getText().setText(name);

    // 生成图片唯一ID(可使用Utilities.getUuid()或其他方法)
    var imageId = Utilities.getUuid();

    // 将图片ID保存到Google Sheets的C列
    var imageIdColumn = sheet.getRange(sheet.getLastRow() + 1, 3); // 假设图片ID存储在C列
    imageIdColumn.setValue(imageId);

   // 获取新幻灯片的objectId
var newSlideObjectId = newSlide.getObjectId().replace("SLIDES_API", "");

try {
  // 使用正确的objectId调用API获取缩略图
  var { contentUrl } = Slides.Presentations.Pages.getThumbnail(presentationId, newSlideObjectId, { "thumbnailProperties.thumbnailSize": "LARGE" });

  MailApp.sendEmail({
    to: email,
    subject: "Invitation to the walima of Ameen to Aqeela",
    htmlBody: "Assalamu Alaikum Dear" ...",
    inlineImages: { image1: UrlFetchApp.fetch(contentUrl).getBlob() }
  });
} catch (error) {
  Logger.log("Error getting thumbnail. Slide ID:", newSlideObjectId);
  Logger.log("Error details:", error);
}


  });

  // 处理完所有幻灯片后关闭演示文稿
  presentation.saveAndClose();
}

内容的提问来源于stack exchange,提问作者Tasneem Ghoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:00:56