Quasyhumonoid
Quasyhumonoid

Reputation: 3

Can I divide a text box by paragraph in Google Slides using Apps Script?

I am trying to design some code in Apps Script that can be put on any Google Slides presentation and split every text box by paragraphs so every paragraph has its own text box.

I started out using var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300); to make the new text boxes like google describes to use in most of its tutorials but it 'couldn't identify the TEXT_BOX type' so I found .insertTextBox and that seems to work better but I've found other problems.

I can use .getParagraphs to find the number of paragraphs in a text box but I can't tell if it doesn't include the contents of each paragraph or if I'm just not using the correct command to get the text from the paragraph. I have also tried to find an alternative to find the beginning of each paragraph and divide the text from there but I can't find a command for that either. Maybe would I have to use .indexOf to find each /n or /r, or is there a simpler way?

I'm also having a problem where my equations to divide up the text box size are giving me undefined answers and I've tried declaring the variables as numbers but it just makes things worse.

function myFunction() { // get slides in the presentation and establish 'for' variables
    var slide = SlidesApp.getActivePresentation().getSlides();
    var i;
    var j;
    var k;
    for (i = 0; i < slide.length; i++) { // get the text boxes on each slide
      var text = slide[i].getShapes(); 
      for (j = 0; j < text.length; j++) { // get the location of and the paragraphs in each textbox (locations don't work)
        var top = text[j].getTop;
        var left = text[j].getLeft;
        var width = text[j].getWidth;
        var height = text[j].getHeight;
        var paragraph = text[j].getText().getParagraphs(); 
        for (k = 0; k < paragraph.length; k++){ // make a textbox for each paragraph distributed vertically over the original textbox
          var content = text[j].getRange(paragraph[k]); //I was hoping this would fill with the contents of current paragraph
          var shapeheight = height / paragraph.length; //NaN and I don't know why
          var shapetop = height * k + top; //also doesn't work these should all be numbers
          slide[i].insertTextBox(content, left, shapetop, width, shapeheight);
        }
        text[j].remove(); //delete original textbox on slide
      }
    }
}

Here are pictures of what I'm trying to do:

Slide before intended changes

Approximate slide after intended changes

Upvotes: 0

Views: 3118

Answers (1)

Tanaike
Tanaike

Reputation: 201388

I believe your goal as follows.

  • You want to split each paragraph in a text box as each text box on Google Slides.
  • You want to achieve this using Google Apps Script.

Modification points:

  • In your script,
    • getTop, getLeft, getWidth and getHeight are the method. So please add ().
    • About var content = text[j].getRange(paragraph[k]), getRange has no arguments.
    • About var shapeheight = height / paragraph.length, in this case, this can be put outof the for loop.
    • About var shapetop = height * k + top, in this case, that might be var shapetop = shapeheight * k + top.

When above points are reflected to your script, it becomes as follows.

Modified script:

function myFunction() {
  var slide = SlidesApp.getActivePresentation().getSlides();
  var i;
  var j;
  var k;
  for (i = 0; i < slide.length; i++) {
    var text = slide[i].getShapes();
    for (j = 0; j < text.length; j++) {
      var top = text[j].getTop(); // Modified
      var left = text[j].getLeft(); // Modified
      var width = text[j].getWidth(); // Modified
      var height = text[j].getHeight(); // Modified
      var paragraph = text[j].getText().getParagraphs();
      var shapeheight = height / paragraph.length; // Modified
      for (k = 0; k < paragraph.length; k++) {
        var content = paragraph[k].getRange().asString(); // Modified
        var shapetop = shapeheight * k + top; // Modified
        slide[i].insertTextBox(content, left, shapetop, width, shapeheight);
      }
      text[j].remove();
    }
  }
}

Note:

  • In the current stage, it seems that AutoFit cannot be set. By this, when slide[i].insertTextBox(content, left, shapetop, width, shapeheight) is used, the text deviates a little from the box. So in this case, how about not using shapeheight? In this case, please modify slide[i].insertTextBox(content, left, shapetop, width, shapeheight); as follows.

      var t = slide[i].insertTextBox(content);
      t.setLeft(left);
      t.setTop(shapetop);
      t.setWidth(width);
    

References:

Upvotes: 1

Related Questions