【问题标题】:Aligning text in the center of a text box using Google app script for slides使用幻灯片的 Google 应用脚本在文本框的中心对齐文本
【发布时间】: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


    【解决方案1】:

    在您的脚本中,请按如下方式修改它。

    从:

    textBox.getText().getParagraphs()[0].setAlignment(alignment);
    

    到:

    textBox.getText().getParagraphStyle().setParagraphAlignment(alignment);
    

    笔记:

    • 在您的脚本中,[] 似乎被转义了。我很担心这个。所以,我想按如下方式添加修改后的脚本。

    function testinsertTextOnImage() {
      const fileId = "###";
      const sheetId = "###";
    
      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().getParagraphStyle().setParagraphAlignment(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);
    }

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多