【问题标题】:How align an appended image in Google Docs using Apps Script如何使用 Apps 脚本对齐 Google Docs 中的附加图像
【发布时间】:2020-05-14 01:55:57
【问题描述】:

此脚本从 html 元素中获取一个值,并将其作为 textappendText() 和 QR 码 codeappendInlineImage() 递增地附加到 gdocs 中。到目前为止,它已经完成了这项工作。

现在我一直在尝试自动对齐目标 gDocs 中的文本和图像,但我无法正确使用语法。

我已经附上了我当前结果和预期结果的图片。

到目前为止,这是我的脚本。

function createQRCodeSingleItem(fieldData) {
var doc = DocumentApp.openById('1NRSSD_gdfgdfgergdfgdfgdf');
var body = doc.getBody();
var qrCode = fieldData.productId.toString();

 //Logger.log(qrCode)

var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + qrCode
var resp = UrlFetchApp.fetch(url); // Get the image of QR code
body.getChild(0).asParagraph().appendText(qrCode);// QR Code referencence number
body.getChild(0).asParagraph().appendInlineImage(resp.getBlob());// OR code
var msg = 'Item INCLUIDO com sucesso!'
    Logger.log(msg);
      return msg;

 }

【问题讨论】:

    标签: google-apps-script web-applications google-docs


    【解决方案1】:

    看看this example doc

    您可以使用addPositionedImage() 来对齐相关段落的图像。

     function createQRCodeSingleItem() {
      var doc = DocumentApp.getActiveDocument();
      var body = doc.getBody();
      var qrCode = '990';  // sample qrCode
    
      var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + qrCode
      var resp = UrlFetchApp.fetch(url); // Get the image of QR code
      // Get or create your desired paragraph
      var paragraph = body.appendParagraph(qrCode).setAlignment(DocumentApp.HorizontalAlignment.LEFT);
      // add positioned image with offsets... Figure out your optimal offset points values
      paragraph.addPositionedImage(resp.getBlob()).setTopOffset(-50).setLeftOffset(75);
    }
    

    这里是Apps Script

    【讨论】:

    • 您好,您的建议适用于第一个布局。但是当我向列表中添加其他值时,它不会附加到同一行。插入它只是与第一个图像段落重叠。addPositionedImage(resp.getBlob()).setTopOffset(-50).setLeftOffset(20);我需要设置某种模板吗?
    • 偏移量从它们绑定的段落开始工作。因此,您应该为每 4 张左右的图像创建一个新段落。否则它们都将从同一个起点偏移。 setLeftOffset()setTopOffset()
    • 我理解逻辑,但我的编码知识令人恐惧。我尝试了一些变体,包括这个 body.getChild(0).asParagraph().appendText(qrCode);但我不知道在哪里。我正在查看文档页面,看看我是否可以实施您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多