首页 > 解决方案 > 有没有办法在保留文本格式的同时替换 TextBox 形状内的文本?

问题描述

我正在尝试替换幻灯片文本框中的文本,同时保留先前文本的文本格式。有没有办法使用谷歌应用程序脚本方法来工作?

我尝试将 Google 幻灯片转换为 PDF,然后转换为 DOC 以获取 HTML 值,以便我可以以某种方式保留文本格式。虽然转换工作,但我被困在必须替换 TextBox 中的文本同时保留原始文本格式的部分。

到目前为止,这就是我所拥有的:

function replacePresentationContent(presentationCopyId, slideId, shapeId, content) {
  var presentationCopy = SlidesApp.openById(presentationCopyId);
  var slidesCopy = presentationCopy.getSlides();

  for (var i = 0; i < this.getSlidesCount(presentationCopy); i++) {
    var slideCopy = slidesCopy[i];
    var slidesCopyId = slideCopy.getObjectId();
    var shapesCopy = slideCopy.getShapes();

    if (slidesCopyId === slideId) {
      for (var j = 0; j < shapesCopy.length; j++) {
        if (shapesCopy[j].getObjectId() === shapeId) {
         var textRange = shapesCopy[j].getText();
           textRange.setText(content);
        }
      }
    }
  }
}

标签: google-apps-scriptgoogle-slides-apigoogle-slides

解决方案


您可以通过以下方式操作形状的文本来做到这一点:

var shape = slide.getShapes()[0]; //Change this to get the Shape you want
shape.getText().setText("The new text you want here, but with the same formatting!");

希望这可以帮助!


推荐阅读