簡體   English   中英

我可以使用 Apps 腳本在 Google 幻燈片中按段落划分文本框嗎?

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

我正在嘗試在 Apps Script 中設計一些代碼,這些代碼可以放在任何 Google 幻燈片演示文稿中,並按段落拆分每個文本框,以便每個段落都有自己的文本框。

我開始使用var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300); 讓谷歌描述的新文本框在其大部分教程中使用,但它“無法識別 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
      }
    }
}

以下是我正在嘗試做的圖片:

在預期更改之前滑動

預期更改后的近似幻燈片

我相信你的目標如下。

  • 您希望將文本框中的每個段落拆分為 Google 幻燈片上的每個文本框。
  • 您想使用 Google Apps 腳本實現此目的。

修改點:

  • 在你的腳本中,
    • getTopgetLeftgetWidthgetHeight是方法。 所以請添加()
    • 關於var content = text[j].getRange(paragraph[k])getRange沒有 arguments。
    • 關於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);

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM