用图像(PNG格式)在Google文档中的身体中替换文本
我需要在Google文档中替换图像(PNG格式)中的文本。我尝试了七种方法,但似乎存在一个问题,从png文件中获得斑点(强制使用该格式为png,因为是从appsheet中的签名生成的)。我尝试了getas('image/jpeg'),但它不起作用
function myFunction() {
var replaceTextToImage = function(body, searchText, image, width) {
var next = body.findText(searchText);
if (!next) return;
var r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
return next;
};
var folder = DriveApp.getFolderById('1uMkFrLzT6rrGngS4f7vWUJCK8GJwrvF6');
var files = folder.getFilesByName(imagen);
var fileId = files.hasNext() ? files.next().getId() : "";
Logger.log(fileId)
var image = DriveApp.getFileById(fileId).getBlob(;
do {
var next = replaceTextToImage(body,'{{firma}}', image, 200);
} while (next);
}
I need to replace a text in google doc for an image (PNG format). I tried sevaral ways but it seems that there is a problem getting blob from a PNG file (is mandatory that the format is PNG because is generated from a signature in Appsheet). I tried getAs('image/jpeg') but it didnt work
function myFunction() {
var replaceTextToImage = function(body, searchText, image, width) {
var next = body.findText(searchText);
if (!next) return;
var r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
return next;
};
var folder = DriveApp.getFolderById('1uMkFrLzT6rrGngS4f7vWUJCK8GJwrvF6');
var files = folder.getFilesByName(imagen);
var fileId = files.hasNext() ? files.next().getId() : "";
Logger.log(fileId)
var image = DriveApp.getFileById(fileId).getBlob(;
do {
var next = replaceTextToImage(body,'{{firma}}', image, 200);
} while (next);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论