2014-10-08 3 views
1

다음 코드는 더 큰 프로그램의 일부입니다. Google 드라이브의 이미지를 Google 문서에 삽입하고 크기를 조정하고 가운데에 정렬하려고합니다. 지금까지 프로그램에 이미지를 삽입하고 크기를 조정할 수 있지만 인라인 이미지를 중앙에 배치하는 방법을 알지 못합니다. 나는 Google Apps 스크립트를 처음 사용하며 기본적으로 다른 사람들의 예제를 복사하여 수정했습니다. 귀하의 도움을 주시면 감사하겠습니다. 내가 분명히해야하는지 알려줘. 다시, 나는 inlineImage (var inlineI)를 CENTER하려고 시도하고있다. 감사!Google 앱 스크립트 : 인라인 이미지를 가로로 정렬하는 방법

var GDoc = DocumentApp.openByUrl("URL"); //I deleted my actual URL 

function insertImageFromDrive(){ 
var img = DriveApp.getFileById(myImageFileID).getBlob(); //I deleted my actual image ID 
var inlineI = GDoc.appendImage(img); //insert image 

    //resizing the image 
    var width = inlineI.getWidth(); 
    var newW = width; 
    var height = inlineI.getHeight(); 
    var newH = height; 
    var ratio = width/height; 
    Logger.log('w='+width+'h='+height+' ratio='+ratio); 
    if(width>320){ 
     //max width of image 
     newW = 320; 
     newH = parseInt(newW/ratio); 
    } 
    inlineI.setWidth(newW).setHeight(newH); //resizes the image but also needs to center it 
} 

답변

0

이미지가 포함 된 단락을 가운데 맞춤해야합니다. 그것을 할이 코드를 추가

var styles = {}; 
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; 
inlineI.getParent().setAttributes(styles); 

getParent() 방법은 이미지를 포함하는 컨테이너 요소 (단락)를 가져옵니다. setAttributes() 메서드는 custom style attributes (이 경우 가운데 정렬)을 요소에 적용합니다.