Search code examples
google-apps-scriptgoogle-sheets-apigoogle-slides-api

Error when using setContentAlignment and setParagraphAlignment methods of Slides Service


I have a container-bound script in Sheets that inserts a new table in a Slides page before copying data of a given Sheets range in that Slides table.

After copying the data (cell by cell) I want to set the vertical and horizontal alignments. For that I use setContentAlignment(TOP) and setParagraphAlignment(CENTER), but both of these return the following errors:

ReferenceError: "TOP" is not defined. (line 295, file "Code")

ReferenceError: "CENTER" is not defined. (line 296, file "Code")

The full code is as follows.

function test() {
  
  var rnFindings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Findings').getRange(2,2,4,2);
  var slPrez = SlidesApp.openById('1PdGCWmgUMxBnMIXgPEVQoyxm8EMvE-ub0eejV8ISmLk');
  var slSlide = slPrez.getSlides()[0];
  Logger.log(slSlide.getLayout().getLayoutName());
  
  copyTableToSlides(rnFindings, slPrez, slSlide);
        
}

function copyTableToSlides(rnTable, slPrez, slSlide) {

  var iRow = rnTable.getNumRows();
  var iCol = rnTable.getNumColumns();
  var peTitle = slSlide.getPageElements()[0];
    var iLeft = peTitle.getLeft();
    var iTop = (peTitle.getTop() * 2) + peTitle.getHeight();
      var iWidth = slPrez.getPageWidth() - (iLeft * 2);
  var slTable = slSlide.insertTable(iRow, iCol, iLeft, iTop, iWidth, 10); // hard-coding height lets table height fit to content

  // var starTableRange = create2DArray(iRow, iCol);
  
  for (r = 1; r <= iRow; r++) {
    for (c = 1; c <= iCol; c++) {
      slTable.getCell(r-1,c-1).getText().setText(rnTable.getCell(r,c).getValue());
    }
  }

  for (r = 1; r <= iRow; r++) {
    for (c = 1; c <= iCol; c++) {
      if (c == 1) {
        slTable.getCell(r-1,c-1).setContentAlignment(TOP);
        slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(CENTER);
      } else {
        slTable.getCell(r-1,c-1).setContentAlignment(TOP);
        slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(LEFT)
      }
    }
  }

}

According to Google's App Script Reference these are the right ENUM values. I can't figure out what else is going wrong here.

How do I set both the vertical and horizontal alignments of each cell in a Slides table, when my code is container-bound to a Sheets file?

Thanks in advance,

Vincent


Solution

  • How about this modification?

    From:

    if (c == 1) {
      slTable.getCell(r-1,c-1).setContentAlignment(TOP);
      slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(CENTER);
    } else {
      slTable.getCell(r-1,c-1).setContentAlignment(TOP);
      slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(LEFT)
    }
    

    To:

    if (c == 1) {
      slTable.getCell(r-1,c-1).setContentAlignment(SlidesApp.ContentAlignment.TOP);
      slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);
    } else {
      slTable.getCell(r-1,c-1).setContentAlignment(SlidesApp.ContentAlignment.TOP);
      slTable.getCell(r-1,c-1).getText().getParagraphStyle().setParagraphAlignment(SlidesApp.ParagraphAlignment.START);
    }
    
    • Please use SlidesApp.ContentAlignment as enum for setContentAlignment.
    • Please use SlidesApp.ParagraphAlignment as enum for setParagraphAlignment.

    Note:

    • There is no value of LEFT of setParagraphAlignment(LEFT). So in your case, is that START? If this is not what you want, please modify it.

    References:

    If I misunderstood your question and this was not the result you want, I apologize.