【问题标题】:Is it possible to update/replace text in a specific slide?是否可以更新/替换特定幻灯片中的文本?
【发布时间】:2021-03-31 05:51:43
【问题描述】:

是否可以更新/替换特定幻灯片中的文本内容?

示例 - 假设我们总共有 5 张幻灯片,并且我们在第 2、3、4 号幻灯片中有 {product_name}。但我们只需要更新/替换第 3 张幻灯片中的文本 {product_name} 而不是第 2,4 号幻灯片。

ReplaceAllText 是可能的,但它会替换找到 {PRODUCT_NAME} 的所有幻灯片,但我们只需要更新特定幻灯片的文本,假设仅幻灯片 3。我进行了很多探索,但找不到实现相同目标的方法。

Snippet for ReplaceAllText (Works for all the slides replace, but we need the same for a specific slide & not for all): -

   requests = [{
      replaceAllText: {
        containsText: {
          text: '{{PRODUCT_NAME}}',
          matchCase: true
        },
        replaceText: PRODUCT_NAME
      }
    }

谢谢!

【问题讨论】:

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


    【解决方案1】:

    我相信你的目标如下。

    • 您想替换 Google 幻灯片中第三张幻灯片中 {{PRODUCT_NAME}} 的文本。
    • 您希望通过 Google Apps 脚本使用 Slides API 的 batchRequest 来实现此目的。

    在这种情况下,我认为可以使用pageObjectIds的属性。

    修改脚本:

    function myFunction() {
      const PRODUCT_NAME = "sample value"; // Please set your actual value.
      const s = SlidesApp.getActivePresentation(); // or SlidesApp.openById("id")
      const pageElementId = s.getSlides()[2].getObjectId(); // pageElementId of 3rd page.
      console.log(pageElementId)
      const resource = {
        requests: [{
          replaceAllText: {
            pageObjectIds: [pageElementId],
            replaceText: PRODUCT_NAME,
            containsText: { matchCase: true, text: "{{PRODUCT_NAME}}" }
          }
        }]
      };
      Slides.Presentations.batchUpdate(resource, s.getId());
    }
    
    • const pageElementId = s.getSlides()[2].getObjectId(); 返回第 3 页的页面元素 ID。这用于pageObjectIds: [pageElementId]

    参考资料:

    【讨论】:

    • 像魅力一样工作!但是,它不保留字体大小、字体粗细等文本样式。是否可以保留文字的风格?
    • @developer55555 感谢您的回复。关于替换文本,我很高兴您的问题得到了解决。并且,对于给您带来的不便,我深表歉意。关于However, it is not retaining the text style such as font-size, font-weight. Is it possible to retain the style of the text?,不幸的是,我无法复制您的情况。当我测试上面的脚本时,文本样式没有改变。我为这种情况道歉。为了正确了解您的情况,您能否提供您期望的示例输入和输出情况?借此,我想确认一下。
    • 在我的情况下,文本为粗体,字体大小为 18。是否可以将字体粗细保留为粗体,将字体大小保留为 18?
    • @developer55555 感谢您的回复。我不得不为我糟糕的英语水平道歉。关于In my case the text is bold and font size is 18. Is it possible to retain the font-weight as bold and font-size to 18?,当我测试上面的脚本时,the font-weight as bold and font-size to 18的文字样式没有改变。我对此深表歉意。为了正确复制您的情况,能否请教一下复制的详细流程?
    • 非常感谢!使用您的代码保留样式。
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多