如何编写从 Google 表单上传的照片并将其大小设置为 1 英寸以便在模板中显示的脚本?

How do I script a photo from Google Form upload and set its size to 1 inch for display in a template?

我正在尝试制作需要附上申请人照片的申请。如何编写来自 Google 表单上传的照片并将其大小设置为 1 英寸以便在模板中显示?太感谢了 :)

我的代码

function onFormSubmit(e){
  var fullname = e.values[1];
  var address = e.values[2];
  var phone = e.values[3];
  var email = e.values[4];
  var photo = e.values[5];
  
  var copyId = DriveApp.getFileById('1tO7L1tWbwMvMyfJGCQB2cONOCyai_ZCIYt_VnDBBnBo')
  .makeCopy('Application'+'_2020-'+fullname)
  .getId();

  var copyDoc = DocumentApp.openById(copyId);

  copyDoc.getBody()
  var copyBody = copyDoc.getActiveSection();

  copyBody.replaceText('keyfullName', fullname);
  copyBody.replaceText('keyaddress', address);
  copyBody.replaceText('keyphone', phone);
  copyBody.replaceText('keyemail', email);
  copyBody.replaceText('keyphoto', photo);
  
  copyDoc.saveAndClose();
  
} 

这需要一些准备步骤,因为您不是替换文本而是输入实际图像。您需要从 e.values(5) 的 url 中的 id 中获取图像,然后在文档中找到您的占位符文本的位置,添加它并对其进行格式化。 如评论中所述,您只能按像素而不是英寸设置大小。

我只编写了必要的代码部分,而忽略了您的其他替换部分。可能有更简洁的方法来执行此操作,但这个对我有用。

function onFormSubmit(e) {
  var photo = e.values[5]
  // get the actual image based on the trimmed drive url
  var image = DriveApp.getFileById(photo.replace('https://drive.google.com/open?id=', ''))
  .getBlob();
  
  var copyId = DriveApp.getFileById('1tO7L1tWbwMvMyfJGCQB2cONOCyai_ZCIYt_VnDBBnBo')
  .makeCopy('Application'+'_2020-'+fullname)
  .getId();

  // you can get the body in one go
  var copyBody = DocumentApp.openById(copyId).getBody();

  // find the position where to add the image
  var position = copyBody.findText('keyphoto').getElement()
  var offset = copyBody.getChildIndex(position.getParent())
  
  // add the image below and resize it
  var insertedImage = copyBody.insertImage(offset +1,image).setHeight(100).setWidth(100)

  // align it to the right 
  var styles = {};
  styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT;
  insertedImage.getParent().setAttributes(styles)
  
  // remove the keyphoto placeholder text
  position.removeFromParent()
}

有关方法的更多资源: Inserting a Google Drive image into a document using Google Apps Script Google apps script: How to horizontally align an inlineImage