Google Apps Script getWidth() for document table cell width returns null

杀马特。学长 韩版系。学妹 提交于 2020-08-26 07:43:29

问题


I'd like to insert image in the Google document by using Google Apps Script, and to spread it (the image) to the width of the page - margins or to insert it in the table cell and spread it to the width of the cell.

The code goes like this:

var image = DriveApp.getFileById(fileId).getBlob();
var insertPlace = body.findText(searchText).getElement();
insertPlace.asText().setText("");
var insertedImage = insertPlace.getParent().asParagraph().insertInlineImage(0, image);
var h = insertedImage.getHeight();
var w = insertedImage.getWidth();
insertedImage.setWidth(width).setHeight(width * h / w);

And where is the problem?

width!

How to find width of the table cell or body?

I have tried with

var width = insertPlace.getParent().asParagraph().asTableCell().getWidth();

but got null although cell is in the single column of the table, and table takes all the width of the page-margins.

Anyone got the idea about how to get width of the cell?

Also, how to get page width, margins etc...

UPDATE

I have discovered that the issue must be with the Google code about this. Actually, they have made differently handling with tables in slides compared to document:

https://developers.google.com/apps-script/reference/slides/table-cell https://developers.google.com/apps-script/reference/document/table-cell

In slides version there is, for example, getParentColumn(), which is not available for the document... any many similar discrepancies.

What are doing these guys at Google?!?!?


回答1:


This is not perfect but it seems to get me pretty close. May be you could work with it some more and get the rest of the way. I thought that the PAGE_WIDTH and PAGE_HEIGHT were in points but when I look at the number it seemed rather large so I used them as pixels instead. And it appears to work for me the image is centered in the page and goes from left margin to right margin. I didn't adjust the page height because my image looked ok but you might wish to do that.

function insertImage() {
  var doc=DocumentApp.getActiveDocument();
  var body=doc.getBody();
  var atts=body.getAttributes();
  for(var att in atts) {
    Logger.log(att + ':' + atts[att]);
  }
  var folderId="Folder ID";
  var files=DriveApp.getFolderById(folderId).getFiles();
  while(files.hasNext()) {
    var file=files.next();
    if(file.getName()=="Image File Name") {
      var image=file.getBlob().getAs('image/png');//may need to change content type
      var img=body.appendImage(image);
      var h=img.getHeight();
      var w=img.getWidth();
      var mr=atts['MARGIN_RIGHT'];
      var ml=atts['MARGIN_LEFT'];
      var sw=atts['PAGE_WIDTH'];
      var sh=atts['PAGE_HEIGHT'];
      img.setHeight(sw*h/w);
      img.setWidth((sh-mr-ml)*w/h);
    }
  }
}


来源:https://stackoverflow.com/questions/58675314/google-apps-script-getwidth-for-document-table-cell-width-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!