【发布时间】:2023-02-04 10:18:27
【问题描述】:
我正在从事一个项目,该项目需要使用谷歌应用程序脚本将文本对齐到谷歌幻灯片上文本框的中心。
目标是从谷歌工作表中提取文本。然后从谷歌驱动器中提取图像并添加提取的工作表文本,方法是将两者添加到具有图像分辨率的谷歌幻灯片中。在此之后,导出幻灯片以创建新图像,并将其添加到谷歌驱动器。
该代码确实有效,这很棒,但是当我尝试将拉出的文本对齐到文本框的中间时,我不断收到错误消息。希望有人能帮助我。
我使用了库:“DocsServiceApp”、“ImgApp”,并启用了“slidesAPI”
`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);
}`
【问题讨论】:
标签: javascript java google-apps-script google-sheets google-slides-api