1
votes

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

1
Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, I apologize. At that time, can I ask you about your current situation? I would like to study to solve your issues. - Tanaike
Thanks Tanaike! It does look like you found the solution to my problem. I'm in the process of implementing at the moment, however this became part of a bigger set of functions since, so I have not yet been able to test. As soon as I do the first test and confirm that your solution works, I'll mark it as accepted! - Vincent Courtemanche

1 Answers

1
votes

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.