我可以使用 Apps Script 在 Google 幻灯片中按段落划分文本框吗?

时间:2021-05-22 18:06:16

标签: javascript google-apps-script google-slides

我正在尝试在 Apps Script 中设计一些代码,这些代码可以放在任何 Google 幻灯片演示文稿中,并按段落拆分每个文本框,以便每个段落都有自己的文本框。

我开始使用 var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300); 来制作像 google 描述的新文本框使用 in most of its tutorials 但它“无法识别 TEXT_BOX 类型”所以我找到了 .insertTextBox,这似乎效果更好但我发现了其他问题。

我可以使用 .getParagraphs 来查找文本框中的段落数,但我不知道它是否不包含每个段落的内容,或者我只是没有使用正确的命令来获取文本从段落。我还试图找到一种替代方法来找到每个段落的开头并将文本从那里分开,但我也找不到相应的命令。也许我必须使用 .indexOf 来查找每个 /n 或 /r,还是有更简单的方法?

我也遇到了一个问题,我的用于划分文本框大小的方程给出了未定义的答案,我曾尝试将变量声明为数字,但这只会让事情变得更糟。

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
      }
    }
}

以下是我正在尝试做的事情的图片:

Slide before intended changes

Approximate slide after intended changes

1 个答案:

答案 0 :(得分:1)

我相信你的目标如下。

  • 您希望将文本框中的每个段落拆分为 Google 幻灯片上的每个文本框。
  • 您希望使用 Google Apps 脚本来实现这一目标。

修改点:

  • 在您的脚本中,
    • getTopgetLeftgetWidthgetHeight 是方法。所以请添加()
    • 关于 var content = text[j].getRange(paragraph[k])getRange 没有参数。
    • 关于 var shapeheight = height / paragraph.length,在这种情况下,可以将其置于 for 循环之外。
    • 关于 var shapetop = height * k + top,在本例中,可能是 var shapetop = shapeheight * k + top

当以上几点反映到你的脚本中时,它变成如下。

修改后的脚本:

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();
    }
  }
}

注意:

  • 现阶段好像不能设置AutoFit。这样,当使用 slide[i].insertTextBox(content, left, shapetop, width, shapeheight) 时,文本会稍微偏离框。那么在这种情况下,不使用 shapeheight 怎么样?在这种情况下,请按如下方式修改 slide[i].insertTextBox(content, left, shapetop, width, shapeheight);

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

参考: