0

I am creating sheet add-on to print name tags from Google Sheet list to Google Slides document. One slide would contain the name tag and company name. I am no t able to insert title when creating the slide.

var dataRangeNotation = 'Customers!A2:M6';
var values = SpreadsheetApp.openById('').getRange(dataRangeNotation).getValues();
var presentationId = ''
var requests = []
// For each record, create a new merged presentation.
for (var i = 0; i < values.length; ++i) {
  var row = values[i];
  var customerName = row[0]; // name in column 1
  var customerCompany = row[1]; // company description in column 2
  var pageId = Utilities.getUuid();
  var bodyId = Utilities.getUuid();
  var titleID = Utilities.getUuid();

  requests.push({
    'createSlide': {
      'objectId': pageId,
      'insertionIndex': 0,
      'slideLayoutReference': {
        'predefinedLayout': 'TITLE'
      },
      "placeholderIdMappings": [
      {
        "layoutPlaceholder": {
          "type": "TITLE",
          "index": 0
        },
        "objectId": titleID,
       },
      {
        "layoutPlaceholder": {
          "type": "BODY",
          "index": 0
        },
        "objectId": bodyId,
       },
      ],
    },
        {
      "insertText": {
        "objectId": titleID,
        "text": "This is my slide title",
      },
        },

  });


},
  var slide =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
  Logger.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);

Layout placeholerIdMappings does not work. I would not want to create slides and then loop the slides to change slides titles. Looking for way to insert in already to the request.

LaBSo
  • 1
  • 2

1 Answers1

0
var dataRangeNotation = 'Customers!A2:M6';
var values = SpreadsheetApp.openById('ss').getRange(dataRangeNotation).getValues();
var presentationId = 'xxx'
var requests = []
// For each record, create a new merged presentation.
for (var i = 0; i < values.length; ++i) { 
  var pageId = Utilities.getUuid();
  var bodyId = Utilities.getUuid();
  var subtitleID = Utilities.getUuid();
      requests.push(
        {
            "createSlide": {
              "objectId": pageId,
              "slideLayoutReference": {
                'predefinedLayout': 'TITLE'
              },  
            }
          });

}
  var slide =
      Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
  Logger.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);
  var preso = SlidesApp.openById(presentationId);

  for (var i = 0; i < values.length; ++i) {
  var row = values[i];
  var customerName = row[0]; // name in column 1
  var customerCompany = row[1];// case description in column 2
  var [title, subtitle] = preso.getSlides()[i].getPageElements();
  title.asShape().getText().setText(customerName);
  subtitle.asShape().getText().setText(customerCompany);
  }

This looping does the trick but is not suitable for large updates due the api-limits.

LaBSo
  • 1
  • 2