Javascript 如何从Google Form upload为照片编写脚本,并将其大小设置为1英寸以便在模板中显示?
我正在申请一份要求附上申请人照片的申请书。如何从Google Form upload为照片编写脚本,并将其大小设置为1英寸以便在模板中显示?非常感谢:) 我的代码Javascript 如何从Google Form upload为照片编写脚本,并将其大小设置为1英寸以便在模板中显示?,javascript,forms,google-apps-script,google-sheets,doc,Javascript,Forms,Google Apps Script,Google Sheets,Doc,我正在申请一份要求附上申请人照片的申请书。如何从Google Form upload为照片编写脚本,并将其大小设置为1英寸以便在模板中显示?非常感谢:) 我的代码 函数onFormSubmit(e){ var fullname=e.values[1]; var地址=e.值[2]; var phone=e.values[3]; var email=e.values[4]; var photo=e.值[5]; var copyId=DriveApp.getFileById('1to7l1twbw
函数onFormSubmit(e){
var fullname=e.values[1];
var地址=e.值[2];
var phone=e.values[3];
var email=e.values[4];
var photo=e.值[5];
var copyId=DriveApp.getFileById('1to7l1twbwmvfjgcqb2conocyai\u ZCIYt\u vndbbnbbo')
.makeCopy('Application'+''u 2020-'+全名)
.getId();
var copyDoc=DocumentApp.openById(copyId);
copyDoc.getBody()
var copyBody=copyDoc.getActiveSection();
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()
}
关于方法的进一步资源:
如何编写照片脚本。。。将其大小设置为1英寸取决于您使用的显示器。计算机不以英寸(或其他更合理的单位)为单位,图形以像素为单位。将完全取决于屏幕的像素密度。非常感谢:)