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.