-
Notifications
You must be signed in to change notification settings - Fork 1
/
Resizing and Centering Images in Google Docs with Google Apps Script
48 lines (39 loc) · 1.73 KB
/
Resizing and Centering Images in Google Docs with Google Apps Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function resizeAndCenterImages() {
// Open the document by ID. Replace with your document ID.
var docId = 'YOUR_DOCUMENT_ID_HERE';
var doc = DocumentApp.openById(docId);
var body = doc.getBody();
// Get the page width in points
var pageWidth = doc.getPageWidth();
var maxImageWidth = pageWidth * 0.75;
// Get all the elements in the document
var totalElements = body.getNumChildren();
for (var i = 0; i < totalElements; i++) {
var element = body.getChild(i);
if (element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var paragraph = element.asParagraph();
var numChildren = paragraph.getNumChildren();
for (var j = 0; j < numChildren; j++) {
var child = paragraph.getChild(j);
if (child.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var image = child.asInlineImage();
var originalWidth = image.getWidth();
var originalHeight = image.getHeight();
// Calculate the new dimensions maintaining the aspect ratio
var newWidth = maxImageWidth;
var newHeight = (newWidth / originalWidth) * originalHeight;
// Resize the image
image.setWidth(newWidth);
image.setHeight(newHeight);
// Center the image by wrapping it in a centered paragraph
paragraph.removeChild(image);
var centeredParagraph = body.insertParagraph(i + 1, "");
centeredParagraph.appendInlineImage(image);
centeredParagraph.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
i++; // Adjust index to account for newly inserted paragraph
break; // Exit inner loop after processing the image
}
}
}
}
}