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

基于Google表单提交生成并发送JPG/PNG格式Instagram故事(附脚本)

问题

我通过Google表单收集应用成员的反馈,想用提交内容自动生成自定义Instagram故事并邮件发给成员直接发布。目前用带占位符的Google Docs模板生成内容,但现有脚本只能发送PDF,而PDF无法直接用于Ins故事发布。需要修改脚本,改为发送JPG/PNG格式,或添加PDF转图片的步骤。现有脚本如下:

function onSubmit(e) {
  const rg = e.range;
  const sh = rg.getSheet();
  
  //Get all the form submitted data
  //Note: This data is dependent on the headers. If headers, are changed update these as well.
  const Email = e.namedValues["What's your email address?"][0];
  const Name = e.namedValues["What's your name?"][0];
  const Text = e.namedValues["If you want to make a custom Instagram story sharing what your experience was like, feel free to drop your text here!"][0];
  
  //Build a new story from the template
  //Folder ID (save destination) and file IDs (template ID + new doc ID)
  const StoryFolderID = '1ghiNSWwkUS7WJ5wwnEON5g0-zRueYlfd';
  const StoryFolder = DriveApp.getFolderById(StoryFolderID);
  
  const TemplateFileID = '1IJ-i8VeazzmWlxyjyXXKHCLrA3pT1fgu4Zy_m74OgHc';
  const newFilename = 'Story -' + TemplateFileID;
  
  //Make a copy of the template file
  const newTemplateFileID = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, StoryFolder).getId();;
  
  //Get the body into a variable
  var document = DocumentApp.openById(newTemplateFileID);
  var body = document.getBody();
  
  //Replace all the {{ }} text in the story body
  body.replaceText('{{text}}', Text);

  document.saveAndClose();

// define email variables
var subject = 'Instagram Story';
var img = DriveApp.getFileById("1d2jofSzEsxuUUEw5g-B684AKMALqyy-b").getBlob();



var msgHtml = 
"Hi " + Name + "!" + "<br/>" + "<br/>" +
"Please find your custom story image attached."  ;
var attachment = DriveApp.getFileById(newTemplateFileID);

//send email with the file
GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, inlineImages: {sampleImage: img}, attachments: [attachment.getAs(MimeType.PDF)]});
  }
解决方案

要实现将Google Docs生成的内容转为JPG/PNG并发送,可利用Google Drive API的导出功能直接将文档转为图片格式,无需先转PDF,具体步骤如下:

1. 启用高级Drive服务

在脚本编辑器中:

  • 点击顶部菜单栏「扩展程序」→「Apps Script」(已打开则跳过)
  • 点击「服务」→「添加服务」,找到「Drive API」并启用,点击「添加」

2. 修改脚本代码

替换原有邮件发送逻辑,改为直接导出文档为PNG(或JPEG),同时可添加临时文件清理逻辑节省空间:

function onSubmit(e) {
  const rg = e.range;
  const sh = rg.getSheet();
  
  // 获取表单提交数据
  const Email = e.namedValues["What's your email address?"][0];
  const Name = e.namedValues["What's your name?"][0];
  const Text = e.namedValues["If you want to make a custom Instagram story sharing what your experience was like, feel free to drop your text here!"][0];
  
  // 模板与文件夹配置
  const StoryFolderID = '1ghiNSWwkUS7WJ5wwnEON5g0-zRueYlfd';
  const StoryFolder = DriveApp.getFolderById(StoryFolderID);
  const TemplateFileID = '1IJ-i8VeazzmWlxyjyXXKHCLrA3pT1fgu4Zy_m74OgHc';
  const newFilename = `Story - ${Name}`; // 用成员名称命名更清晰
  
  // 复制模板并替换占位符
  const newTemplateFile = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, StoryFolder);
  const document = DocumentApp.openById(newTemplateFile.getId());
  const body = document.getBody();
  body.replaceText('{{text}}', Text);
  document.saveAndClose();

  // 导出文档为PNG格式(需JPG则替换为MimeType.JPEG)
  const exportOptions = {
    mimeType: MimeType.PNG
    // 可选:若需适配Ins故事1080x1920尺寸,需先在Docs模板中设置页面为9:16比例
  };
  const imageBlob = Drive.Files.export(newTemplateFile.getId(), exportOptions.mimeType, exportOptions);
  imageBlob.setName(`${newFilename}.png`);

  // 邮件配置与发送
  const subject = '你的自定义Instagram故事';
  const img = DriveApp.getFileById("1d2jofSzEsxuUUEw5g-B684AKMALqyy-b").getBlob();
  const msgHtml = `Hi ${Name}!<br/><br/>请查收附件中的自定义故事图片,可直接用于Instagram故事发布。`;
  
  GmailApp.sendEmail(Email, subject, msgHtml, {
    htmlBody: msgHtml,
    inlineImages: {sampleImage: img},
    attachments: [imageBlob]
  });

  // 可选:删除临时生成的Google Docs文件,节省存储空间
  newTemplateFile.setTrashed(true);
}

关键注意事项

  • 尺寸适配:Instagram故事标准尺寸为1080x1920(9:16),需先在Google Docs模板中设置页面比例为9:16(文件→页面设置),导出的图片可直接使用无需裁剪。
  • 格式切换:若需要JPG格式,仅需将代码中的MimeType.PNG替换为MimeType.JPEG即可。
  • 临时文件管理:脚本末尾的setTrashed(true)会将临时Docs文件移至回收站,可根据需求保留或删除该逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 09:55:23