Google Apps脚本:如何在Google幻灯片上创建幻灯片,并使用数组中的信息填充其中的占位符? [英] Google Apps Script: How to create slides on Google Slides and fill the placeholders in it with information from an array?

查看:142
本文介绍了Google Apps脚本:如何在Google幻灯片上创建幻灯片,并使用数组中的信息填充其中的占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,正在尝试使用其中的信息来创建Google幻灯片演示文稿.我需要一些帮助,并且在可能的情况下,还向我指出一些我可以阅读的材料,以更好地理解这些问题的可能解决方案.

I have an array and am trying to use the information in it to create a Google Slides presentation. I would like some help and, if possible, also point me to some material that I could read to better understand the possible solutions to these problems.

数组如下:

var myArray = [[Project1,Status1,Info1],[Project2,Status2,Info2],...,[ProjectN,StatusN,InfoN]]

数组中的每个元素都引用一个不同的项目,并且包含:

Each element in the array refers to a different project and contains:

  • 项目名称
  • 项目状态
  • 有关该项目的一些随机信息.

它们都是字符串.数组中元素的数量可能会有所不同,但是它们将始终遵循此结构.

All of them are strings. The number of elements in the array may vary, but they will always follow this structure.

在演示文稿中,我在主布局中创建了一个新布局,并将其命名为"NEW LAYOUT".该布局有4个组,每个组中有3个占位符.假定每组占位符都填充有不同项目的信息,这意味着将用myArray[i][1]myArray[i][2]myArray[i][3]填充一组占位符.

In the presentation, I created a new layout inside the master layout that I named "NEW LAYOUT". That layout has 4 groups with 3 placeholders in each group. Each group of placeholders is supposed to be filled with the information of a different project, meaning that a group of placeholders will be filled with myArray[i][1], myArray[i][2] and myArray[i][3].

到目前为止,我的代码如下:

My code so far is the following:

function CreatingSlides() {

var displayName = "NEW LAYOUT"; // Name of the layout that I created
var presentationId = SlidesApp.openById(PageId).getId(); //It is an existing presentation, so I just get its ID
var layouts = Slides.Presentations.get(presentationId).layouts;
var layout = {};

//The following is the array of elements. In this example,
//I only used 4 projects, but it may vary and may not be
//a multiple of 4.
var myArray = [["Project1","Status1","Info1"],["Project2","Status2","Info2"],["Project3","Status3","Info3"],["Project4","Status4","Info4"]];


//This loop searches for the layout that I created
for (i in layouts) {
  layout[layouts[i].layoutProperties.displayName] = layouts[i].objectId;
  }

//In the slide created, this loop will fill the information in it using the myArray
for (i in myArray) {
  var resource = {"requests": [{"createSlide": {"slideLayoutReference": {"layoutId": layout[displayName]},
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 0},"objectId":myArray[i][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 0},"objectId":myArray[i][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 1},"objectId":myArray[i][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 1},"objectId":myArray[i+1][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 2},"objectId":myArray[i+1][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 3},"objectId":myArray[i+1][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 2},"objectId":myArray[i+2][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 4},"objectId":myArray[i+2][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 5},"objectId":myArray[i+2][3],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "SUBTITLE","index": 3},"objectId":myArray[i+3][1],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 6},"objectId":myArray[i+3][2],},],
                                                "placeholderIdMappings": [{"layoutPlaceholder": {"type": "BODY","index": 7},"objectId":myArray[i+3][3],},],
                                                }
                                },
                 {"insertText": {"objectId": myArray[i][1],"text": myArray[i][1],},
                  "insertText": {"objectId": myArray[i][2],"text": myArray[i][2],},
                  "insertText": {"objectId": myArray[i][3],"text": myArray[i][3],},
                  "insertText": {"objectId": myArray[i+1][1],"text": myArray[i+1][1],},
                  "insertText": {"objectId": myArray[i+1][2],"text": myArray[i+1][2],},
                  "insertText": {"objectId": myArray[i+1][3],"text": myArray[i+1][3],},
                  "insertText": {"objectId": myArray[i+2][1],"text": myArray[i+2][1],},
                  "insertText": {"objectId": myArray[i+2][2],"text": myArray[i+2][2],},
                  "insertText": {"objectId": myArray[i+2][3],"text": myArray[i+2][3],},
                  "insertText": {"objectId": myArray[i+3][1],"text": myArray[i+3][1],},
                  "insertText": {"objectId": myArray[i+3][2],"text": myArray[i+3][2],},
                  "insertText": {"objectId": myArray[i+3][3],"text": myArray[i+3][3],}},
                               ]};

Slides.Presentations.batchUpdate(resource, presentationId);   

}

}

问题1:当数组中元素的数量可能不同时,该怎么做?

在此代码中,我仅在myArray中使用4个元素,因此将填充在创建的幻灯片中的所有占位符.但是,如果元素数量大于4,我不知道如何告诉它创建新幻灯片.此外,元素数量可能不是4的倍数(可能是5,可能是60,可能是是72 ...).

In this code, I am using only 4 elements in the myArray and therefore all of the placeholders in the slide created would be filled. However, I do not understand how can I tell it to create a new slide if the number of elements is bigger than 4. Besides, the number of elements may not be a multiple of 4 (it may be 5, may be 60, may be 72...).

问题2:我不知道如何在请求中正确使用objectIds

我从Google Apps脚本参考中看到的另一个代码中获得了这一部分.在其中,我了解到,在请求内部,如果先为objectId设置一个名称,然后使用该名称在该占位符中写一些东西.但是,这会带来一些问题:objectId有时不接受我试图提供的名称,或者objectId表示它不能使用我建议的名称,因为它应该是唯一的.有更好的方法吗?

I got this part from another code I saw on the Google Apps Script reference. In it, I understood that, inside the request, if first sets a name for the objectId and then it uses that name to write something in that placeholder. However, this creates some problems: the objectId sometimes doesn't accept the name I am trying to give it or the objectId says that it can't use the name that I am suggesting because it should be unique. Is there a better way to do this?

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

我认为这仅是因为我没有正确地使用objectIds引起的(问题2).因此,我必须分别命名幻灯片中的每个objectId.我相信有某种方法可以使用循环来执行此操作,但是到目前为止,我还无法找到解决方案或理解它.

I think that this only happens because I do not how to properly use the objectIds (PROBLEM 2). Because of this, I have to name each objectId inside a slide individually. I believe that there is some way to use a loop to do this, but so far I haven't been able to find the solution or understand it.

推荐答案

首先是一个建议:您正在使用高级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)

这篇关于Google Apps脚本:如何在Google幻灯片上创建幻灯片,并使用数组中的信息填充其中的占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆