Google Apps Script: How to create slides on Google Slides and fill the placeholders in it with information from an array?

妖精的绣舞 提交于 2020-01-05 05:30:08

问题


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.

The array is as the following:

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

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

  • the name of the project,
  • the status of the project,
  • some random information about the project.

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

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);   

}

}

PROBLEM 1: How to do this when the number of elements in the array may vary?

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...).

PROBLEM 2: I do not know how to properly use the objectIds in the request

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?

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

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.


回答1:


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.

Answering your questions:

PROBLEM 1: How to do this when the number of elements in the array may vary?

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.

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.

PROBLEM 2: I do not know how to properly use the objectIds in the request

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",
       },
    ],
  }
}

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.

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



来源:https://stackoverflow.com/questions/47415525/google-apps-script-how-to-create-slides-on-google-slides-and-fill-the-placehold

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!