Currently, I am tackling a project that demands the center alignment of text within a textbox on a google slide using a google app script.
The primary objective is to fetch text from a google worksheet and an image from google drive. Subsequently, combine the extracted worksheet text and the image onto a google slide with the same resolution as the image. Once this is done, export the slide to generate a new image and store it in google drive.
I have managed to make the code functional, which is fantastic. However, I'm encountering an error when attempting to align the fetched texts at the center of their respective textboxes. I would greatly appreciate any assistance with this matter.
In implementing this, I made use of the libraries: "DocsServiceApp", "ImgApp," and ensured that the "slidesAPI" is enabled.
`function testinsertTextOnImage() {
const fileId = "1PU6GexJ....tiNkwRum2xYxYP";
const sheetId = "1_5bZsO6YJb8eS....9sgIao9C1uYxt_z2GBDjw";
const columns = ["A", "B", "C"];
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName("Blad1");
const lastRow = sheet.getLastRow();
const texts = columns.map((column) => sheet.getRange(column + lastRow).getValue());
const textProperties = [
{
text: texts[0],
left: 500,
top: 475,
width: 880,
height: 80,
fontSize: 150,
fontFamily: "Tangerine",
alignment: SlidesApp.ParagraphAlignment.CENTER,
},
{
text: texts[1],
left: 320,
top: 660,
width: 1000,
height: 120,
fontSize: 30,
fontFamily: "Questrial",
alignment: SlidesApp.ParagraphAlignment.CENTER,
},
{
text: texts[2],
left: 920,
top: 830,
width: 180,
height: 60,
fontSize: 30,
fontFamily: "Questrial",
alignment: SlidesApp.ParagraphAlignment.CENTER
},
];
const file = DriveApp.getFileById(fileId);
const blob = file.getBlob();
const size = ImgApp.getSize(blob);
const presentation = {
title: "Result",
width: { unit: "pixel", size: size.width },
height: { unit: "pixel", size: size.height },
};
const presentationId = DocsServiceApp.createNewSlidesWithPageSize(presentation);
const slides = SlidesApp.openById(presentationId);
const slide = slides.getSlides()[0];
slide.insertImage(blob);
textProperties.forEach(({ text, left, top, width, height, fontSize, fontFamily, alignment }) => {
const textBox = slide.insertTextBox(text, left, top, width, height);
const textStyle = textBox.getText().getTextStyle();
textStyle.setFontSize(fontSize).setFontFamily(fontFamily);
textBox.getText().getParagraphs()[0].setAlignment(alignment);
});
slides.saveAndClose();
const obj = Slides.Presentations.Pages.getThumbnail(
presentationId,
slide.getObjectId(),
{
"thumbnailProperties.thumbnailSize": "LARGE",
"thumbnailProperties.mimeType": "PNG",
}
);
const url = obj.contentUrl.replace(/=s\d+/, "=s" + size.width);
const resultBlob = UrlFetchApp.fetch(url)
.getBlob()
.setName("Result_" + file.getName());
DriveApp.createFile(resultBlob);
DriveApp.getFileById(presentationId).setTrashed(true);
}`