需要使用Google脚本将图像从文档复制到另一个文档

时间:2019-07-25 18:12:52

标签: google-apps-script

我正在尝试从Google文档的“模板”中复制图像,然后使用脚本粘贴到另一个文档中。已经在网络上搜索了一些解决方案,但没有一个对我有用。这是我的代码,这得到了无效的图像数据错误。

function creatingLabels(link, document, model, labelTemplate) {
    var headerLabel = labelTemplate.getBody().getImages();

    Logger.log(headerLabel.toString());
    Logger.log(headerLabel);

    var textLabel = labelTemplate.getBody().getText();

    var text = textLabel.replace(' %LOCAL%', model);

    var QrCode = getImageFromURL(link);

    document.getBody().insertImage(1, headerLabel)
    labelTemplate.getBody().setText(text);
    labelTemplate.getBody().insertImage(1, QrCode);
}

function getImageFromURL(link) {
    var url = encodeURI(link)

    return UrlFetchApp.fetch(url, { muteHttpExceptions: true });
}

1 个答案:

答案 0 :(得分:1)

此功能从一个文档复制图像并创建另一个文档,然后将该图像附加到新文档。它还在对话框上显示图像。如果您要找的图像找不到,请在根文件夹中查找。

function copyImage() {
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  var images=body.getImages();
  var image=images[0];
  var b64Url='data:' + image.getBlob().getContentType() + ';base64,' + Utilities.base64Encode(image.getBlob().getBytes());
  var html=Utilities.formatString('<img src="%s" width="640" height="480" />',b64Url);
  var userInterface=HtmlService.createHtmlOutput(html).setWidth(700).setHeight(550);
  DocumentApp.getUi().showModelessDialog(userInterface, 'Images');
  var doc1=DocumentApp.create('SO2');
  doc1.getBody().appendImage(image.getBlob());
  var image=doc1.getBody().getImages()[0];
  image.setWidth(640);
  image.setHeight(480);
  doc1.saveAndClose();
}
相关问题