我可以使用 Google Apps 脚本从一个段落中获取列表预设并将其应用于 Google 幻灯片中的另一个段落吗

时间:2021-06-17 17:37:49

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

我正在尝试在应用程序脚本中编写一些代码,以从谷歌幻灯片中的文本框中提取文本并将其按段落拆分为新的文本框,但在保留列表时遇到了障碍。我没有成功找到一种简单的方法来将 listPreset 从原始文本框中的段落获取到它们自己的文本框中的段落中。

我能够成功地使用 ListStyle() 方法 getGlyph()isInList() 但除非我制作一个字典来说明每个字形所在的 listPreset,我似乎无法检索listPreset 使用我需要的任何列出的方法来填充 applyListPreset()

我喜欢 getListPreset() 函数,这样我就可以从原始列表中获取它。有没有我遗漏的东西?

Here is an example "before" condition.

Here is an example of the expected "after" condition.

Here is an example of what the "after" condition is when I am unable to duplicate the listStyle from the original text box into the new text boxes.

在下面的示例中,我从原始文本框中复制文本并从中收集各种属性。我尝试将 listStyle() 信息输入到 applyListPreset() 方法中,即使我知道这是错误的。我只是不知道如何从原始文本框中获取列表预设。

仅供参考,我已经得到了一些帮助,所以这不是我的全部工作。

function myFunction() { // get slides in the presentation
    var slides = SlidesApp.getActivePresentation().getSlides();
    var numberOfLogs = 5;

    for (let slide of slides) { // get the objects on each slide
      splitParagraphs(slide);
    }
}

function splitParagraphs(slide){
  slideShapes = slide.getShapes();
  for(let shape of slideShapes){
      var shapetype = shape.getShapeType();
      if (shapetype == "TEXT_BOX"){ // checks to see if the object is a text box
        createSplitShapes(shape, slide);
        shape.remove();
      }
  }
}

function createSplitShapes(shape, slide){
  var paragraphs = shape.getText().getParagraphs();
  var oldHeight = shape.getHeight();
  var width = shape.getWidth();
  var newShapeHeight = oldHeight / paragraphs.length;

  for (let [index, paragraph] of paragraphs.entries() ){ // make a textbox for each paragraph distributed vertically over the original textbox
    createParagraphShapes(shape, index, paragraph, newShapeHeight, width, slide);
  }
}

function createParagraphShapes(shape, index, paragraph, shapeheight, width, slide){
  var text = paragraph.getRange();
  var list = text.getListStyle().isInList();
  var style = text.getListStyle();
  var glyph = text.getListStyle().getGlyph();
  var rawText = text.asString();
  var textStyle = text.getTextStyle();
  var fontsize = textStyle.getFontSize();
  var fontfamily = textStyle.getFontFamily();
  var fontweight = textStyle.getFontWeight();
  var paragraphStyle = text.getParagraphStyle();
  var alignment = paragraphStyle.getParagraphAlignment();
  var lineSpacing = paragraphStyle.getLineSpacing();
  var indent = paragraphStyle.getIndentStart();
  var shapetop = shapeheight * index + shape.getTop(); 

  if ( ! isBlank(rawText) ) {

    var t = slide.insertTextBox(rawText);

    t.setLeft(shape.getLeft());
    t.setTop(shapetop);
    t.setWidth(width);
    var newTextStyle = t.getText().getTextStyle();
    newTextStyle.setFontSize(fontsize);
    newTextStyle.setFontFamilyAndWeight(fontfamily, fontweight);
    var newParagraphStyle = t.getText().getParagraphStyle();
    newParagraphStyle.setParagraphAlignment(alignment);
    newParagraphStyle.setLineSpacing(lineSpacing);
    newParagraphStyle.setIndentStart(indent);

    if (list = true) {
      t.getText().getListStyle().applyListPreset(style);
    }

  }
}


function paragraphLogs(paragraph, numberOfLogs){
  if(numberOfLogs > 0){
    console.log("\tWhat's in paragraphs?: ");
    for (var k = 0; k < paragraph.length; k++){
      console.log("\t\t" + ( paragraphs[k].getRange().asString() )  );
    }
  }
}

function isBlank(str){
  return (!str || str.trim().length === 0);
}

0 个答案:

没有答案