使用 Google Apps 脚本将图像插入 Google 幻灯片时如何设置图像标题和说明?

How can I set an image title and description when inserting an image into a Google slide using Google Apps Script?

背景

我查看了 Google API 中的图像,我对使用 getDescription() 和 getTitle() 的两种方法感兴趣。这两种方法都出现在我的 appscript 的自动填充中,并且似乎都有效。

问题

在 Google 幻灯片中插入图像时,我找不到设置图像描述和标题的方法。

这是插入图片的方法,但没有标题或描述的参数。

currentPage.insertImage(imageUrl, left, top, width, height)

问题

使用 Google Apps 脚本将图像插入 Google 幻灯片时如何设置图像标题和说明?

虽然我不确定这个解决方法是否对您的情况有用,但这个答案怎么样?我总是使用 Slides API 添加标题和描述,因为我在 SlidesApp 中找不到这些方法。示例脚本如下。在这种情况下,我使用了 Slides API.

的 batchUpdate

示例脚本:

var id = currentPage.insertImage(imageUrl, left, top, width, height).getObjectId();
SlidesApp.getActivePresentation().saveAndClose(); // Important

var resource = {"requests": [
  {"updatePageElementAltText": {
    "objectId": id,
    "description": "sampleDescription",
    "title": "sampleTitle"
  }
}]};
Slides.Presentations.batchUpdate(resource, presentationId);
var fields = {fields: "pageElements(description,objectId,title)"};
var ele = Slides.Presentations.Pages.get(presentationId, currentPage.getObjectId(), fields).pageElements;
ele.forEach(function(e){
  if (e.objectId == id) {
    Logger.log(e)
  }
});

注:

  • 当你使用这个脚本时
    • 请在高级 Google 服务和 API 控制台启用幻灯片 API。
    • 请准备presentationIdcurrentPage
    • 插入图片后请使用saveAndClose()

参考:

2018 年 11 月 20 日更新:

The Slides service was updated at Google's update at November 14, 2018, and several methods were added for achieving this issue.

new methods let you add alt titles and alt descriptions to page elements. The following methods have been added to the Group, Image, Line, PageElement, Shape, SheetsChart, Table, Video, and WordArt classes

  • setDescription(description)
  • setTitle(title)