简体   繁体   中英

How align an appended image in Google Docs using Apps Script

This script gets a value from an html element and appends it into a gdocs as a textappendText() and a QR code codeappendInlineImage() incrementally. It does the job so far.

Now I've been trying to align the text and the image in the destination gDocs automatically, but I can't get the syntax right.

I've included an image of my current results and expected results. 示例图像

This is my script so far.

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;

 }

Take a look at this example doc .

You can align the image using the addPositionedImage() for the paragraph in question.

 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);
}

Here is the Apps Script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM