-
Notifications
You must be signed in to change notification settings - Fork 1
/
googleSlides.ts
121 lines (105 loc) · 3.24 KB
/
googleSlides.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { applyFormattingToTextStyle, TextFormatting } from "./formatting";
export type ShapePositionArgs = {
left: number;
top: number;
width: number;
height: number;
};
export function insertImage(
slide: GoogleAppsScript.Slides.Slide,
{ left, top, width, height }: ShapePositionArgs,
url: string,
) {
return slide.insertImage(url, left, top, width, height);
}
export function getSlideUrl(
slideId: string,
presentation: GoogleAppsScript.Slides.Presentation,
): string {
const baseUrl = presentation.getUrl();
return `${baseUrl}#slide=id.${slideId}`;
}
export function removeShapesAndImages(
slide: GoogleAppsScript.Slides.Slide,
): void {
const shapes = slide.getShapes();
const images = slide.getImages();
const tables = slide.getTables();
shapes.forEach((shape) => shape.remove());
images.forEach((image) => image.remove());
tables.forEach((table) => table.remove());
}
export function insertTextBox(
slide: GoogleAppsScript.Slides.Slide,
formatting: TextFormatting = {},
{ left, top, width, height }: ShapePositionArgs,
text: string,
) {
const {
backgroundColor,
alignment = SlidesApp.ContentAlignment.TOP,
paragraphAlignment = SlidesApp.ParagraphAlignment.START,
}: TextFormatting = formatting;
const textBox = slide.insertShape(
SlidesApp.ShapeType.TEXT_BOX,
left,
top,
width,
height,
);
const textStyle = textBox.getText().setText(text).getTextStyle();
applyFormattingToTextStyle(textStyle, formatting);
if (backgroundColor) textBox.getFill().setSolidFill(backgroundColor);
if (backgroundColor === null) textBox.getFill().setTransparent();
textBox.setContentAlignment(alignment);
textBox
.getText()
.getParagraphStyle()
.setParagraphAlignment(paragraphAlignment);
return textBox;
}
export function insertImageIntoSlide(
imageBlob: GoogleAppsScript.Base.BlobSource,
altText: string,
slide: GoogleAppsScript.Slides.Slide,
left: number,
currentTop: number,
textBoxHeight: number,
width: number,
) {
const image = slide.insertImage(
imageBlob,
left,
currentTop + textBoxHeight + 10,
width,
textBoxHeight,
);
image.setTitle(altText);
// Adjust the image size if necessary
if (image.getWidth() > width) {
const ratio = width / image.getWidth();
image.setWidth(width);
image.setHeight(image.getHeight() * ratio);
}
}
export function insertTableIntoSlide(
slide: GoogleAppsScript.Slides.Slide,
numRows: number,
numCols: number,
{ left, top, width, height }: ShapePositionArgs,
textFormatting: TextFormatting = {},
) {
const table = slide.insertTable(numRows, numCols, left, top, width, height);
// Apply the provided text formatting to each cell in the table
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const cellElement = table.getCell(row, col);
const textRange = cellElement.getText();
applyFormattingToTextStyle(
textRange.getTextStyle(),
textFormatting,
);
}
}
return table;
}