且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Apps脚本:如何在Google幻灯片上创建幻灯片,并使用数组中的信息填充其中的占位符?

更新时间:2023-02-14 09:19:35

首先是一个建议:您正在使用高级Google幻灯片服务和内置的

First a suggestion: you're using a mix of the Advanced Google Service for Slides and the built-in Slides service (SlidesApp). It would be a lot simpler if you used SlidesApp only, as it should do what you need and you don't need to worry about getting the objectIds right.

回答您的问题:

这里的问题是您的布局中只有12个占位符.尝试填充数量超过该数量的方法将无济于事,因为您将用尽所有占位符以进行填充.您可以选择以下两种方式之一:不使用占位符,而是创建和定位文本框,或者以其他方式显示数据.例如,每个项目使用1张幻灯片,带有3个占位符,一个占位符用于项目名称,状态和信息.

The problem here is that your layout only has 12 placeholders in it. Trying to fill more than that number of them isn't going to work, as you're going to run out of placeholders to fill. Your options here are either: don't use placeholders and create and position text boxes instead, OR present your data differently. For example, use 1 slide per project with 3 placeholders, one for project name, status, and infos.

我建议您手动创建一个示例幻灯片,以设计最终幻灯片的外观,并向后工作以编写代码以生成它.通常,在带有{{project_1}}: {{status_1}}之类的可变字符串的幻灯片上执行ReplaceAllText效果很好.

I recommend you create a sample slide by hand to design what you want your final slide to look like, and work backwards to write the code to generate it. Often doing ReplaceAllText on a slide with variable strings like {{project_1}}: {{status_1}} will work really well.

您在正确的道路上尝试为占位符提供对象ID,然后使用InsertText设置内容.我认为您的问题是placeholderIdMappings不能获得权利,您需要一个映射对象列表,而不是多次指定placeholderIdMappings键(请参阅此

You're on the right track to try to give your placeholders an object ID and then use InsertText to set the content. I think your problem is that the placeholderIdMappings arent right, you want a list of mapping objects, not specifying the placeholderIdMappings key multiple times (see this sample).

{
  "createSlide": {
    "objectId": pageId,
    "slideLayoutReference": {
      "layoutId": "layoutId"
    },
    "placeholderIdMappings": [
      {
        "layoutPlaceholder": {
          "type": "SUBTITLE",
          "index": 0
        },
        "objectId": "project1Id",
       },
      {
        "layoutPlaceholder": {
          "type": "BODY",
          "index": 0
        },
        "objectId": "project1Id",
       },
    ],
  }
}

问题3:是否可以不必写12行来填充占位符?

我认为这主要是由于您如何构造项目数据,幻灯片的布局以及placeholderIdMappings问题.修复这些问题应该使它更易于处理.

PROBLEM 3: Is it possible to not have to write the 12 lines to fill placeholders?

I think this is mostly due to how you've structured your project data, the the layout of your slide, and the placeholderIdMappings issue. Fixing those should make this a little easier to deal with.

使用SlidesApp也应该更容易(使用

It should also be easier to deal with with SlidesApp (use Slide#getPlaceholder)